<img src="https://raw.githubusercontent.com/adlawson/scala-json4s/master/logo.png" alt=JSON4S" align="right" width=200/>
JSON4S Extensions is a small collection of serializers, formatters and other useful extensions for JSON4S in Scala.
The ExtendedFormats
object (and trait) works in exactly the same way
as JSON4S DefaultFormats
, but with a few other useful serializers included by
default.
package mypackage
import com.adlawson.json4s.ExtendedFormats
import org.json4s.native.Serializer.{read, write}
implicit val formats = ExtendedFormats
val foo = read[Foo](someJsonString)
val raw = write(foo)
Included in ExtendedFormats
are the following serializers
CharSerializer
for [de]serializingChar
field typesTypeFieldSerializer
for mapping a JSONtype
field totyp
in a class
The abstract TypeSerializer
can be used to create custom serializers
for complex types, including types with mixins and matching trait
implementations.
package mypackage
import com.adlawson.json4s.TypeSerializer
import org.json4s.DefaultFormats
type FooBar = Foo with Bar
case class Foo(a: Int)
trait Bar { def b: Int }
private[mypackage] case class _FooBar(a: Int, b: Int)
object FooBarSerializer extends TypeSerializer[FooBar, _FooBar] {
override def wrap(f: FooBar) = _FooBar(f.a, f.b)
override def unwrap(f: _FooBar) = new Foo(f.a) with Bar { val b = f.b }
}
implicit val format = DefaultFormats + FooBarSerializer
val fbar = read[FooBar]("""{"a":1,"b":2}""")
val json = write(fbar)
For further use cases and working examples of the different library components,
please see the bundled examples
.
Contributions are accepted via Pull Request, but passing unit tests must be included before it will be considered for merge.
The content of this library is released under the MIT License by
Andrew Lawson.
You can find a copy of this license in
LICENSE
or at http://opensource.org/licenses/mit.