6

以下测试片段

implicit val formats = DefaultFormats + FullTypeHints(Contacts.classList)

val serialized = Serialization.write(List(Mail(field = "random@mail.com", note = "Random note.")))
println(serialized)

Serialization.read[List[Contact[_]]](serialized).isInstanceOf[List[Mail]] should be (true)

失败了

Can't find constructor for Contact[Object]
org.json4s.package$MappingException: Can't find constructor for Contact[Object]
    at org.json4s.reflect.package$.fail(package.scala:95)
    at org.json4s.reflect.ScalaSigReader$$anonfun$5.apply(ScalaSigReader.scala:21)
    at org.json4s.reflect.ScalaSigReader$$anonfun$5.apply(ScalaSigReader.scala:21)
    at scala.Option.getOrElse(Option.scala:121)
    at org.json4s.reflect.ScalaSigReader$.readConstructor(ScalaSigReader.scala:21)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder.ctorParamType(Reflector.scala:93)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$createConstructorDescriptors$3$$anonfun$15.apply(Reflector.scala:156)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$createConstructorDescriptors$3$$anonfun$15.apply(Reflector.scala:142)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$createConstructorDescriptors$3.apply(Reflector.scala:142)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$createConstructorDescriptors$3.apply(Reflector.scala:136)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
    at scala.collection.mutable.ArraySeq.foreach(ArraySeq.scala:74)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder.createConstructorDescriptors(Reflector.scala:136)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder.constructorsAndCompanion(Reflector.scala:121)
    at org.json4s.reflect.Reflector$ClassDescriptorBuilder.result(Reflector.scala:183)
    at org.json4s.reflect.Reflector$.createDescriptor(Reflector.scala:53)
    at org.json4s.reflect.Reflector$$anonfun$describe$1.apply(Reflector.scala:48)
    at org.json4s.reflect.Reflector$$anonfun$describe$1.apply(Reflector.scala:48)
    at org.json4s.reflect.package$Memo.apply(package.scala:36)
    at org.json4s.reflect.Reflector$.describe(Reflector.scala:48)
    at org.json4s.Extraction$$anonfun$extract$6.apply(Extraction.scala:393)
    at org.json4s.Extraction$$anonfun$extract$6.apply(Extraction.scala:392)
    at org.json4s.Extraction$.customOrElse(Extraction.scala:606)
    at org.json4s.Extraction$.extract(Extraction.scala:392)
    at org.json4s.Extraction$CollectionBuilder$$anonfun$7.apply(Extraction.scala:410)
    at org.json4s.Extraction$CollectionBuilder$$anonfun$7.apply(Extraction.scala:410)
    at scala.collection.immutable.List.map(List.scala:284)
    at org.json4s.Extraction$CollectionBuilder.mkCollection(Extraction.scala:410)
    at org.json4s.Extraction$CollectionBuilder.result(Extraction.scala:430)
    at org.json4s.Extraction$$anonfun$extract$5.apply(Extraction.scala:382)
    at org.json4s.Extraction$$anonfun$extract$5.apply(Extraction.scala:382)
    at org.json4s.Extraction$.customOrElse(Extraction.scala:606)
    at org.json4s.Extraction$.extract(Extraction.scala:382)
    at org.json4s.Extraction$.extract(Extraction.scala:39)
    at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
    at org.json4s.jackson.Serialization$.read(Serialization.scala:50)
    at org.json4s.Serialization$class.read(Serialization.scala:30)
    at org.json4s.jackson.Serialization$.read(Serialization.scala:17)

Contact在哪里

abstract class Contact[Field : Validable](
  val field: Field,
  val created: Long,
  val updated: Long,
  val note: String) { }

并且Mail

case class Mail(
  override val field: String,
  override val created: Long = System.currentTimeMillis(),
  override val updated: Long = System.currentTimeMillis(),
  override val note: String)
extends Contact[String](field, created, updated, note)(Mail)

case object Mail extends Validable[String] {
  override def valid(field: String): Boolean = {
    Validator.email(field)
  }
}

和测试输出是

[{"jsonClass":"whatever.core.entities.utility.contact.Mail","field":"random@mail.com","created":1508428385266,"updated":1508428385266,"note":"Random note."}]

深入研究 JSON4S 的代码库表明根本没有使用类型提示。

如何强制 JSON4S 使用类型提示?

干杯

4

2 回答 2

0

这是您如何使用的示例FullTypeHints

您不能隐式要求 a Validable[Field],也不能添加多个显式参数列表(隐式参数也会发生这种情况,请参见下面的注释),因为 JSON4S 不支持它们。

注意:请求Validable[Field]隐式 withContact[Field: Validable]语法等于添加一个额外的参数列表 with implicit validable: Validable[Field]

相反,您可以validable: Validable[Field]Contact构造函数添加一个附加参数,或者(如下例所示)一个validable应该被Mail扩展Contact抽象类的具体类(如 )覆盖的字段。

trait Validable[T]{
  def valid(field: T): Boolean
}

abstract class Contact[Field](
  val field: Field, val created: Long, 
  val updated: Long, val note: String) {
  val validable: Validable[Field]
}

object Contacts{ val classList = List(classOf[Mail]) }

case class Mail(
  override val field: String,
  override val created: Long = System.currentTimeMillis(),
  override val updated: Long = System.currentTimeMillis(),
  override val note: String
) extends Contact[String](field, created, updated, note){
  override val validable: Validable[String] = Mail
}

case object Mail extends Validable[String] {
  override def valid(field: String): Boolean = true
}

implicit val formats = DefaultFormats + FullTypeHints(Contacts.classList)
val mail: List[Mail] = List(Mail(field = "random@mail.com", note = "Random note."))
val serialized = Serialization.write(mail)
val mailS = Serialization.read[List[Contact[_]]](serialized)

print(mail == mailS)

serializedJSON 表示如下:

[
  {
    "jsonClass":"whatever.core.entities.utility.contact$Mail",
    "field":"random@mail.com",
    "created":1509098018776,
    "updated":1509098018776,
    "note":"Random note."
  }
]
于 2017-10-25T22:13:57.013 回答
0

Contact使用s实现trait,它将起作用。

于 2017-10-27T09:25:53.500 回答