Skip to content
debasishg edited this page Feb 18, 2012 · 8 revisions

sjson: Non intrusive JSON Serialization in Scala

sjson offers serialization of JSON in Scala. You can either use the transparent reflection based serialization protocol or the typeclass based protocol that does not use reflection. sjson supports serialization of primitive data types, basic Scala data types and generic data types like List, Map and Set.

Reflection based JSON Serialization

This is as simple as having your Scala object as ..

val addr = Address("Market Street", "San Francisco", "956871")

and using sjson API as ..

serializer.in[Address](serializer.out(addr))

to serialize out to JSON and back in to the object. More details at Reflection based JSON Serialization

Typeclass based JSON Serialization without Reflection

Suppose you have a Scala list as ..

val list = List(100, 200, 300, 400)

Use the serialization protocol API for serializing to JSON and back ..

fromjson[List[Int]](tojson(list))

More details at Typeclass based JSON Serialization

Typeclass based JSON Serialization with Error Accumulation

[Moved to new repository sjsonapp]

This provides the same API as the typeclass based serialization with a slight deviation. fromjson returns a Validation, which can be used as an Applicative to accumulate all error messages that can occur during de-serialization. Here's an example ..

val a = Address(12, "Monroe Street", "Denver", "80231")
(fromjson[Address](tojson(a))).fail.toOption.get.list should equal (List("field number not found", "field stret not found", "field City not found"))

More details at Applicative style APIs for JSON serialization