exceeding LONG.MAX_VALUE in JSON Simple parser

≡放荡痞女 提交于 2019-12-14 02:48:14

问题


I am currently using the JSON Simple library for parsing JSON in my application. I have run into an issue while parsing that is throwing the following error:

java.lang.NumberFormatException: For input string: "107701583138930170002"

I have narrowed this down to what I believe is the issue, the number is to big to be a LONG type. The problem is that I do not have control over this number otherwise I would make it a string. Is there a way to make JSON Simple parse this into a BigInt instead of LONG?


回答1:


The json-simple package downloadable from Google Code doesn't seem to have been updated since 2009. If you look on github there was a commit in Nov 2011 that adds support for numbers outside the Long range.

You will probably have to get the source from github and build a more current version of the library. Looks like json-simple is not being maintained.




回答2:


If you are using Json Simple's streaming parser, you can actually use your json simple ContentHandler with jackson as well with relatively little trouble. I did this for my jsonj project: https://github.com/jillesvangurp/jsonj/blob/master/src/main/java/com/github/jsonj/tools/JsonParserNg.java which is an alternative to the JsonParser.java in the same package that uses jackson instead of jsonsimple to drive the json simple handler.

The jackson parser supports BigInteger. The key method in the class I linked above simply calls the appropriate jsonsimple handler method in a switch statement. Where I call parser.getLongValue(), you can instead call parser.getBigIntegerValue()



来源:https://stackoverflow.com/questions/24064028/exceeding-long-max-value-in-json-simple-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!