Skip to content

Automatic serialization and deserialization of objects using gson

License

Notifications You must be signed in to change notification settings

ravenlab/autogson

Repository files navigation

autogson

build codecov license

A utility for automatic conversion to and from json using gson.

Using

You will need gson in your class path as autogson does not include it.

//Create a gson instance

//To get json
String json = new AutoGson.Builder().build().toJson(gson, object);
	
//From json
FooBar bar = new AutoGson.Builder().build().fromJson(gson, json);

Gradle

repositories {
	maven { url 'https://jitpack.io' }
}

compile 'com.github.ravenlab:autogson:2.0.0'

Maven

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

<dependency>
	<groupId>com.github.ravenlab</groupId>
	<artifactId>autogson</artifactId>
	<version>2.0.0</version>
</dependency>

How

When creating a json string a seperate field in the json with the class's name is created.

When deserializing from json the new field is used to find the class via reflection to create your new object.

Caveats

Since the class name is stored if the class is missing or refactored to a new class path autogson will not be able to deserialize your object.