How to configure PlayFramework JSON serializer to exclude null values

What is the recommended way to configure the jackson ObjectMapper used by play.libs.Json to include only non-null properties across the board?

Normally I would configure it like so in code:


The PlayFramework documentation section is very sparse and suggests using Akka config properties to tweak the ObjectMapper.

But the Akka jackson documentation also doesn’t mention how to configure the SerializationInclusion property.

The Akka source code for the the JacksonObjectMapperProvider doesn’t appear to expose a way to configure it either.

I tried configuring the ObjectMapper in my app Guice Module like so:


however, it’s not the same instance that controllers see when invoking the play.libs.Json class

One way to do this is using Json.setObjectMapper(<your own configured mapper>) in a module at startup.

You could try getting ahold of the object mapper from the JacksonObjectMapperProvider but you’ll need an Akka actor system to do so.

val system: ActorSystem = // ...
val objectMapper: ObjectMapper = JacksonObjectMapperProvider(system).getOrCreate("jackson-json", None)

If you are using Java this is slightly different from Scala:

JacksonObjectMapperProvider.get(system).getOrCreate("jackson-json", Optional.EMPTY)

I must say though that I’m unsure if Play-Json is using the same object mapper that Akka uses. Play-Json is Jackson under the hood but I don’t know if they are setup to share an ObjectMapper or not.