rjmac / rojoma-json-jackson   1.0.1

GitHub

rojoma-json/jackson interop support

Scala versions: 2.13 2.12 2.11 2.10

rojoma-json-v3-jackson

Jackson interop for rojoma-json-v3

This package provides modules that can be added to a Jackson ObjectMapper in order to have it serialize and deserialize rojoma-json JValues. It provides implementations for both codehaus.org Jackson and fasterxml.com Jackson.

import scala.beans.BeanProperty
import com.rojoma.json.v3.ast.JValue
import com.rojoma.json.v3.interpolation._
import org.codehaus.jackson.annotate.{JsonCreator, JsonProperty}
import org.codehaus.jackson.map.ObjectMapper
import com.rojoma.json.v3.jackson.codehaus.Module // or .fasterxml.Module

case class Foo @JsonCreator() (
  @JsonProperty("someValue") @BeanProperty someValue: JValue
)

val mapper = new ObjectMapper
mapper.registerModule(Module)

val foo = Foo(j"{hello:'world'}")
mapper.writeValueAsString(foo) // == {"someValue":{"hello":"world"}}
mapper.readValue("""{"someValue":{"hello":"world"}}""", classOf[Foo]) // == foo

It also provides classes that can be used to create serializers and deserializers for things with rojoma-json JsonEncodes and JsonDecodes.

import scala.beans.BeanProperty
import com.rojoma.json.v3.ast.JValue
import com.rojoma.json.v3.util._
import org.codehaus.jackson.annotate.{JsonCreator, JsonProperty}
import org.codehaus.jackson.map.ObjectMapper
import org.codehaus.jackson.map.annotate.{JsonSerialize, JsonDeserialize}
import com.rojoma.json.v3.jackson.codehaus._ // or .fasterxml._

@JsonSerialize(using=classOf[JsonableThingSerializer])
@JsonDeserialize(using=classOf[JsonableThingDeserializer])
case class JsonableThing(x: Int, y: String)
object JsonableThing {
  implicit val jCodec = AutomaticJsonCodecBuilder[JsonableThing]
}
class JsonableThingSerializer extends ViaJValueSerializer[JsonableThing]
class JsonableThingDeserializer extends ViaJValueDeserializer[JsonableThing]

case class Foo @JsonCreator() (
  @JsonProperty("someValue") @BeanProperty someValue: JsonableThing
)

val mapper = new ObjectMapper
val foo = Foo(JsonableThing(42, "hello!"))
mapper.writeValueAsString(foo) // == {"someValue":{"x":42,"y":"hello!"}}
mapper.readValue("""{"someValue":{"x":42,"y":"hello!"}}""", classOf[Foo]) // == foo

Getting it

rojoma-json-jackston is published on Maven central, so setting up SBT is as simple as

libraryDependencies += "com.rojoma" %% "rojoma-json-v3-jackson" % "1.0.1"

While for Maven, the pom snippet is:

<dependencies>
  <dependency>
    <groupId>com.rojoma</groupId>
    <artifactId>rojoma-json-v3-jackson_${scala.version}</artifactId>
    <version>1.0.1</version>
  </dependency>
</dependencies>

rojoma-json-jackson is published for Scala version 2.10, 2.11, 2.12, and 2.13.