-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ravenlab/2.0.0
2.0.0 Release
- Loading branch information
Showing
11 changed files
with
257 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/java/com/github/ravenlab/resolver/ClassResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.