How can I deserialize a json array using lift-json for scala vector?
For instance:
case class Foo(bar: Vector[Bar]) trait Bar { def value: Int } case class Bar1(value: Int) extends Bar case class Bar2(value: Int) extends Bar import net.liftweb.json.{ShortTypeHints, Serialization, DefaultFormats} implicit val formats = new DefaultFormats { override val typeHintFieldName = "type" override val typeHints = ShortTypeHints(List(classOf[Foo],classOf[Bar1],classOf[Bar2])) } println(Serialization.writePretty(Foo(Vector(Bar1(1), Bar2(5), Bar1(1)))))
Result:
{ "type":"Foo", "bar":[{ "type":"Bar1", "value":1 },{ "type":"Bar2", "value":5 },{ "type":"Bar1", "value":1 }] }
Good. But when I try to deserialize this line
println(Serialization.read[Foo](Serialization.writePretty(Foo(Vector(Bar1(1), Bar2(5), Bar1(1))))))
I get an exception:
net.liftweb.json.MappingException: The developed JSON values do not match the class constructor args = List (Bar1 (1), Bar2 (5), Bar1 (1)) arg types = scala.collection.immutable. $ colon $ colon constructor = public test.Foo (scala.collection.immutable.Vector)
This means that the json array is associated with the scala list, and not with the vector type defined in the Foo class. I know there is a way to create my own serializer by extending net.liftweb.json.Serializer and including it in the value format. But how can I recover the type of objects that are stored in Vector. I want to get the result of deserialization as follows:
Foo (Vector (Bar1 (1), Bar2 (5), Bar1 (1)))