[0.17.69] table_to_json outputs inf, which is invalid json

We are aware of them, but they have low priority. We have more important things to do. They go here in order not to take space in the main bug thread list.
Post Reply
Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

[0.17.69] table_to_json outputs inf, which is invalid json

Post by Boodals »

The following command will output invalid json:

Code: Select all

/c game.print(game.table_to_json({math.huge}))
Will output:

Code: Select all

[inf]
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

User avatar
TruePikachu
Filter Inserter
Filter Inserter
Posts: 978
Joined: Sat Apr 09, 2016 8:39 pm
Contact:

Re: [0.17.69] table_to_json outputs inf, which is invalid json

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.17.69] table_to_json outputs inf, which is invalid json

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Minor issues”