Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading json file integer values as double values in ObjectTypeAdapter #1351

Closed
prem-jason opened this issue Jul 17, 2018 · 1 comment
Closed

Comments

@prem-jason
Copy link

Hi,

My sample json is as following:
{
"myevents": [1234, 4567]
}

String path = "src//main//resources/sample.json";
FileReader fileReader = new FileReader(path);
JsonReader jsonReader = new JsonReader(fileReader);
Map<String, ArrayList> map = new HashMap<String, ArrayList>();
Gson gson = new Gson();
Map<String, ArrayList> map1 =gson.fromJson(jsonReader, map.getClass());

System.out.println(map1.get("myevents").toString());

Prints: [1234.0, 5678.0]

Reason:
com.google.gson.internal.bind.ObjectTypeAdapter's read method reads jsons numbers as double values, can you introduce integers, floats or double as case types? instead of treating all numbers as double values. Thanks

@OverRide public Object read(JsonReader in) throws IOException {
JsonToken token = in.peek();
switch (token) {
case BEGIN_ARRAY:
List list = new ArrayList();
in.beginArray();
while (in.hasNext()) {
list.add(read(in));
}
in.endArray();
return list;

case BEGIN_OBJECT:
  Map<String, Object> map = new LinkedTreeMap<String, Object>();
  in.beginObject();
  while (in.hasNext()) {
    map.put(in.nextName(), read(in));
  }
  in.endObject();
  return map;

case STRING:
  return in.nextString();

case NUMBER:
  return in.nextDouble();

case BOOLEAN:
  return in.nextBoolean();

case NULL:
  in.nextNull();
  return null;

default:
  throw new IllegalStateException();
}

}

@prem-jason prem-jason changed the title reading json file integers values as double values in ObjectTypeAdapter reading json file integer values as double values in ObjectTypeAdapter Jul 17, 2018
@inder123
Copy link
Collaborator

Changing the types will break backward compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants