-
-
Notifications
You must be signed in to change notification settings - Fork 882
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 Infinity creates invalid JSON #2482
Comments
Setting the options object to |
Hi @MatthiasMann thanks for raising this. It does indeed seem wrong to me that
So the correct fix (though I would need to consult on this more to be sure) would be to convert to |
Hmm you are right - this is very annoying. As I only have very few elements which need to handle -Infinity I just replaced these with my own function and template literals for this object: function peak_to_JSON(n: number): string {
return (n > -190) ? n.toString() : "\"-Infinity\"";
}
function peak_to_JSON_nullable(n: number | null): string {
return (n === null) ? "null" : peak_to_JSON(n);
}```
let serializer = (data:LoudnessMeasurement) => `{\"m\":${peak_to_JSON_nullable(data.momentary)},\
\"s\":${peak_to_JSON_nullable(data.short_term)},\
\"g\":${peak_to_JSON_nullable(data.gated)},\
\"g_ms\":${data.gated_recorded.ms()}}`; (this also avoid to convert it to another object to change the key names) |
Yeah, that should also mean when we fix this behaviour to return the standardised |
@epoberezkin not a high priority but I wanted to flag to you that I do think this one is a legit bug. The serializer shouldn't be returning invalid JSON strings and therefore |
What version of Ajv are you using? Does the issue happen if you use the latest version?
8.17.1
Ajv options object
{}
Test case:
Result:
{"m":-Infinity,"s":-12,"g":null,"g_seconds":420.69}
This JSON is invalid and should instead be:
{"m":"-Infinity","s":-12,"g":null,"g_seconds":420.69}
The text was updated successfully, but these errors were encountered: