Deserializing to java objects with Scala and json4s Deserializing to java objects with Scala and json4s json json

Deserializing to java objects with Scala and json4s


Key for solving your issue, is composition of formatters. Basically you want to define JList formatter as list formatter composed with toJList function.Unfortunately, json4s Formatters are extremely difficult to compose, so I used the Readers for you to get an idea. I also simplified an example, to having only java list:

import DefaultReaders._import scala.collection.JavaConverters._implicit def javaListReader[A: Reader]: Reader[java.util.List[A]] = new Reader[util.List[A]] {        override def read(value: JValue) = DefaultReaders.traversableReader[List, A].read(value).asJava}val input = """["a", "b", "c"]"""val output = Formats.read[java.util.List[String]](parse(input))

To my knowledge json4s readers will not work with java classes out of the box, so you might either need to implement the Serializer[JList[_]] the same way, or mirror your java classes with case classes and use them inside your domain.

P.S.Highly recommend you to switch to circe or argonaut, then you will forget about the most problems with jsons.