Comment #0 by lumi.pakkanen — 2015-06-23T18:53:29Z
The JSON specification intentionally leaves out encodings for Infinity and NaN as these are global variables in javascript that can be replaced with something malicious.
std.json currently encodes double.infinity as inf and double.nan as nan. These variables can also be replaced with malicious versions.
The correct encoding for double.infinity, -double.infinity and double.nan is null.
import std.json;
void main()
{
assert(JSONValue(double.infinity).toString == "null");
assert(JSONValue(-double.infinity).toString == "null");
assert(JSONValue(double.nan).toString == "null");
}
Comment #1 by ag0aep6g — 2015-06-23T19:17:24Z
I think this has been fixed recently. Git head phobos throws "std.json.JSONException@std/json.d(1170): Cannot encode Infinity. Consider passing the specialFloatLiterals flag."
Corresponding pull request:
https://github.com/D-Programming-Language/phobos/pull/3141
The mentioned specialFloatLiterals is not set by default. Setting it makes it so that inf/nan get stringified to JSON strings (not null). Are you ok with this?
Comment #2 by lumi.pakkanen — 2015-06-24T07:59:53Z
(In reply to ag0aep6g from comment #1)
> I think this has been fixed recently. Git head phobos throws
> "std.json.JSONException@std/json.d(1170): Cannot encode Infinity. Consider
> passing the specialFloatLiterals flag."
>
> Corresponding pull request:
> https://github.com/D-Programming-Language/phobos/pull/3141
>
> The mentioned specialFloatLiterals is not set by default. Setting it makes
> it so that inf/nan get stringified to JSON strings (not null). Are you ok
> with this?
Those options are nice but I would like to have the third option that works like javascript's JSON.stringify and encodes inf and nan as null.
Comment #3 by robert.schadek — 2024-12-01T16:24:44Z