Over a million developers have joined DZone.

Spray JSON and ADTs

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

In this post, I’ll show you how to serialize (and deserialize) complex hierarchies of objects in Spray JSON. We’ll be dealing with ever so slightly complex tree structure. You’ll find out how to go beyond the JsonFormat[A] instances you get from calling jsonFormatn.

Let’s first begin by defining our tree. We’ll be modelling forms; a form contains fields or other forms as sub-forms. Both the form and the field need a label and an optional hint element. The field element needs to know what type of value it will hold (boolean, number, date, …). In code, this is quite nice structure:

sealed trait Element {
  def label: String
  def hint: Option[String]

This defines the base type Element that every node in our tree will share. Moving on, let’s outline the various kinds of fields we intend to support.

sealed trait FieldElementKind
case object BooleanFieldElementKind extends FieldElementKind
case object StringFieldElementKind  extends FieldElementKind
case object DateFieldElementKind    extends FieldElementKind
case object NumberFieldElementKind  extends FieldElementKind

We can now bring everything together, defining the FieldElement as well as the FormElement like so:

sealed trait FieldElement extends Element
case class ScalarFieldElement(
  label: String, 
  kind: FieldElementKind, 
  hint: Option[String]) extends FieldElement

case class FormElement(
  label: String, 
  hint: Option[String], 
  elements: List[Element]) extends Element

So, having a form: FormElement allows us to encode a form with any number of fields and any number of sub-forms and so on, recursively. Now, imagine that we have to turn this structure into JSON. Suppose our form instance is:

FormElement("Top-level form", None, List(
  ScalarFieldElement("Field 1", NumberFieldElementKind, Some("Enter 42"),
  FormElement("Sub-form", None, List(
    ScalarFieldElement("Field 1.1", BooleanFieldElementKind, None)))))

Turning it into JSON could give us something similar to:

{ "label":"Top-level form",
    {"label":"Field 1", "kind":"NumberFieldElementKind", "hint":"Enter 42"},
    {"label":"Sub-form", "elements":[
      {"label":"Field 1", "kind":"NumberFieldElementKind"}

That seems easy. But what about the way back in? We need to be able to somehow discriminate between the field and form so that we can construct the entire tree back together. So, let’s modify the JSON to be:

{ "kind":"form",
    "label":"Top-level form",
       "element":{"label":"Field 1", ... }
       "element":{"label":"Sub-form", "elements":[...]}

In other words, we’ve wrapped every element document into a document with two fields: a String defining the kind and the actual payload. This gives us the ability to discriminate when we’re marshalling the instances into JSON and back.

So, let’s finally define the Spray JSON formats for our structure:

trait FormDefinitionFormats extends DefaultJsonProtocol {

  // we write the kinds as JsStrings of their values
  implicit object ElementKindFormat extends JsonFormat[FieldElementKind] {
    def write(obj: FieldElementKind) = JsString(obj.toString)

    def read(json: JsValue): FieldElementKind = json match {
      case JsString("BooleanFieldElementKind") => BooleanFieldElementKind
      case JsString("NumberFieldElementKind")  => NumberFieldElementKind
      case JsString("StringFieldElementKind")  => StringFieldElementKind
      case JsString("DateFieldElementKind")    => DateFieldElementKind

  // we wrap every Element in document containing its kind and the
  // underlying value
  implicit def elementFormat: JsonFormat[Element] = new JsonFormat[Element] {
    def write(obj: Element) = obj match {
      case e: ScalarFieldElement => 
          ("kind", JsString("scalar")), 
          ("element", ScalarFieldElementFormat.write(e))
      case e: FormElement        => 
          ("kind", JsString("form")),
          ("element", FormElementFormat.write(e))

    def read(json: JsValue) = json match {
      case JsObject(fields) =>
        (fields("kind"), fields("element")) match {
          case (JsString("scalar"), element) => 
          case (JsString("form"),   element) => 

  implicit val ScalarFieldElementFormat = jsonFormat3(ScalarFieldElement)
  implicit val FormElementFormat = jsonFormat3(FormElement)


And there you have it. We are able to use our tree structure and deal with all the ADTs with as little painful code as possible.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


Published at DZone with permission of Jan Machacek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}