JSON4S type hint does not work JSON4S type hint does not work json json

JSON4S type hint does not work


Implement Contact using traits and it will work.


This is an example of how you could use FullTypeHints.

You cannot ask implicitly for a Validable[Field], neither adding multiple explicit arguments lists (and this happens also for implicitly parameters, see note below) because they are not supported by JSON4S.

Note: asking for a Validable[Field] implicitly with Contact[Field: Validable] syntax is equal to adding an additional arguments list with implicit validable: Validable[Field].

You could, instead, add an additional parameter validable: Validable[Field] to the Contact constructor or (as in the example below) a validable field that should be overridden by concrete classes (like Mail) extending the Contact abstract class.

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)

The serialized JSON representation is the following:

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