Skip to content

Commit

Permalink
Merge pull request #2 from ravenlab/2.0.0
Browse files Browse the repository at this point in the history
2.0.0 Release
  • Loading branch information
virustotalop authored Jun 27, 2020
2 parents 426fd9e + d18c284 commit 9cc064c
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 88 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repositories {
maven { url 'https://jitpack.io' }
}
compile 'com.github.ravenlab:autogson:1.0.0'
compile 'com.github.ravenlab:autogson:2.0.0'
```

### Maven
Expand All @@ -44,7 +44,7 @@ compile 'com.github.ravenlab:autogson:1.0.0'
<dependency>
<groupId>com.github.ravenlab</groupId>
<artifactId>autogson</artifactId>
<version>1.0.0</version>
<version>2.0.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = 'com.github.ravenlab'
version = '1.0.0'
version = '2.0.0'

repositories {
mavenCentral()
Expand Down
71 changes: 57 additions & 14 deletions src/main/java/com/github/ravenlab/AutoGson.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,86 @@
package com.github.ravenlab;

import java.util.HashMap;
import java.util.Map;

import com.github.ravenlab.resolver.ClassResolver;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

public final class AutoGson {
public class AutoGson {

public static final String AUTO_GSON_CLASS = "auto_gson_class";

private AutoGson() {}
private Map<String, String> refactored;
private ClassResolver resolver;

public static String AUTO_GSON_CLASS = "auto_gson_class";
private AutoGson(Map<String, String> refactored, ClassLoader loader) {
this.refactored = refactored;
this.resolver = new ClassResolver(loader);
}

public static String toJson(Gson gson, Object obj)
{
public String toJson(Gson gson, Object obj) {
JsonElement element = gson.toJsonTree(obj);
JsonObject jsonObject = element.getAsJsonObject();
String className = obj.getClass().getName();
jsonObject.addProperty(AUTO_GSON_CLASS, className);
String json = gson.toJson(jsonObject);
return json;
}

@SuppressWarnings("unchecked")
public static <T> T fromJson(Gson gson, String json) throws JsonSyntaxException, ClassNotFoundException
{
try
{
public <T> T fromJson(Gson gson, String json) throws JsonSyntaxException{
try {
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
JsonElement classElement = jsonObject.get(AUTO_GSON_CLASS);
String className = classElement.getAsString();
Class<?> clazz = null;
if(this.resolver.classExists(className)) {
clazz = this.resolver.loadClass(className);
} else {
String refactoredClassName = this.refactored.get(className);
if(this.resolver.classExists(refactoredClassName)) {
clazz = this.resolver.loadClass(refactoredClassName);
}
}

if(clazz == null) {
return null;
}

jsonObject.remove(AUTO_GSON_CLASS);
Class<?> clazz = Class.forName(className);
Object fromJsonGeneric = gson.fromJson(jsonObject, clazz);
T fromJson = (T) fromJsonGeneric;
return fromJson;
}
catch(JsonSyntaxException ex)
{
} catch(JsonSyntaxException ex) {
throw(ex);
}
}

public static class Builder {

private Map<String, String> refactored;
private ClassLoader loader;

public Builder() {
this.refactored = new HashMap<>();
this.loader = this.getClass().getClassLoader();
}

public Builder addRefactoredClass(String mapFrom, String mapTo) {
this.refactored.put(mapFrom, mapTo);
return this;
}

public Builder setClassLoader(ClassLoader loader) {
this.loader = loader;
return this;
}

public AutoGson build() {
return new AutoGson(this.refactored, this.loader);
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/github/ravenlab/resolver/ClassResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.ravenlab.resolver;

public class ClassResolver {

private ClassLoader loader;

public ClassResolver(ClassLoader loader) {
this.loader = loader;
}

public Class<?> loadClass(String className) {
if(className == null) {
return null;
}
try {
return this.loader.loadClass(className);
} catch (ClassNotFoundException e) {
return null;
}
}

public boolean classExists(String className) {
return this.loadClass(className) != null;
}
}
161 changes: 110 additions & 51 deletions src/test/java/com/github/ravenlab/autogson/test/AutoGsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,108 +17,167 @@
public class AutoGsonTest {

@Test
public void testToJson()
{
public void testToJson() {
Gson gson = new Gson();
FooBar foo = new FooBar();
String json = AutoGson.toJson(gson, foo);
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, foo);
JsonObject obj = gson.fromJson(json, JsonObject.class);
String autoGsonClass = obj.get(AutoGson.AUTO_GSON_CLASS).getAsString();
assertTrue(autoGsonClass.equals(FooBar.class.getName()));
}

@Test
public void testFromJson()
{
public void testFromJson() {
Gson gson = new Gson();
FooBar orginalFoo = new FooBar();
String json = AutoGson.toJson(gson, orginalFoo);
try
{
FooBar fooBar = AutoGson.fromJson(gson, json);
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, orginalFoo);

try {
FooBar fooBar = autoGson.fromJson(gson, json);
String foo = fooBar.getFoo();
assertTrue(foo.equals("bar"));
}
catch (JsonSyntaxException | ClassNotFoundException e)
{
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
}

@Test(expected = JsonSyntaxException.class)
public void testInvalidJsonDeserialize() throws JsonSyntaxException
{
public void testInvalidJsonDeserialize() throws JsonSyntaxException {
Gson gson = new Gson();
String json = "{foo/thing}";
try
{
AutoGson.fromJson(gson, json);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
fail("Class not found exception was thrown");
}
AutoGson autoGson = new AutoGson.Builder().build();
autoGson.fromJson(gson, json);
}

@Test
public void testInheritance()
{
public void testInheritance() {
FooBar child = new FooBarChild();
Gson gson = new Gson();
String json = AutoGson.toJson(gson, child);
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, child);

try
{
FooBar foo = AutoGson.fromJson(gson, json);
try {
FooBar foo = autoGson.fromJson(gson, json);
assertTrue(foo instanceof FooBarChild);
assertTrue(foo instanceof FooBar);
}
catch (JsonSyntaxException | ClassNotFoundException e)
{
} catch (JsonSyntaxException e) {
e.printStackTrace();
fail("Class not found or an issue with the original json");
}
}

@Test
public void testInnerCustomClass()
{
public void testInnerCustomClass() {
FooBar child = new FooBarChild();
Gson gson = new Gson();
String json = AutoGson.toJson(gson, child);
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, child);

try
{
FooBarChild foo = AutoGson.fromJson(gson, json);
try {
FooBarChild foo = autoGson.fromJson(gson, json);
String data = foo.getData().getData();
assertTrue(data.equals("somedata"));
}
catch (JsonSyntaxException | ClassNotFoundException e)
{
} catch (JsonSyntaxException e) {
e.printStackTrace();
fail("Class not found or an issue with the original json");
}
}

@Test
public void testInnerCustomClassChild()
{
public void testInnerCustomClassChild() {
FooBar child = new FooBarChild();
Gson gson = new Gson();
String json = AutoGson.toJson(gson, child);
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, child);

try
{
FooBarChild foo = AutoGson.fromJson(gson, json);
try {
FooBarChild foo = autoGson.fromJson(gson, json);
FooData<String> fooData = foo.getData();
System.out.println(fooData.getClass().getName());
assertTrue(fooData instanceof ExtraFooData);
}
catch (JsonSyntaxException | ClassNotFoundException e)
{
} catch (JsonSyntaxException e) {
e.printStackTrace();
fail("Class not found or an issue with the original json");
}
}

@Test
public void testRefactoredClass() {
Gson gson = new Gson();
FooBar foo = new FooBar();
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, foo);
JsonObject obj = gson.fromJson(json, JsonObject.class);
String className = obj.get(AutoGson.AUTO_GSON_CLASS).getAsString();
String refactoredClassName = "refactored." + className;
obj.remove(AutoGson.AUTO_GSON_CLASS);
obj.addProperty(AutoGson.AUTO_GSON_CLASS, refactoredClassName);
String newJson = obj.toString();
AutoGson newAutoGson = new AutoGson
.Builder()
.addRefactoredClass(refactoredClassName, className)
.build();
FooBar newFoo = newAutoGson.fromJson(gson, newJson);
assertTrue(obj.get(AutoGson.AUTO_GSON_CLASS).getAsString().equals(refactoredClassName));
assertTrue(newFoo != null);
}

@Test
public void testNoRefactoredClass() {
Gson gson = new Gson();
FooBar foo = new FooBar();
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, foo);
JsonObject obj = gson.fromJson(json, JsonObject.class);
String className = obj.get(AutoGson.AUTO_GSON_CLASS).getAsString();
String refactoredClassName = "refactored." + className;
obj.remove(AutoGson.AUTO_GSON_CLASS);
obj.addProperty(AutoGson.AUTO_GSON_CLASS, refactoredClassName);
String newJson = obj.toString();
AutoGson newAutoGson = new AutoGson
.Builder()
.build();
FooBar newFoo = newAutoGson.fromJson(gson, newJson);
assertTrue(newFoo == null);
}

@Test
public void testDoesNotExistClass() {
Gson gson = new Gson();
FooBar foo = new FooBar();
AutoGson autoGson = new AutoGson.Builder().build();
String json = autoGson.toJson(gson, foo);
JsonObject obj = gson.fromJson(json, JsonObject.class);
String className = obj.get(AutoGson.AUTO_GSON_CLASS).getAsString();
String refactoredClassName = "refactored." + className;
String refactoredDoesNotExistClassName = refactoredClassName + "DoesNotExist";
obj.remove(AutoGson.AUTO_GSON_CLASS);
obj.addProperty(AutoGson.AUTO_GSON_CLASS, refactoredDoesNotExistClassName);
String newJson = obj.toString();
AutoGson newAutoGson = new AutoGson
.Builder()
.addRefactoredClass(refactoredClassName, refactoredDoesNotExistClassName)
.build();
FooBar newFoo = newAutoGson.fromJson(gson, newJson);
assertTrue(newFoo == null);
}

@Test
public void testSetClassLoader() {
Gson gson = new Gson();
FooBar orginalFoo = new FooBar();
AutoGson autoGson = new AutoGson.Builder()
.setClassLoader(this.getClass().getClassLoader())
.build();
String json = autoGson.toJson(gson, orginalFoo);

try {
FooBar fooBar = autoGson.fromJson(gson, json);
String foo = fooBar.getFoo();
assertTrue(foo.equals("bar"));
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit 9cc064c

Please sign in to comment.