Open GoogleCodeExporter opened 9 years ago
Google Gson Version 2.2.4
Original comment by troshkov...@gmail.com
on 12 Feb 2014 at 3:04
I have the same problem, How to treat object as integer first ?
private static class ObjectLongFirstDeserializer
implements JsonDeserializer<Object>
{
@Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx)
throws JsonParseException
{
Object o = null;
try{o = json.getAsLong();} catch (Exception ignored){}
if (o == null)
try{o = json.getAsDouble();} catch (Exception ignored){}
if (o == null)
try{o = json.getAsString();} catch (Exception ignored){}
return o;
}
}
I have a deserializer, But It's not works.
Full Test
public class JsonTest
{
private static class ObjectLongFirstDeserializer
implements JsonDeserializer<Object>
{
@Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx)
throws JsonParseException
{
Object o = null;
try{o = json.getAsLong();} catch (Exception ignored){}
if (o == null)
try{o = json.getAsDouble();} catch (Exception ignored){}
if (o == null)
try{o = json.getAsString();} catch (Exception ignored){}
return o;
}
}
@Test
public void test()
{
SimpleValue value = new SimpleValue();
value.integer = 123;
value.doubleFloat = 123.2;
Gson gson = new GsonBuilder()
.registerTypeAdapter(Object.class, new ObjectLongFirstDeserializer())
.create();
Object o = gson.fromJson(gson.toJson(value), new TypeToken<Map<String, Object>>() {}.getType());
System.out.println(o);
}
public static class SimpleValue
{
Object integer;
Object doubleFloat;
}
}
Original comment by wenerm...@gmail.com
on 25 Sep 2014 at 6:46
BTW, Gson 2.3
Original comment by wenerm...@gmail.com
on 25 Sep 2014 at 6:47
I solved my problem
public class JsonTest
{
private static class IntegerFirstSOMapDeserializer
implements JsonDeserializer<Map<String, Object>>
{
@Override
public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx)
throws JsonParseException
{
Map<String, Object> map = Maps.newLinkedHashMap();
for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet())
{
JsonElement el = entry.getValue();
Object o = null;
try
{
float f = el.getAsFloat();
// not loose precision
if (Math.ceil(f) == f)
o = (int) f;
else o = f;
} catch (Exception ignored) {}
if (o == null)
try {o = el.getAsString();} catch (Exception ignored) {}
map.put(entry.getKey(), o);
}
return map;
}
}
@Test
public void test()
{
Map<String,Object> value = Maps.newHashMap();
value.put("i",123);
value.put("d",123.2f);
Gson gson = new GsonBuilder()
.registerTypeAdapter(new TypeToken<Map<String, Object>>() {}.getType(), new IntegerFirstSOMapDeserializer())
.create();
Map o = gson.fromJson(gson.toJson(value), new TypeToken<Map<String, Object>>() {}.getType());
System.out.println(o);
assert o.get("i").equals(value.get("i"));
assert o.get("d").equals(value.get("d"));
o = gson.fromJson(gson.toJson(value), new TypeToken<Map<String, Float>>() {}.getType());
System.out.println(o);
assert !o.get("i").equals(value.get("i"));
assert o.get("d").equals(value.get("d"));
}
}
Original comment by wenerm...@gmail.com
on 25 Sep 2014 at 7:45
Original issue reported on code.google.com by
troshkov...@gmail.com
on 12 Feb 2014 at 3:00