i have problem when trying serialize sequences of anyval using json4s in scala.
here test using funsuite reproduces problem:
import org.json4s._ import org.json4s.jackson.jsonmethods._ import org.json4s.jackson.serialization._ import org.scalatest.{funsuite, matchers} case class myid(id: string) extends anyval case class mymodel(ids: seq[myid]) class anyvaltest extends funsuite matchers { test("should serialize correctly") { implicit val formats = defaultformats val model = mymodel(seq(myid("1"), myid("2"))) val text = write(model) parse(text).extract[mymodel] shouldbe model } } the test fails when trying extract mymodel jvalue because can not find suitable value ids field.
i notice anyval working fine when used directly though like:
case class anothermodel(id: myid) then able serialise , deserialise correctly.
i know question 1 year old ran same issue. writing did in case helps else. need custom serializer.
case class id(asstring: string) extends anyval class notificationserializer extends customserializer[id](format ⇒ ( {case jstring(s) => id(s)}, {case id(s) => jstring(s)})) without above serialization, json
{"ids":[[{"asstring":"testid1"},{"asstring":"testid2"}]]} i not entirely sure why anyval case class serialization works fine when part of case class not standalone. best guess behavior due allocation behavior of jvm array containing value classes. see http://docs.scala-lang.org/overviews/core/value-classes.html 'when allocation necessary' section.
Comments
Post a Comment