2.4.x Best Practice for customizing Json ObjectMapper

439 views
Skip to first unread message

Michael Dop

unread,
May 28, 2015, 2:26:05 PM5/28/15
to play-fr...@googlegroups.com
We use this in Play 2.3.x

@Override
public void onStart(Application app) {
// Setup the ObjectMapper to support JodaTime (Un)marshalling
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
Json.setObjectMapper(mapper);

}

Would the best practice for doing this in Play 2.4.x using DI.  Would it be to create a new Class to perform this in the Constructor and then configure DI? 

@Singleton
public class CustomJsonMapper {

protected CustomJsonMapper() {
// Setup the ObjectMapper to support JodaTime (Un)marshalling
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
Json.setObjectMapper(mapper);
}
}


public class CommonModule extends AbstractModule {
protected void configure() {
bind(CustomJsonMapper.class).asEagerSingleton();
}
}

application.conf -> play.modules.enabled += "common.CommonModule"

James Roper

unread,
May 28, 2015, 11:03:26 PM5/28/15
to play-framework
What you have there is perfect.

Note, you may be feeling that something isn't quite right (hence why you're asking the question here), why isn't the object mapper itself dependency injected?  The answer - we haven't yet removed all the global state from Play, as this would be a big breaking change.  We will be addressing this in Play 3.

Cheers,

James

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/2f22071a-9375-4ce4-bf73-f9d41ab817ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
James Roper
Software Engineer

Typesafe – Build reactive apps!
Twitter: @jroper
Reply all
Reply to author
Forward
0 new messages