Add a way to have null fields in game.table_to_json() function

Post your ideas and suggestions how to improve the game.

Moderator: ickputzdirwech

leo_85
Burner Inserter
Burner Inserter
Posts: 6
Joined: Wed Nov 22, 2023 10:30 am
Contact:

Add a way to have null fields in game.table_to_json() function

Post by leo_85 »

hello,

I'm working on a mod that use the game.table_to_json function a lot.
The problem I face is that, in Lua, setting a table value to nil is equivalent to removing this value from the table and because of this the fields does not appear in the json, but I have plenty of usecases where I need some fields on the final json string to appear (null or not).

So here's my proposition: add an optional argument to table_to_json that is an array, specifying the fields that need to be set to null in the json representation if they are not found in the table.

The problem i have with that is that it doesn't work for nested fields (but maybe someone will have an idea to work around that).

Also adding a "null placeholder" object that is converted to null when passed to the table_to_json function is not a solution because it will break the codes that rely on table length and fields existence, and make object that have no sens outside of the table_to_json function occupying memory.

I hope you will find my suggestion relevant and that it is not a duplicate.
robot256
Filter Inserter
Filter Inserter
Posts: 932
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: Add a way to have null fields in game.table_to_json() function

Post by robot256 »

(edited after more reading) Your post already explains why the suggestion as written is cumbersome and incomplete. Table keys that were set to nil don't exist, ergo every possible table key is set to nil besides the ones that aren't. You can't print them all, so which nil values do you decide to print? Only your specific application code can know, unless you narrow your suggestion to a particular scenario like Lua objects.

The suggested method of using an array of fields that default to printing nil can be implemented as a six-line helper function in your code or a library, which should work fine for your application. It should not be part of table_to_json itself.

Function setTableNilFields(tab, keys)
For _,key in pairs(keys) do
Tab[key] = tab[key] or "nil"
End
Return tab
End

This reads like an X-Y problem: why does the code *reading* the JSON file need to know which values were *set* to nil? They don't exist. So really, you are trying to send some other information by having them explicitly written in the file as nil. You can find another way to send that information in the JSON. Maybe by setting particular keys to the string "nil", or maybe you can add a more explicit key like "vehicle-color-was-cleared-by-user"=true.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Add a way to have null fields in game.table_to_json() function

Post by quyxkh »

This is a straightforward `jq` filter, object addition defaults to the field values on the left if they're missing on the right so just supply your defaults that way. No point reimplementing existing tools.
robot256
Filter Inserter
Filter Inserter
Posts: 932
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: Add a way to have null fields in game.table_to_json() function

Post by robot256 »

I should have thought of this first. You can use the existing util.merge function in the Factorio core Lualib:

util.merge({defaults, input})
Post Reply

Return to “Ideas and Suggestions”