I originally included a TypeFactory for both Optional (java.util and Guava) but that was a long time ago and I think
most people have switched over to java.util.Optional
.
There are a lot of libraries that handle serialization of the Java 8 classes (Optional, Instant, etc) so I'll
deprecate java.util.Optional
factory and keep the original Guava factory and keep this library more Guava-centric.
Re-writing serializers is tedious!
A few ways.
For a specific class..
Gson gson = new GsonBuilder().registerTypeAdapter(ImmutableList.class, new ImmutableListDeserializer()).create();
or, if the mood grabs you.. (but really don't do this)
Gson gson = new GsonBuilder().registerTypeAdapter(List.class, new ImmutableListDeserializer()).create();
or, the easiest way
public Gson getGson() {
return ImmutableTypeAdapters.withImmutableCollectionSerializers(new GsonBuilder())
.registerTypeAdapterFactory(OptionalTypeFactory.forJDK()) // you probably dont need this
.create();
}
Gson gson = new GsonBuilder().registerTypeAdapterFactory(OptionalTypeFactory.forJDK()).create();
Gson gson = new GsonBuilder().registerTypeAdapterFactory(OptionalTypeFactory.forGuava()).create();
- ImmutableList
- ImmutableSet
- ImmutableSortedSet
- ImmutableBiMap
- ImmutableMap
- ImmutableBiMap
- ImmutableSortedMap
- Optionals (java.util and guava)
- ImmutableList/SetMultimap
- Whatever anyone asks me to do
Github packages registry!
https://maven.pkg.github.com/acebaggins/guava-gson-serializers
Built by me for the wonderful Guava and Gson libraries.