JSON Extensions

Code Build Codacy Badge Codacy Badge LICENSE

Simple wrapper for json4s to simplify de/serialization of JSON.

Usage

Deserialize JSON to a Scala Case Class

import io.onema.json.Extensions._

case class TestJsonFoo(name: String, value: String, id: Int = 0)

val fooJson = "{\"name\": \"test\", \"value\": \"foo\"}"

val fooObject = fooJson.jsonDecode[TestJsonFoo]

fooObject.name should be("test")
fooObject.value should be("foo")
fooObject.id should be(0)

Serialize Scala Class to JSON string

import io.onema.json.Extensions._

case class Message(data: Seq[String])
val result = "{\"data\":[\"http://foo.com\",\"http://bar.com\",\"http://baz.com\",\"http://blah.org\"]}"
val message = Message(Seq("http://foo.com", "http://bar.com", "http://baz.com", "http://blah.org"))

val jsonValue = message.asJson

jsonValue should be(result)

Serialize using a custom serializer

Case class and custom types

case class TestWithTypes(name: String, testType: TestType)
object TestTypes {
  sealed abstract class TestType(val name: String)
  case object TEST_1 extends TestType("work-request")
  case object TEST_2 extends TestType("process-request")
}

Custom Serializer

object TestTypeSerializer extends CustomSerializer[TestType](format => ({
  case JString("test-1") => TEST_1
  case JString("test-2") => TEST_2
}, {
  case TEST_1 => JString("test-1")
  case TEST_2 => JString("test-2")
}
))

Using the custom serializer

val result = "{\"name\":\"foo\",\"testType\":\"test-1\"}"
val obj = TestWithTypes("foo", TEST_1)
val jsonValue = obj.asJson(TestTypeSerializer)
jsonValue should be(result)

Deserialize to Java POJO

import io.onema.json.JavaExtensions._

val fooJson = "{\"name\": \"test\", \"value\": \"foo\"}"

val fooObject = fooJson.jsonDecode[TestJsonPojo]

fooObject.getName should be("test")
fooObject.getValue should be("foo")
fooObject.getId should be(0)

Serialize java POJO to JSON string

import io.onema.json.JavaExtensions._

val result = "{\"data\":[\"http://foo.com\",\"http://bar.com\",\"http://baz.com\",\"http://blah.org\"]}"
val message = new TestJsonPojo()

val jsonValue = message.asJson

jsonValue should be(result)

Using a Custom Object Mapper for POJOs

import io.onema.json.JavaExtensions._
import com.fasterxml.jackson.databind.ObjectMapper
val mapper: ObjectMapper = new ObjectMapper()
                              .registerModule(new JodaModule)
                              .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
                              .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val fooObject = json.jsonDecode[TestJsonPojo](mapper)