Skip to content

Instantly share code, notes, and snippets.

@bearprada
Created June 14, 2015 03:39
Show Gist options
  • Select an option

  • Save bearprada/0cbcdf39c70f63eaca3f to your computer and use it in GitHub Desktop.

Select an option

Save bearprada/0cbcdf39c70f63eaca3f to your computer and use it in GitHub Desktop.

Revisions

  1. bearprada created this gist Jun 14, 2015.
    25 changes: 25 additions & 0 deletions sample_gson_deserializer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    /**
    * Example :
    * input : {server: { result : { data: { a:1, b:2 }} }}
    **/

    public class MyDeserializer implements JsonDeserializer<MyClass> {
    public MyClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
    throws JsonParseException {
    JsonElement j = json.getAsJsonObject().get("server").getAsJsonObject().get("result").getAsJsonObject().get("data");
    MyClass clz = new MyClass();
    clz.data = context.deserialize(j, MyData.class);
    return clz;
    }
    }

    public class MyClass {
    public MyData data;
    }

    public class MyData {
    @Serialize("a")
    public int a;
    @Serialize("b")
    public int b;
    }