Customise Jackson ObjectMapper in Lagom

434 views
Skip to first unread message

jason...@skiddoo.com.au

unread,
Mar 21, 2017, 8:51:27 PM3/21/17
to Lagom Framework Users
Hi there

I wonder if it's possible to customise the Jackson ObjectMapper used by Lagom to have our additional settings.

Specifically what we want to do right now is to set SerializationFeature.WRITE_DATES_AS_TIMESTAMPS to false so that LocalDate is not serialised as number array.

Regards
Jason Huang

Ignasi Marimon-Clos i Sunyol

unread,
Mar 22, 2017, 8:23:33 AM3/22/17
to jason...@skiddoo.com.au, Lagom Framework Users
Hi Jason,

Lagom bundles a Jackson serializer which includes (amongst others) the com.fasterxml.jackson.datatype.jdk8.Jdk8Module and com.fasterxml.jackson.datatype.jsr310.JavaTimeModule but accessing the ObjectMapper is not possible at the moment.

You can implement your own SerializationFactory and set it up on your Service.descriptor. For example, you can extend the Lagom provided JacksonSerializerFactory as in:

public class MyFactory extends JacksonSerializerFactory {
    public MyFactory() {
        super(new ObjectMapper());
    }
}

And have your own ObjectMapper with the setup you prefer.

To achiece that your xyz-api where that factory will be implemented will have to depend on lagomJavadslJackson. In build sbt:

libraryDependencies ++= Seq(
       .....,
      lagomJavadslJackson
    )

Regards,



--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/92dffbdc-9fa2-4620-a763-b073d4f34b43%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ignasi Marimon-Clos
Software Developer @ Lagom

Andres March

unread,
Mar 22, 2017, 3:40:43 PM3/22/17
to Lagom Framework Users, jason...@skiddoo.com.au
We were able to accomplish this with a guice module:

play.modules.enabled += com.my.MyJacksonModule
play.modules.disabled += play.core.ObjectMapperModule

lagom.serialization.json.jackson-modules += com.fasterxml.jackson.datatype.joda.JodaModule
lagom.serialization.json.jackson-modules +=  com.fasterxml.jackson.module.afterburner.AfterburnerModule
lagom.serialization.json.jackson-modules += com.my.MyJacksonModule

then MyJacksonModule accesses the object mapper to modify it:

public class MyJacksonModule extends SimpleModule {

  @Override
  public void setupModule(SetupContext setupContext) {
    ObjectMapper mapper = setupContext.getOwner();

    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
    mapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
    mapper.setNodeFactory(mapper.getNodeFactory().withExactBigDecimals(true));

    mapper.addMixIn(BigDecimal.class, Double.class);
    mapper.addMixIn(Double.class, BigDecimal.class);

  }
}



On Wednesday, March 22, 2017 at 8:23:33 AM UTC-4, Ignasi Marimon-Clos i Sunyol wrote:
Hi Jason,

Lagom bundles a Jackson serializer which includes (amongst others) the com.fasterxml.jackson.datatype.jdk8.Jdk8Module and com.fasterxml.jackson.datatype.jsr310.JavaTimeModule but accessing the ObjectMapper is not possible at the moment.

You can implement your own SerializationFactory and set it up on your Service.descriptor. For example, you can extend the Lagom provided JacksonSerializerFactory as in:

public class MyFactory extends JacksonSerializerFactory {
    public MyFactory() {
        super(new ObjectMapper());
    }
}

And have your own ObjectMapper with the setup you prefer.

To achiece that your xyz-api where that factory will be implemented will have to depend on lagomJavadslJackson. In build sbt:

libraryDependencies ++= Seq(
       .....,
      lagomJavadslJackson
    )

Regards,


On Wed, Mar 22, 2017 at 1:51 AM, <jason...@skiddoo.com.au> wrote:
Hi there

I wonder if it's possible to customise the Jackson ObjectMapper used by Lagom to have our additional settings.

Specifically what we want to do right now is to set SerializationFeature.WRITE_DATES_AS_TIMESTAMPS to false so that LocalDate is not serialised as number array.

Regards
Jason Huang

--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framewo...@googlegroups.com.
To post to this group, send email to lagom-f...@googlegroups.com.

jason...@skiddoo.com.au

unread,
Mar 22, 2017, 8:24:09 PM3/22/17
to Lagom Framework Users, jason...@skiddoo.com.au
Thanks a lot, Ignasi and Andres. They are good solutions. Much appreciated.
Reply all
Reply to author
Forward
0 new messages