reugn / scalikejackson

Lightweight Scala JSON library
Apache License 2.0
7 stars 1 forks source link
jackson json play-json scala

ScalikeJackson

Download Build Status

ScalikeJackson is a lightweight scala JSON library which provides play-json like interface and backed by Jackson.
A collection of benchmarks results can be viewed here.

Partial play-json features support

Basic operations

Reading and writing objects

Getting started

Add bintray resolver:

resolvers += Resolver.bintrayRepo("reug", "maven")

Add ScalikeJackson as a dependency in your project:

libraryDependencies += "reug" %% "scalikejackson" % "<version>"

Usage

Basic operations:

import reug.scalikejackson.play.Json
import reug.scalikejackson.ScalaJacksonImpl._
import reug.scalikejackson.utils.Converters._

val obj = Json.obj("i" -> 1,
    "b" -> false,
    "o" -> Json.obj("s" -> "str"),
    "arr" -> Json.arr(1, 2, 3))

(obj \ "o" \ "s").asOpt[String]
(obj \\ "s").head.as[String]
(obj \ "arr").asSeq[Int]

Json.stringify("""{"i":1}""".toJson)

Reading and writing objects:

import reug.scalikejackson.play.Json
import reug.scalikejackson.ScalaJacksonImpl._

case class MockStruct(
                         i: Int,
                         s: String,
                         b: Option[Boolean]
                     )
case class Container(
                        i_str: String,
                        i_mock: MockStruct
                    )

val mock_instance = MockStruct(1, "a", Some(true))
val container_instance = Container("asdf", mock_instance)

val mock_writes = Json.writes[MockStruct](
    p => (
        ("in", classOf[Int], p.i),
        ("sn", classOf[String], p.s),
        ("bn", Option(classOf[Boolean]), p.b)
    )
)

val mock_reads = Json.reads[MockStruct](
    ("in", classOf[Int]),
    ("sn", classOf[String]),
    ("bn", Option(classOf[Boolean]))
)

implicit val mock_format = Json.format[MockStruct]() or(mock_writes, mock_reads)
implicit val container_format = Json.format[Container]() or(mock_writes, mock_reads)

mock_instance.write shouldBe """{"in":1,"sn":"a","bn":true}"""
container_instance.write shouldBe """{"i_str":"asdf","i_mock":{"in":1,"sn":"a","bn":true}}"""

Support custom mapper configuration:

implicit val format = Json.format[MockStruct](PropertyNamingStrategy.SNAKE_CASE)

Contributing

Feedback, bug reports, and pull requests are greatly appreciated.

License

Licensed under the Apache 2.0 License.