Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing NaN results in non-JSON compliant text #1

Closed
DrFriedParts opened this issue Feb 23, 2012 · 7 comments
Closed

Serializing NaN results in non-JSON compliant text #1

DrFriedParts opened this issue Feb 23, 2012 · 7 comments

Comments

@DrFriedParts
Copy link

When serializing numeric properties of classes that, for example, divide by zero, the result is NaN, breaking the JSON output.

{ "InvalidCalculatedProperty" : NaN }

I'd recommend switching to string type in this case:

{ "InvalidCalculatedProperty" : "NaN" }

...or, perhaps, throwing an exception.

Anything else, but the current output isn't valid.

Thank you for this amazing library. Keep up the great work. Cheers! -Jonathan

@JamesNK
Copy link
Owner

JamesNK commented Mar 12, 2012

Write a JsonConverter for float/double to make NaN safe if this is important for you.

@JamesNK JamesNK closed this as completed Mar 12, 2012
@brugidou
Copy link

The problem is that NaN (and infinity) values in float/double are not valid JSON values. Deserializing the JSON from any other application will simply not work... can we reopen this issue?

@warappa
Copy link

warappa commented Dec 10, 2012

+1

@JamesNK: Shouldn't Newtonsoft.Json try to be fully compatible to http://tools.ietf.org/html/rfc4627 as defined by D. Crockford which means no "NaN" or "Infinite"?

"Write a JsonConverter for float/double to make NaN safe if this is important for you."
I would say it should (must?) be the other way round: Write a converter if you want to distinguish between null, NaN and Infinite.

@jonira
Copy link

jonira commented Feb 4, 2013

+1 NaN-values really should comply with the rfc.

@collinsauve
Copy link

+1 when a json serializer outputs invalid json - definitely a bug

@collinsauve
Copy link

For posterity, here's a very simple example of a JsonConverter for doubles that JamesNK was referring to: https://gist.github.com/collinsauve/6122751

@JamesNK
Copy link
Owner

JamesNK commented Jul 31, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants