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

@json.stringify prints NaN for NaN double value #1235

Open
tonyfettes opened this issue Nov 22, 2024 · 3 comments · May be fixed by #1240
Open

@json.stringify prints NaN for NaN double value #1235

tonyfettes opened this issue Nov 22, 2024 · 3 comments · May be fixed by #1240

Comments

@tonyfettes
Copy link
Contributor

tonyfettes commented Nov 22, 2024

Example to reproduce:

fn main {
  let a : Json = Number((-1.2).sqrt())
  println(@json.stringify(a)) // NaN
}

Some other programming languages:

Language Code Output
JavaScript JSON.stringify(NaN) null
Python json.dumps(float('nan')) NaN
C (json-c) puts(json_object_to_json_string(json_object_new_double(NAN))) NaN
Rust serde_json::Value::Number(serde_json::Number::from_f64(f64::NAN).unwrap()) panicked
@Lampese Lampese added bug Something isn't working to be discussed enhancement and removed bug Something isn't working labels Nov 23, 2024
@hackwaly
Copy link
Contributor

NaN is not a valid JSON value. null is right.

@tonyfettes
Copy link
Contributor Author

Relavent cases:

  • Infinity
  • -Infinity

@tonyfettes
Copy link
Contributor Author

tonyfettes commented Nov 26, 2024

On second thought this might be a backfire for scientific applications: they can't use JSON for RPC and storage simply because we won't emit/parse NaN and +-Inf.

Maybe we should use an option for it?

Edit: to support usage mentioned above, we need parsing support as well.

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

Successfully merging a pull request may close this issue.

3 participants