I have a problem trying to serialize AnyVal sequences using json4s in scala.
Here is a test using FunSuite that reproduces the 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 with 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 does not work when trying to retrieve MyModel from JValue, because it cannot find a suitable value for the field ids.
I notice that AnyVal works fine when used directly, but with something like:
case class AnotherModel(id: MyId)
Then I can serialize and deserialize correctly.
source
share