使用在Java代码中具有隐式参数的Scala对象的函数。

huangapple go评论80阅读模式
英文:

Use functions from a Scala object that have implicit parameters in java code

问题

这里有没有一种方法可以在我的 Java 代码中使用 fromJsontoJson 方法?我希望调用它们将我从 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));

基于 https://stackoverflow.com/questions/21582045/calling-a-scala-from-java-play-framework-which-takes-a-classtag

关于 toJson,我看不出有问题。尽管如果您调用带有 Map 参数的重载版本的 toJson,也许您需要在 scala.Predef.Map(也称为 scala.collection.immutable.Map)和 java.util.Map 之间进行转换。

https://stackoverflow.com/questions/16918956/convert-java-map-to-scala-map

https://stackoverflow.com/questions/16583265/how-can-i-convert-scala-map-to-java-map-with-scala-float-to-java-float-k-v-conve

顺便说一下,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));

Based on https://stackoverflow.com/questions/21582045/calling-a-scala-from-java-play-framework-which-takes-a-classtag

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

https://stackoverflow.com/questions/16583265/how-can-i-convert-scala-map-to-java-map-with-scala-float-to-java-float-k-v-conve

By the way, Manifest is deprecated in favor of ClassTag/TypeTag

https://docs.scala-lang.org/overviews/reflection/typetags-manifests.html

huangapple
  • 本文由 发表于 2020年10月7日 18:06:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定