5

I have json string represenatation of some object class objects is

public class SMPBBaseObjectsList {

    public ArrayList<Object> data = new ArrayList<>();
    public Integer count;
    public Integer limitFrom;
    public Integer limitTo;
    public Boolean hasMore;
    public String dataItemsClass;
}

And i have json

{"classItem":"smpb.utility.classes.SMPBBaseObjectsList","dataItemsClass":"smpb.base.classes.SMPBUser","dataSliceCode":"012013","data":[{"id":1374046117510970000,"Name":"Test3","classItem":"smpb.base.classes.SMPBUser","dataSliceCode":"012013"}],"filter":{"orderItems":[],"filterItems":[]}}

I try parse this json and create object of my class with next code:

 String json = "{\"classItem\":\"smpb.utility.classes.SMPBBaseObjectsList\",\"dataItemsClass\":\"smpb.base.classes.SMPBUser\",\"dataSliceCode\":\"012013\",\"data\":[{\"id\":1374046117510970000,\"Name\":\"Test3\",\"classItem\":\"smpb.base.classes.SMPBUser\",\"dataSliceCode\":\"012013\"}],\"filter\":{\"orderItems\":[],\"filterItems\":[]}}";
        SMPBBaseObjectsList list = new GsonBuilder().create().fromJson(json, SMPBBaseObjectsList.class);
        System.out.println("BEFORE:" + json);
        System.out.println("AFTER: " + list);

System outputs:

BEFORE:{"classItem":"smpb.utility.classes.SMPBBaseObjectsList","dataItemsClass":"smpb.base.classes.SMPBUser","dataSliceCode":"012013","data":[{"id":1374044905885298000,"Name":"Test3","classItem":"smpb.base.classes.SMPBUser","dataSliceCode":"012013"}],"filter":{"orderItems":[],"filterItems":[]}}

AFTER: {"classItem":"smpb.utility.classes.SMPBBaseObjectsList","dataItemsClass":"smpb.base.classes.SMPBUser","dataSliceCode":"012013","data":[{"Name":"Test3","id":1.374044905885298011E18,"classItem":"smpb.base.classes.SMPBUser","dataSliceCode":"012013"}],"filter":{"orderItems":[],"filterItems":[]}}

As u can see in Json String i have ID with value 1374044905885298000 , but when object serialized from string i got 1.374044905885298011E18

And problem is what this representation of Long lost last zeros 0000 and i got Long 1374044905885297920

Why? and how fix it?

Data in Array is String map, and it's already all Long id Double format.

I try registerAdapater for Long or Double but never triggered.

Version of Gson 2.2.4

UPDATE

It's not duplicate of question

How to prevent Gson from converting a long number (a json string ) to scientific notation format?

Community
  • 1
  • 1
Dmitry Nelepov
  • 7,246
  • 8
  • 53
  • 74
  • possible duplicate of [How to prevent Gson from converting a long number (a json string ) to scientific notation format?](http://stackoverflow.com/questions/11586299/how-to-prevent-gson-from-converting-a-long-number-a-json-string-to-scientific) –  Jul 17 '13 at 08:56
  • It's not. If you read anwsers u will found what it's not soluction of deserialization, it's jus STRING representation. but if u try deserialize JsonElement jsonElement = jsonParser.parse(json); to class u got my error. – Dmitry Nelepov Jul 17 '13 at 09:00
  • To fix it, you can write a custom deserializer class. Take a look here: http://stackoverflow.com/questions/5845822/gson-deserializing-key-value-to-custom-object – gawi Jul 17 '13 at 09:04

1 Answers1

0

I can't tell exactly what the problem is, but you can solve it by creating another class, i.e. Data to use in the List instead of the Object class... I tried this code and it's working fine for me!

So, you need to replace the ArrayList<Object> in your SMPBBaseObjectsList by:

public ArrayList<Data> data = new ArrayList<>()

And create a new class like this:

public class Data {    
    public Long id;
    public String Name;
    public String classItem;
    public String dataSliceCode;
}

I guess there's an issue when parsing the JSON to an Object object, it probably makes some conversion that leads to that number formatting, but unfortunately I'm not an expert in this Java low-level issues...
Anyway, with this code you are explicitly specifying that you want that value parsed into a Long, so there's no problem!

MikO
  • 18,243
  • 12
  • 77
  • 109
  • My probles is that Data is Array of Objects because it's containts different classes – Dmitry Nelepov Jul 18 '13 at 04:52
  • @DmitryNelepov: You can always create the `Data` class containing all the attributes of all the classes the `Array` can contain and it will be able to parse all the possible classes... or you can create a custom deserializer for that part... – MikO Jul 18 '13 at 11:23