Page 1 of 1
[0.17.69] table_to_json outputs inf, which is invalid json
Posted: Wed Oct 02, 2019 2:59 am
by Boodals
The following command will output invalid json:
Code: Select all
/c game.print(game.table_to_json({math.huge}))
Will output:
According to some quick googling, json doesn't support infinity, negative infinity, nor nan.
Possible solutions:
Error if it encounters any of these values, but that would mean we'd have to verify that there are none of these ourselves.
Output it as a string "infinity", "-infinity", "nan". I think I prefer this solution.
Output infinity as a really big value. Not sure what you'd do for nan though.
Ignore the problem entirely and force us to either: use/make a json parser that supports this, or handle these cases ourselves before exporting. If you go with this, you should probably add a note to the API doc stating this behavior.
thx
Re: [0.17.69] table_to_json outputs inf, which is invalid json
Posted: Wed Nov 06, 2019 6:26 pm
by TruePikachu
JSON also doesn't support negative zero (in that there's no guarantee a negative zero in the JSON will be read successfully as negative zero), which I believe is the only other "special" case (aside from your listed NaN/+∞/-∞ cases) in IEEE754 but not in JSON. Regardless, many standard JSON readers will not successfully read infinities or NaN.
Stringifying the values, while producing "valid" JSON, could result in issues with some code that reads the produced JSON, especially since something that is expected to be a number might instead be a string. Some use cases could also depend on the type of value in a particular field; stringifying these values could complicate the related logic.
My proposal would be to give the decision of what to do to the programmer: add an optional argument taking a table that contains keys "+inf"/"-inf"/"nan"/"-0" mapping to the alternative Lua representation to serialise in the particular situation; if the entry for a particular situation isn't present in the table, or the table isn't provided, throw an exception. This means that the person using the function has the guarantee that the resulting JSON is valid, and if there's any expectation that special numbers are to show up, full control is granted over how they're written to the still-valid JSON file.
Re: [0.17.69] table_to_json outputs inf, which is invalid json
Posted: Thu Nov 07, 2019 11:24 am
by Rseding91
Note to other developers: I don't really think this is worth looking into. The table_to_json and json_to_table were added to the Lua API through a pull request because "hey, that might be useful" but we really don't care about actually following the JSON spec and I don't consider it worth the code bloat/complication it's going to have to add in these extra edge cases.
I consider it a failure in the JSON spec if it can't handle inf/nan/-0 and not something that we should be changing how we store files on disk to account for. If anything, I would just remove the Lua API if enough people complain about it since it's for our own use and not for modders. The fact modders can use it is just a small bonus because "why not".
With that said, I'm moving this to minor issues.