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
You have to use public T fromJson(JsonElement json, Type typeOfT)
public void keepsIntsAsIs(){
String json="[{\"id\":1,\"quantity\":2},{\"id\":3,\"quantity\":4}]";
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Type objectListType = new TypeToken>>(){}.getType();
List
Output:
{id=1, quantity=2}
{id=3, quantity=4}
[EDIT]
If not all fields are integers then one way to resolve this is to map the json to an object and define a deserializer for that object.
Below is the example.
I am mapping json to IdQuantityName
and IdQuantityDeserializer
is the json deserializer.
package com.foo;
import java.lang.reflect.Type;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
public class TestGSON {
public void keepsIntsAsIs(){
String json="[{\"id\":1,\"quantity\":2,\"name\":\"apple\"},{\"id\":3,\"quantity\":4,\"name\":\"orange\"}]";
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeHierarchyAdapter(IdQuantityName.class, new IdQuantityDeserializer());
gsonBuilder.registerTypeAdapter(IdQuantityName.class, new IdQuantityDeserializer());
Gson gson = gsonBuilder.create();
Type objectListType = new TypeToken>(){}.getType();
List l = gson.fromJson(json,objectListType);
for (IdQuantityName idQuantityName : l) {
System.out.println(idQuantityName);
}
}
class IdQuantityName{
private int id;
private Object quantity;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Object getQuantity() {
return quantity;
}
public void setQuantity(Object quantity) {
this.quantity = quantity;
}
public Object getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "IdQuantityName [id=" + id + ", quantity=" + quantity
+ ", name=" + name + "]";
}
}
private class IdQuantityDeserializer implements JsonDeserializer{
@Override
public IdQuantityName deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jo = json.getAsJsonObject();
IdQuantityName idq = new IdQuantityName();
idq.setId(jo.get("id").getAsInt());
idq.setName(jo.get("name").getAsString());
JsonElement jsonElement = jo.get("quantity");
if(jsonElement instanceof JsonPrimitive){
if(((JsonPrimitive) jsonElement).isNumber()){
idq.setQuantity(jsonElement.getAsInt());
};
}
return idq;
}
}
public static void main(String[] args) {
new TestGSON().keepsIntsAsIs();
}
}