I have json object with arbitary values inside. And I want to deserialize it in a Map. Everything is ok except converting integers to a doubles. See example:
Here is my example, the first part is the definition of the class that has an int type field.
import com.google.api.client.util.Key;
public class Folder {
public static final String FIELD_NAME_CHILD_COUNT = "childCount";
@Key(FIELD_NAME_CHILD_COUNT)
public final int childCount;
public Folder(int aChildCount) {
childCount = aChildCount;
}
}
Then the TypeAdapter to convert the number type in Gson to a Java object.
GsonBuilder gsb = new GsonBuilder();
gsb.registerTypeAdapter(Folder.class, new JsonDeserializer() {
@Override
public Folder deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
int value = json.getAsJsonObject().get("childCount").getAsJsonPrimitive().getAsInt();
return new Folder(value);
}
}
);
The third part is the test data, and it works.
String gsonData = new String("\"folder\":{\"childCount\":0}");