Skip to content

Instantly share code, notes, and snippets.

@jarcode-foss
Created August 1, 2015 04:29
Show Gist options
  • Save jarcode-foss/0249f92477d51c1070e9 to your computer and use it in GitHub Desktop.
Save jarcode-foss/0249f92477d51c1070e9 to your computer and use it in GitHub Desktop.
gson = new GsonBuilder()
.registerTypeAdapter(ConsoleMeta.class, new JsonSerializer<ConsoleMeta>() {
@Override
public JsonElement serialize(ConsoleMeta consoleMeta, Type type,
JsonSerializationContext jsonSerializationContext) {
JsonObject object = new JsonObject();
object.add("location",
locationTypeAdapter.serialize(consoleMeta.location, null, jsonSerializationContext));
object.add("face", new JsonPrimitive(consoleMeta.face.name()));
object.add("w", new JsonPrimitive(consoleMeta.w));
object.add("h", new JsonPrimitive(consoleMeta.h));
return object;
}
})
.registerTypeAdapter(ConsoleMeta.class, new JsonDeserializer<ConsoleMeta>() {
@Override
public ConsoleMeta deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject object = jsonElement.getAsJsonObject();
Location location = locationTypeAdapter.deserialize(object.get("location"),
null, jsonDeserializationContext);
BlockFace face = BlockFace.valueOf(object.get("face").getAsString());
int w = object.get("w").getAsInt();
int h = object.get("h").getAsInt();
return new ConsoleMeta(location, face, w, h);
}
})
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.create();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment