producing json from case classes
I posted previously about consuming JSON using the Scala json4s library.
Today I learned something new about that library when trying to produce JSON.
The situation: I want to produce json where each top-level key points to a JSON Array that was created from a List
of case class
es
To be specific I want to convert the following Scala:
to this JSON:
(for example’s sake, please imagine the case classes in use have more than just a single field…)
First..imports are important! Esp. the JsonDSL
one for this example.
Using the docs, I would expect the following Scala code to produce the intended JSON using the json4s ~
operator:
However it fails with the following error:
So what is going on here? It seems the library does not know how to serialize our case class.
This can be confirmed by trying the same approach using a list of primitive strings instead of case classes:
That code checks out just fine. So what’s the fix?
The answer lies in the json4s Extraction
object. Its decompose()
method will “Decompose a case class into JSON.”
Usage:
This will produce the intended output.