Add a way to have null fields in game.table_to_json() function
Posted: Sun Jan 21, 2024 1:28 am
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.
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.