I\'ve got integers in my json, and I do not want gson to convert them to doubles. The following does not work:
@Test
public void keepsIntsAsIs(){
String
This works fine for me:
private static class DoubleSerializer implements JsonSerializer {
@Override
public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
return src == src.longValue() ? new JsonPrimitive(src.longValue()) : new JsonPrimitive(src);
}
}
Gson gson = new GsonBuilder().registerTypeAdapter(Double.class, new DoubleSerializer()).setPrettyPrinting().create();