json-extensions


License

License

Categories

Categories

JSON Data
GroupId

GroupId

io.onema
ArtifactId

ArtifactId

json-extensions_2.12
Last Version

Last Version

0.5.1
Release Date

Release Date

Type

Type

jar
Description

Description

json-extensions
json-extensions
Project URL

Project URL

https://github.com/onema/JsonExtensions
Project Organization

Project Organization

io.onema
Source Code Management

Source Code Management

https://github.com/onema/JsonExtensions

Download json-extensions_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/io.onema/json-extensions_2.12/ -->
<dependency>
    <groupId>io.onema</groupId>
    <artifactId>json-extensions_2.12</artifactId>
    <version>0.5.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.onema/json-extensions_2.12/
implementation 'io.onema:json-extensions_2.12:0.5.1'
// https://jarcasting.com/artifacts/io.onema/json-extensions_2.12/
implementation ("io.onema:json-extensions_2.12:0.5.1")
'io.onema:json-extensions_2.12:jar:0.5.1'
<dependency org="io.onema" name="json-extensions_2.12" rev="0.5.1">
  <artifact name="json-extensions_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.onema', module='json-extensions_2.12', version='0.5.1')
)
libraryDependencies += "io.onema" % "json-extensions_2.12" % "0.5.1"
[io.onema/json-extensions_2.12 "0.5.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.8
org.json4s : json4s-jackson_2.12 jar 3.6.6
com.fasterxml.jackson.datatype : jackson-datatype-joda jar 2.9.9

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.0.8

Project Modules

There are no modules declared in this project.

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)

Versions

Version
0.5.1
0.5.0
0.4.0
0.3.1
0.3.0
0.2.0
0.1.0