• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSON Parsing and NaN and Infinity  RSS feed

 
Stephen McConnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have been using JSON4J as a simple JSON parser. It has been working great UNTIL we started getting both NaN and Infinity as numeric values from a data provider. THEY say that those are accepted values in JSON, but I cannot find a Java JSON parser that handles that.

Is anyone aware of one that does so?


Stephen McConnell
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know the answer to your question (not familiar with all the browsers out there), but I would contest the claim that these are accepted values in JSON. A quick look at www.json.org shows how numbers can be constructed -- Nan and Infinity are nowhere to be found. Can you show exactly how they're passing this value in JSON? I mean, what does the JSON text look like when it allegedly represents Infinity or NaN?
 
Stephen McConnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are not dealing with browsers or JavaScript and JSON. If I were parsing JSON with JavaScript, it would accept NaN and Infinity (all except IE and this is the rub as I will explain later).

However, I am using a Java Parser to parse the JSON. As I said previously, I can find no documentation of a Third Party Java JSON parser that handles NaN or Infinity. However, some people INSIST on sending it, even though it is not part of the standard. Getting to the rub (as I discussed before), usually the people are using Microsoft Servers and running ASP.net to create their JSON. So, on one hand Microsoft Insists that those values are part of JSON and on the other hand their JavaScript in their browsers don't support it.... (Catch 22)

here is an example of the data they are passing in JSON:
{
"AverageRestartSpeed": NaN,
"AverageRunningPosition": 12.566037735849056,
"AverageSpeed": 61.764678923555394,
"BestLap": 40,
"BestSpeed": 131.30060544168066,
........... more data here .............
}, ...........

The NaN is a result of them dividing 0/0 and not pre-checking their values. This could happen on any numeric value that they send if they have bad real time information that they process.

If the party sending JSON insists on the possibility of sending it, however, I have to program defensively and support it.
So, back to my previous question....

Does anyone know of a Java Based JSON parser that supports "NaN and Infiinity". I heard Jackson does, but have not found it for certain in their documentation.

Thanks
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen McConnell wrote:We are not dealing with browsers

Sorry, I meant to say parsers, not browsers. I'm not familiar with all the parsers out there. I wasn't thinking about browsers at all. Nor about JavaScript. The rest of my comment was just about what the JSON documentation says, which offers no way to support NaN or Infinity with JSON. Well, unless you render the numbers as strings using quotes I suppose. Which is not what they did, unfortunately.

I know, this doesn't really help you if someone insists on sending not-really-JSON messages and you are being paid to parse the not-really-JSON. You still need a solution. I just wish the people doing it would stop calling what they're doing "JSON", because it isn't.

So anyway, yeah, if anyone else knows of an existing parser that does this, please speak up.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about pre-processing the data to look for NaN etc and provide special handling before giving it to the JSON parser?

Bill
 
Stephen McConnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the spirit of posting a possible solution when the poster finds it, I'm posting this one.

GSON, which Google's API support of JSON serialization and deserialization says they support the NaN, Positive_Infinity and Negative_Infinity double values in their API.

Gson Support of NaN

I'm going to test this and see if it works... If it does, I will post this.

Does anyone have any experience in using GSON API and know how fast this works. (Really it has to be faster than JSON4J or the JSON.org API), their benchmarks are terrible.

 
Stephen McConnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:How about pre-processing the data to look for NaN etc and provide special handling before giving it to the JSON parser?

Bill



Bill. That would defeat the purpose. I'm already running a IOStream filter on the JSON to remove or transform non-standard characters in the JSON Stream (which I get about 1% non-standard un-parsable characters from Microsoft). I suppose I could, but I'd rather not have to write it and would rather have a Parser do it. Since this is a problem with many different JSON feeds (not following standards), someone, somewhere must have found a solution. I think I have found one in GSON. (see above)

Thanks for the idea, though.
 
Stephen McConnell
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I looked at Gson, and it may handle a "NaN" or "Infinity", but it isn't the easiest API to use.

I tested Jackson version 1.7+ and it is very nice, but STILL doesn't handle "NaN" even though they are thinking about it....


So, I went to JSON.org and tested the groupId: org.json artifactId: json version: 20090211 jar out there in Maven.


It does an interesting thing. It just ignores non-numerics without quotes surrounding them and translates them as Strings... assuming that the person generating the JSON may forgotten to put quotes around a String....

Which makes it possible to handle a NaN or Infinity.

All I have to do is for each Numeric value I have in the JSON I'm parsing, have it come back as an Object. Then, test for instanceof (String).

This is a real pain, but until Jackson (which is both fast and beautiful) takes care of this, I have to use the slower JSON.org standard.

Oh well....

We aren't processing HUGE JSON and we have time to process it before the next package comes in...

I might as well do that.

IF ANYONE HAS A BETTER PACKAGE OR SOLUTION, please tell me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!