Created
September 18, 2012 06:17
-
-
Save nlinker/3741536 to your computer and use it in GitHub Desktop.
How to serialize/deserialize json for generic types with Gson
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
import com.google.gson.*; | |
import com.google.gson.reflect.TypeToken; | |
public class ConversionUtils { | |
// both | |
private static Type typeB = new TypeToken<SpellData>() { }.getType(); | |
private static Gson gson; | |
static { | |
//region Serializer/deserializer for compound object | |
JsonSerializer<SpellData> serializerB = | |
new JsonSerializer<SpellData>() { | |
@Override | |
public JsonElement serialize(SpellData src, Type typeOfSrc, | |
JsonSerializationContext context) { | |
JsonObject object = new JsonObject(); | |
object.add("vars", getVariationsAsJson(src.getVariations())); | |
object.add("conc", getConcatenationsAsJson(src.getConcatenations())); | |
return object; | |
} | |
private JsonElement getVariationsAsJson(Map<String, List<SpellVariation>> src) { | |
JsonObject alts = new JsonObject(); | |
for (String key : src.keySet()) { | |
JsonArray array = new JsonArray(); | |
for (SpellVariation var : src.get(key)) { | |
JsonArray tuple = new JsonArray(); | |
tuple.add(new JsonPrimitive(var.getOriginalWord())); | |
tuple.add(new JsonPrimitive(var.getSimilarWord())); | |
tuple.add(new JsonPrimitive(var.getType().name())); | |
tuple.add(new JsonPrimitive(var.getEditDistance())); | |
tuple.add(new JsonPrimitive(var.getFrequency())); | |
tuple.add(new JsonPrimitive(var.getBoost())); | |
array.add(tuple); | |
} | |
alts.add(key, array); | |
} | |
return alts; | |
} | |
private JsonElement getConcatenationsAsJson(List<List<Token>> src) { | |
JsonArray ja0 = new JsonArray(); | |
for (List<Token> tokens : src) { | |
JsonArray ja1 = new JsonArray(); | |
for (Token token : tokens) { | |
JsonObject jo = new JsonObject(); | |
String tokenText = new String(token.buffer(), 0, token.length()); | |
jo.add("t", new JsonPrimitive(tokenText)); | |
jo.add("f", new JsonPrimitive(token.getFlags())); | |
ja1.add(jo); | |
} | |
ja0.add(ja1); | |
} | |
return ja0; | |
} | |
}; | |
JsonDeserializer<SpellData> deserializerB = | |
new JsonDeserializer<SpellData>() { | |
@Override | |
public SpellData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { | |
return new SpellData( | |
getConcatenationsAsPojo(((JsonObject) json).get("conc")), | |
getVariationsAsPojo(((JsonObject) json).get("vars"))); | |
} | |
private List<List<Token>> getConcatenationsAsPojo(JsonElement json) { | |
JsonArray ja0 = (JsonArray) json; | |
List<List<Token>> list = new ArrayList<List<Token>>(ja0.size()); | |
for (JsonElement je0 : ja0) { | |
JsonArray ja1 = (JsonArray) je0; | |
List<Token> tokens = new ArrayList<Token>(ja1.size()); | |
for (JsonElement je1 : ja1) { | |
JsonObject jo = (JsonObject) je1; | |
String text = jo.get("t").getAsString(); | |
int flags = jo.get("f").getAsInt(); | |
Token token = new Token(text, 0, text.length()); | |
token.setFlags(flags); | |
tokens.add(token); | |
} | |
list.add(tokens); | |
} | |
return list; | |
} | |
private Map<String, List<SpellVariation>> getVariationsAsPojo(JsonElement json) { | |
Map<String, List<SpellVariation>> map = new HashMap<String, List<SpellVariation>>(); | |
JsonObject obj = (JsonObject) json; | |
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { | |
String key = entry.getKey(); | |
JsonArray list = (JsonArray) entry.getValue(); | |
List<SpellVariation> aws = new ArrayList<SpellVariation>(list.size()); | |
for (JsonElement quadElement : list) { | |
JsonArray tuple = (JsonArray) quadElement; | |
SpellVariation var = new SpellVariation( | |
tuple.get(0).getAsString(), | |
tuple.get(1).getAsString(), SpellVariationType.valueOf( | |
tuple.get(2).getAsString()), | |
tuple.get(3).getAsInt(), | |
tuple.get(4).getAsInt(), | |
tuple.get(5).getAsDouble()); | |
aws.add(var); | |
} | |
map.put(key, aws); | |
} | |
return map; | |
} | |
}; | |
//endregion | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
gsonBuilder.registerTypeAdapter(typeB, serializerB); | |
gsonBuilder.registerTypeAdapter(typeB, deserializerB); | |
gson = gsonBuilder.create(); | |
} | |
private ConversionUtils() { | |
} | |
public static String convertSpellData2Json(SpellData spellData) { | |
return gson.toJson(spellData, typeB); | |
} | |
public static SpellData convertJson2SpellData(String serialized) { | |
return gson.fromJson(serialized, typeB); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment