I'm using GSON to serialise some object graphs to JSON. These objects graphs use Joda Time entities (DateTime
, LocalTime
etc).
The top Google hit for "gson joda" is this page:
It provides source for a type adapter for org.joda.time.DateTime
. This link is also what is referenced in the GSON User Guide.
I expected to find a pre-rolled library that included joda-time serialisers that I could reference as a Maven dependency - but I can't find one.
Is there one? Or am I forced to replicate that snippet in my own project?
I've decided to roll my own open source one - you can find it here:
Here's the Maven details (check central for the latest version):
<dependency>
<groupId>com.fatboyindustrial.gson-jodatime-serialisers</groupId>
<artifactId>gson-jodatime-serialisers</artifactId>
<version>1.6.0</version>
</dependency>
And here's a quick example of how you drive it:
Gson gson = Converters.registerDateTime(new GsonBuilder()).create();
SomeContainerObject original = new SomeContainerObject(new DateTime());
String json = gson.toJson(original);
SomeContainerObject reconstituted = gson.fromJson(json, SomeContainerObject.class);