Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to deal with out of range numbers during JSON serialization?

Majdi JaqamanMar 15 2016 — edited Mar 16 2016

Hello,

     I have a JSON string that I'm receiving which contains the following number 52046000000000000000000000000000000000. The JSON serialization using the JSONBeanSerializationHelper or JSONObject leads to the following error:

Caused by: java.lang.ArithmeticException: Overflow

    at java.math.BigDecimal.longValueExact(Unknown Source)

    at org.glassfish.json.JsonNumberImpl.longValueExact(JsonNumberImpl.java:217)

    at oracle.adfmf.json.FastParser.populateJSONArrayFromJsonArray(Unknown Source)

    at oracle.adfmf.json.FastParser.populateJSONObjectFromJsonObject(Unknown Source)

    at oracle.adfmf.json.FastParser.populateJSONObject(Unknown Source)

    ... 2 more

     I understand the error is because the number is out of range for the long type. The scenario I'm trying to fix is the following. I receive an array of JSON objects from a REST call. The array can have thousands of JSON objects. If one of the objects has a number that is out of range, then the JSON serialization fails. This means that somehow I need to manipulate the JSON string that I receive from the web service before parsing. The first thing that comes to mind is to use some regex and do some text substitution to get rid of the troublesome object. This way I would at least only lose the objects with the numbers that are out of range.

     First, I believe that would have a performance hit. Second, a gut feeling tells me that there must be a better way. Maybe there is a way to handle large numbers like that?

     Please let me know if you have any ideas how to approach this.

Regards,

Majdi Jaqaman

Comments
Post Details
Added on Mar 15 2016
1 comment
1,366 views