英文:
Use functions from a Scala object that have implicit parameters in java code
问题
这里有没有一种方法可以在我的 Java 代码中使用 fromJson
和 toJson
方法?我希望调用它们将我从 MongoDB 获取的 BSON 文档转换成我在 Scala 中拥有的模型类。
我的 Scala 对象如下,
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper}
object JsonUtils {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
def toJson(value: Map[Symbol, Any]): String = {
toJson(value map { case (k, v) => k.name -> v })
}
def toJson(value: Any): String = {
mapper.writeValueAsString(value)
}
def toMap[V](json: String)(implicit m: Manifest[V]) = fromJson[Map[String, V]](json)
def fromJson[T](json: String)(implicit m: Manifest[T]): T = {
mapper.readValue[T](json)
}
implicit class Marshallable[T](marshallMe: T) {
def toJson: String = JsonUtils.toJson(marshallMe)
}
implicit class Unmarshallable(unMarshallMe: String) {
def toMap: Map[String, Any] = JsonUtils.toMap(unMarshallMe)
def fromJson[T]()(implicit m: Manifest[T]): T = JsonUtils.fromJson[T](unMarshallMe)
}
}
英文:
Is there a way that I can use the fromJson
and toJson
methods in my java code? I would want to call it to transform a bson document that I get from mongoldb to a model class that I have in Scala
My Scala object is below,
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper}
object JsonUtils {
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
def toJson(value: Map[Symbol, Any]): String = {
toJson(value map { case (k,v) => k.name -> v})
}
def toJson(value: Any): String = {
mapper.writeValueAsString(value)
}
def toMap[V](json:String)(implicit m: Manifest[V]) = fromJson[Map[String,V]](json)
def fromJson[T](json: String)(implicit m : Manifest[T]): T = {
mapper.readValue[T](json)
}
implicit class Marshallable[T](marshallMe: T) {
def toJson: String = JsonUtils.toJson(marshallMe)
}
implicit class Unmarshallable(unMarshallMe: String) {
def toMap: Map[String,Any] = JsonUtils.toMap(unMarshallMe)
def fromJson[T]()(implicit m: Manifest[T]): T = JsonUtils.fromJson[T](unMarshallMe)
}
}
答案1
得分: 1
在Java代码中,您必须手动解析Scala的隐式参数。
尝试像这样调用 fromJson
:
JsonUtils.fromJson("a", scala.reflect.Manifest.classType(LocalUser.class));
或者
JsonUtils$.MODULE$.fromJson("a", scala.reflect.Manifest$.MODULE$.classType(LocalUser.class));
关于 toJson
,我看不出有问题。尽管如果您调用带有 Map
参数的重载版本的 toJson
,也许您需要在 scala.Predef.Map
(也称为 scala.collection.immutable.Map
)和 java.util.Map
之间进行转换。
https://stackoverflow.com/questions/16918956/convert-java-map-to-scala-map
顺便说一下,Manifest
已经被 ClassTag
/TypeTag
所取代。
https://docs.scala-lang.org/overviews/reflection/typetags-manifests.html
英文:
In Java code you have to resolve Scala implicits manually.
Try to call fromJson
like
JsonUtils.fromJson("a", scala.reflect.Manifest.classType(LocalUser.class));
or
JsonUtils$.MODULE$.fromJson("a", scala.reflect.Manifest$.MODULE$.classType(LocalUser.class));
Regarding toJson
I can't see problems. Although if you call overloaded version of toJson
with Map
argument, maybe you'll have to convert between scala.Predef.Map
aka scala.collection.immutable.Map
and java.util.Map
https://stackoverflow.com/questions/16918956/convert-java-map-to-scala-map
By the way, Manifest
is deprecated in favor of ClassTag
/TypeTag
https://docs.scala-lang.org/overviews/reflection/typetags-manifests.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论