ninny-json

JSON typeclasses that know the difference between null and absent fields

View the Project on GitHub nrktkt/ninny-json

Automatic derivation for ToJson and FromJson

Rather than implementing ToJson and FromJson by hand, you can generate them automatically using

Full-auto derivation for ToJson and FromJson

If you like you can even skip the declaration by mixing in AutoToJson or AutoFromJson, or importing nrktkt.ninny.Auto._.

Modifying field names with annotations

You can change the name of a field being read to/from JSON using the @JsonName annotation.

Modifying field names with rules

You can change the name of all fields being considered by a typeclass using the preprocess and postprocess methods.
Below is an example of converting a camel cased case class to snake case using guava CaseFormat.

Note that if the renamed typeclass derives from another, like ToJson[Person] does from ToJson[Address], then that dependency type class will still dictate its own naming.

And therefore in this case we can see the address is still camel cased.

{
  "first_name": "Jim",
  "last_name": "Bob",
  "kids": [],
  "address": {
    "houseNumber": 718,
    "street": "Ashbury St."
    "zip": "94117",
  }
}

To make both use snake case, apply the renaming process to the Address typeclass as well.

Using default values for optional fields

If your case class has optional parameters then you can use their default values when a field is absent by importing the feature flag FromJsonAuto.useDefaults in the scope of the derivation for your FromJson.

Handling null references

We don’t usually expect to have null references in Scala. So the default behavior is for null references to be passed to ToJson instances where they could be handled for a specific type.
However if you need to handle null references generally we have NullPointerBehavior.
By providing an instance of NullPointerBehavior in the scope of your ToJson derivation you can control if null references are passed to ToJson instances or if a value is provided instead. NullPointerBehavior.Ignore and IgnorePointerBehavior.WriteNull are provided the typical uses cases.