acebaggins/guava-gson-serializers


Expanded serializers for Gson

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.acebaggins:gson-serializers:1.0.1'
	}
	dependencies {
		implementation("com.github.acebaggins:gson-serializers:1.0.1")
	}
	<dependency>
	    <groupId>com.github.acebaggins</groupId>
	    <artifactId>gson-serializers</artifactId>
	    <version>1.0.1</version>
	</dependency>

                            
    libraryDependencies += "com.github.acebaggins" % "gson-serializers" % "1.0.1"
        
        

                            
    :dependencies [[com.github.acebaggins/gson-serializers "1.0.1"]]
        
        

Readme


guava-gson-serializers

Build Status Coverage Status Apache 2.0

Expanded Gson serialization for Guava ImmutableCollections, ImmutableMaps, and Optionals.

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.

Why?

Re-writing serializers is tedious!

How?

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();
}

Optionals

JDK (deprecated)

Gson gson = new GsonBuilder().registerTypeAdapterFactory(OptionalTypeFactory.forJDK()).create();

Guava

Gson gson = new GsonBuilder().registerTypeAdapterFactory(OptionalTypeFactory.forGuava()).create();

What?

Currently supported
  • ImmutableList
  • ImmutableSet
  • ImmutableSortedSet
  • ImmutableBiMap
  • ImmutableMap
  • ImmutableBiMap
  • ImmutableSortedMap
  • Optionals (java.util and guava)

Future support (maybe)

  • ImmutableList/SetMultimap
  • Whatever anyone asks me to do

Where?

Github packages registry! https://maven.pkg.github.com/acebaggins/guava-gson-serializers

Who?

Built by me for the wonderful Guava and Gson libraries.