-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Produce valid JSON in the presence of NaN or INF values #22
Comments
Erg. I'd classify this as a major issue. I'm gonna do a quick whip-around of low-level javascript experts and see what they think |
The standard approach for dealing with custom values in serialized JSON-like blobs in JavaScript is to use revivers. For example, supposing that Python inserts var str = '{"a":NaN,"b":Infinity,"c":[NaN,Infinity,-Infinity]}'; then you'd create the following reviver function function reviver( key, value ) {
if ( value === '***NaN***' ) {
return NaN;
}
if ( value === '***Infinity***' ) {
return Infinity;
}
if ( value === '***-Infinity***' ) {
return -Infinity;
}
return value;
} which can then be applied as follows: var tmp = str.replace( /(NaN|-?Infinity)/g, '***$1***' );
var obj = JSON.parse( tmp, reviver );
// returns { a: NaN, b: Infinity, c: [ NaN, Infinity, -Infinity ] } thus, behaving as expected. Two caveats:
The other alternative is to use a custom JavaScript JSON parser which can handle Hope this helps. |
One additional addendum: One can avoid the string Doing this work upon serialization, while not obviating the need for a custom |
Thanks @kgryte for the detailed info. I feel like I missed a beat somewhere. If we ignore the possibility of
? Or is this exactly what you were referring to in your addendum? |
Because JSON.parse('{"a":NaN,"b":Infinity}')
// throws SyntaxError: Unexpected token N in JSON at position 5 If the serialization logic happens to encode var str = '{"a":"NaN","b":"Infinity"}'; then, yes, you can simplify the |
I chose to replace with |
This issue should be handled by the work in #41, so I'm closing this |
Currently the backend server extension is using Python's native JSON package to serialize the payload. Python's JSON library will produce invalid JSON in the presence of NaN or Inf values. The JSON library will add values equivalent to Javascript's NaN and Inf values, but unfortunately these values are not considered valid JSON, and we fail on the frontend when parsing the JSON payload. While this could be handled on the frontend, I think it makes more sense to handle this server-side. Unfortunately, I wasn't able to find a solution for this without bringing in a third party dependency. The
simplejson
library provides anignore_nan
flag as input to itsdump
function that encodes NaN values asNone
/null
. While Javascript data rendering and plotting libraries typically prefer null values, Python libraries (e.g, Matplotlib) tend to prefer NaN values, and may produce errors in the presence of null values. Is it likely this extension will ever be called from a non-Javascript context? If not, it might make sense to simply encode NaN values as null by default. However, if it is likely that this extension will be called from a non-Javascript context, we could add an ignoreNan flag to the query parameters. In my PR for n-dim support, I added an implementation of the former. Here's a discussion on this topic:https://stackoverflow.com/questions/15228651/how-to-parse-json-string-containing-nan-in-node-js
The text was updated successfully, but these errors were encountered: