Overview
Genson provides an easy to use boiler free integration with Scala. It supports common Scala mutable and immutable collections, Option, tuples, case classes and even functions (when class metadata ser/de is enabled)!
Standard classes will be ser/de with the classic Genson behaviour for Java. Genson classic features and annotations are supported (JsonIgnore, JsonProperty, JsonConverter, etc), bringing the power of Genson to Scala.
To get you started with Genson in Scala you only have to import com.owlike.genson.defaultGenson._ and you are done!
Scala version
Genson has been tested against scala 2.11 and 2.12. In theory you can use it with all releases from 2.11 and up. We follow the standard cross build conventions for scala projects ex: genson-scala_2.11 and genson-scala_2.12. If you need support for Scala 2.10, you will need to look at the older releases of Genson pre 1.5.
Download
In order to keep the main Genson jar small we chose to provide the Scala extension as a separate jar. The other reason is that some tools might not handle well optional dependencies, in result someone that does not use the Scala extension could end up with the scala language library downloaded.
SBT
Maven
Manual Download
If you don’t use a dependency management tool then you can still use it, but you will have to download the Scala extension and the core Genson jars by hand.
Collections & Tuples
Genson provides a set of default converters for most scala collections (mutable and immutable). Scala provides some optimized types for maps and collections of few elements, Genson will respect it and deserialize to the optimized type.
Genson will serialize scala Tuple1 to 22, as json arrays. There is no restriction on the complexity of the tuples (may contain options, case classes, etc).
Deserialize Unknown Types
When you don’t specify the type to use, Genson will deserialize:
- Integer json numbers to Long
- Double json numbers to Double
- Json objects to scala immutable Map
- Json arrays to scala immutable List
- null to null
Case classes
Genson has also out of the box support for case classes. By default Case classes ser/de will follow these rules:
- Serialize only attributes defined in the main constructor and vars defined in the class, ex: if you define a val in the class but not in the constructor, then it won’t be serialized.
- If multiple constructors are defined, only the default one will be used (that has a default apply method generated in its companion object)
- Set/Get methods are not used as it does not make sense with case classes. If you want to work with java bean conventions then use plain old java classes.
Genson will serialize None as null and null is deserialized as None.
Customizing
Of course if the default configuration does not fit your needs you can make a custom Genson instance and then use it. For example lets say we want to enable indentation, serialize objects based on their runtime type and want to ser/de dates as timestamps and all fields from case classes (not only the ones present in the constructor or the vars).
For a more in depth overview of Genson features and customization have a look at the User Guide and configuration sections.
AST support via json4s
Instead of creating again another DOM structure like all the existing ones, Genson provides it by supporting json4s. Json4s defines an AST for JSON and utilities to work with it.
In order to use json4s features with Genson, you need to add a dependency to it.