2017-10-19 18 views
6

L'extrait de test suivantJSON4S indice de type ne fonctionne pas

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

val serialized = Serialization.write(List(Mail(field = "[email protected]", note = "Random note."))) 
println(serialized) 

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

échoue avec

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 est

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

et Mail est

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) 
    } 
} 

et sortie de test est

[{"jsonClass":"whatever.core.entities.utility.contact.Mail","field":"[email protected]","created":1508428385266,"updated":1508428385266,"note":"Random note."}] 

Creuser dans le code de base de JSON4S montre que indication de type ne sert pas du tout.

Comment puis-je forcer JSON4S à utiliser des indications de type?

Vive

Répondre

0

Mettre en oeuvre Contact utilisant trait s et il fonctionnera.

+0

Vous n'avez pas à (même si cela fonctionne réellement), jetez un oeil à ma réponse pour en connaître la cause. –

0

Ceci est un exemple de la façon dont vous pouvez utiliser FullTypeHints.

Vous ne pouvez pas demander implicitement un Validable[Field], ni d'ajouter plusieurs listes d'arguments explicites (et cela arrive aussi pour les paramètres implicitement, voir note ci-dessous) car ils ne sont pas supportés par JSON4S.

Remarque: demander implicitement un Validable[Field] avec la syntaxe Contact[Field: Validable] équivaut à ajouter une liste d'arguments supplémentaire avec implicit validable: Validable[Field].

Vous pouvez, à la place, ajouter un paramètre supplémentaire validable: Validable[Field] au constructeur Contact ou (comme dans l'exemple ci-dessous) un champ validable qui devrait être remplacé par des classes concrètes (comme Mail) étendant la Contact classe abstraite.

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 = "[email protected]", note = "Random note.")) 
val serialized = Serialization.write(mail) 
val mailS = Serialization.read[List[Contact[_]]](serialized) 

print(mail == mailS) 

La serialized représentation JSON est la suivante:

[ 
    { 
    "jsonClass":"whatever.core.entities.utility.contact$Mail", 
    "field":"[email protected]", 
    "created":1509098018776, 
    "updated":1509098018776, 
    "note":"Random note." 
    } 
] 
+0

N'est-ce pas le même code que le mien? – Dyin

+0

Désolé, j'ai manqué le besoin de 'Validable [Field]', mis à jour ma réponse pour y faire face. Maintenant, il devrait pleinement répondre à votre question. –

+0

Merci, mais cela ne va pas fonctionner, puisque 'Contact [Field]' est une classe abstraite. 'Contact [Field]' doit être un trait. – Dyin