com.rojoma.json.v3.util
Specifies that the base codec should wrap the value generated by subclasses' com.rojoma.json.v3.codec.JsonEncodes and com.rojoma.json.v3.codec.JsonDecodes in another object containing two fields; one for the type-tag and one for the actual value.
abstract class Base case class SubclassA(name: String) extends Base case class SubclassB(x: Int, y: Int) extends Base implicit val aCodec = SimpleJsonCodecBuilder[SubclassA].build("name", _.name) implicit val bCodec = SimpleJsonCodecBuilder[SubclassB].build("x", _.x, "y", _.y) val baseCodec = SimpleHierarchyCodecBuilder[Base](TagAndValue("type", "value")). branch[SubclassA]("a"). branch[SubclassB]("b"). build println(baseCodec.encode(SubclassA("John"))) // { "type" : "a", "value" : { "name" : "John" } } println(baseCodec.encode(SubclassB(1, 2))) // { "type" : "b", "value" : { "x" : 1, "y" : 2 } }
Specifies that the base codec should wrap the value generated by subclasses' com.rojoma.json.v3.codec.JsonEncodes and com.rojoma.json.v3.codec.JsonDecodes in another object containing two fields; one for the type-tag and one for the actual value.