First off, this is a great library since it follows the JSON spec. I tried, XStream, but did not like the extra properties it places there and couples the JSON strings to the server side technology.
Having said that, I have a Bean which has List<Long>. When I deserialize the JSON String to my Java object, it populates the ArrayList<Long> with Integers. So, where i loop through the List<Long> it throws a class cast exception. Here is some code:
Bean:
private long senderId;
private List<Long> chatRequestIdsFromUser = new ArrayList<Long>();
private List<Long> sessionIds = new ArrayList<Long>();
Hi mansari,
the problem lies in that json-lib-xxx-jdk15 doesn't handle generics yet and that numbers are always transformed into a smaller type if possible. Adding generics information when deserializing will solve this problem. You may want to add a Feature Request to keep track of it =)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the response. It would make this great API even greater if the generics handling is implemented. I will go ahead and add a feature request. Thanks again, great job!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have this same exact situation as well. I'll go look for the feature request and see if if there is a vote I can cast for it.
Meanwhile, a question about how to handle the situation in the interim, please. I could simply let it create a List containing integers and then post-process it into the Long values I need in my bean, but is it a correct assumption that precision may be lost during the json toBean de-serialization, or do I interpret your response above to mean that if the lib sees a number that is too large to be an Integer it will create a Long?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Precisely. Json-lib will always try to use the lowest type if possible. If a numeric value is greater than Integer.MAX_VALUE then Json-lib will use a Long number, if it is bigger than Long.MAX_VALUE then it will be a BigInteger.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
First off, this is a great library since it follows the JSON spec. I tried, XStream, but did not like the extra properties it places there and couples the JSON strings to the server side technology.
Having said that, I have a Bean which has List<Long>. When I deserialize the JSON String to my Java object, it populates the ArrayList<Long> with Integers. So, where i loop through the List<Long> it throws a class cast exception. Here is some code:
Bean:
private long senderId;
private List<Long> chatRequestIdsFromUser = new ArrayList<Long>();
private List<Long> sessionIds = new ArrayList<Long>();
Sample JSON:
String json = "{\"senderId\":12,\"chatRequestIdsFromUser\":[],\"sessionIds\":[1955]}";
for (Long id: bean.getSessionIds()){
System.out.println ("id: "+ id); <-- ClassCastException: java.lang.Integer found
}
Any help here would be appreciated.
Hi mansari,
the problem lies in that json-lib-xxx-jdk15 doesn't handle generics yet and that numbers are always transformed into a smaller type if possible. Adding generics information when deserializing will solve this problem. You may want to add a Feature Request to keep track of it =)
Hi,
Thanks for the response. It would make this great API even greater if the generics handling is implemented. I will go ahead and add a feature request. Thanks again, great job!
I have this same exact situation as well. I'll go look for the feature request and see if if there is a vote I can cast for it.
Meanwhile, a question about how to handle the situation in the interim, please. I could simply let it create a List containing integers and then post-process it into the Long values I need in my bean, but is it a correct assumption that precision may be lost during the json toBean de-serialization, or do I interpret your response above to mean that if the lib sees a number that is too large to be an Integer it will create a Long?
Precisely. Json-lib will always try to use the lowest type if possible. If a numeric value is greater than Integer.MAX_VALUE then Json-lib will use a Long number, if it is bigger than Long.MAX_VALUE then it will be a BigInteger.