Tod Commons Jackson is available on Maven Central. To use it, put this in the dependencies section of your pom.xml
:
<dependency>
<groupId>eu.trentorise.opendata</groupId>
<artifactId>tod-commons-jackson</artifactId>
<version>1.1.0</version>
</dependency>
In case updates are available, version numbers follows semantic versioning rules.
Tod Commons Jackson allows serializing and deserializing Dict
and LocalizedString
in Jackson 2.x by installing the TodCommonsModule
in a Jackson ObjectMapper
. Also, some basic utility to work with Jackson is provided in the Jacksonizer
class.
You can register TodCommonsModule
in your own Jackson ObjectMapper:
ObjectMapper om = new ObjectMapper();
om.registerModule(new GuavaModule());
om.registerModule(new TodCommonsModule());
String json = om.writeValueAsString(LocalizedString.of(Locale.ITALIAN, "ciao"));
LocalizedString reconstructedLocalizedString = om.readValue(json, LocalizedString.class);
Notice we have also registered the necessary Guava (for immutable collections) and Tod Commons modules (for Dict
and LocalizedString
).
To register everything in one command just write:
ObjectMapper om = new ObjectMapper();
TodCommonsModule.registerModulesInto(om);
ObjectMapper om = new ObjectMapper();
TodCommonsModule.registerModulesInto(om);
String json = om.writeValueAsString(LocalizedString.of(Locale.ITALIAN, "ciao"));
LocalizedString reconstructedLocalizedString = om.readValue(json, LocalizedString.class);
Tod Commons Jackson uses native Java logging system (JUL). If you also use JUL in your application and want to see Tod commons jackson logs, you can take inspiration from tod-commons test logging properties. If you have an application which uses SLF4J logging system, you can route logging with JUL to SLF4J bridge, just remember to programmatically install it first.