Closed GoogleCodeExporter closed 9 years ago
This solution posted here worked for me:
https://groups.google.com/forum/#!msg/google-gson/kNf5HADtY14/bdD1xmDPbH0J
I created a new class as follows:
public class IntTypeAdapter extends TypeAdapter<Number> {
@Override
public void write(JsonWriter out, Number value)
throws IOException {
out.value(value);
}
@Override
public Number read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
try {
String result = in.nextString();
if ("".equals(result)) {
return null;
}
return Integer.parseInt(result);
} catch (NumberFormatException e) {
throw new JsonSyntaxException(e);
}
}
}
And then ran the gson parsing as follows:
Gson gson = new GsonBuilder()
.registerTypeAdapter(int.class, new IntTypeAdapter())
.registerTypeAdapter(Integer.class, new IntTypeAdapter()).create();
myObject = gson.fromJson(myJsonString, Student.class);
Original comment by graili...@gmail.com
on 25 Sep 2012 at 7:08
Working as intended.
Original comment by limpbizkit
on 4 Feb 2013 at 4:09
Original issue reported on code.google.com by
bryan.am...@gmail.com
on 17 Sep 2012 at 4:00