DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Spray JSON and ADTs

Spray JSON and ADTs

Jan Machacek user avatar by
Jan Machacek
·
Dec. 08, 12 · Interview
Like (0)
Save
Tweet
Share
6.33K Views

Join the DZone community and get the full member experience.

Join For Free

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.





JSON

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Memory Debugging: A Deep Level of Insight
  • Java Development Trends 2023
  • Secrets Management

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: