amnaredo / test

0 stars 0 forks source link

Custom pickling for sealed hierarchies #254

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

Citing the manual, "sealed hierarchies are serialized as tagged values, the serialized object tagged with the full name of the instance's class". It works like this:

sealed trait A
case class B(x: Int) extends A
case class C(x: Boolean) extends A

implicit val aW: Writer[A] = Writer.merge(macroW[B], macroW[C])
println(write(s, 2))
[
  {
    "$type": "Tests.B",
    "x": 1
  },
  {
    "$type": "Tests.C",
    "x": false
  }
]

Tagged attribute is not the most beautiful, moreover, the package name is hardcoded into Json, making it vulnerable to any refactoring.

Is there an easy way to override the default picklers and achieve the following result?

[
  {
    "B" : {
      "x" : 1
    }
  },
  {
    "C" : {
      "x" : false
    }
  }
]

Thanks! ID: 272 Original Author: kamilkloch

amnaredo commented 3 years ago

questions go in the gitter channel. Let's discuss there :) Original Author: lihaoyi