jsony
jsony copied to clipboard
Leave out object fields that have a null value
When converting from an object to JSON, if a field is an Option type, then I want to leave that field out of the JSON. The reason is to save space and thereby reduce IO because I'm saving JSON to the DB.
I can understand that you typically want to include the field with a null value, so a setting to optionally leave out null fields would be great.
You can do this with a custom dump hook.
Take a look at: https://github.com/treeform/jsony/blob/master/tests/test_quirkydump.nim#L29-L57
The https://github.com/treeform/jsony/blob/master/tests/test_quirkydump.nim#L29-L57 checks if there is an option then leaves out they key and the value.
You can also use this to compress default values like 0 or "". But again custom dump hook is required.
Does this answer your question?
I need the same, and am doing it like that, but one has to override all dumpHooks that deal with any kind of container (object, ref object, array, seq, table, ...). I do think it should be a default option of jsony, as it is a lot of work/code, for a feature that I think will commonly be requested. It makes senes ot save space like in @jfilby's case, or like in mine, where I want to dump a config file, which should only contain the values that are actually set, to not clutter the file.