Enthusiastic Java, Scala and Haskell programmer with a long history of large and successful systems. Known author, speaker, motivator and coach. Jan is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website. View Full User Profile

Spray JSON and ADTs

12.08.2012
| 2506 views |
  • submit to reddit

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",
  "elements":[
    {"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",
  "element":{
    "label":"Top-level form",
    "elements":[
      {"kind":"field",
       "element":{"label":"Field 1", ... }
      },
      {"kind":"form",
       "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 => 
        JsObject(
          ("kind", JsString("scalar")), 
          ("element", ScalarFieldElementFormat.write(e))
        )
      case e: FormElement        => 
        JsObject(
          ("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) => 
            ScalarFieldElementFormat.read(element)
          case (JsString("form"),   element) => 
            FormElementFormat.read(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.





Published at DZone with permission of Jan Machacek, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)