There are the three well-known format:
It is well-known, that
- JSON can not handle Python sets without custom encoders/decoders.
- YAML 1.2 is strict superset of JSON
- JSONL is JSON written in one line
I want to serialize (and unserialize) Python set (maybe other object as well) into one line (JSON or YAML does not matter), just like JSONL + custom encoder/decoder would do, but in a way that is human readable (like repr()
in Python, see example below) (and possibly compliant with YAML). I would also like to keep all other functionalities and avoid workarounds.
Do I have to write my own custom encoder or there are some better/existing solution? (e.g. parameter to yaml.dump() to do it in one line) What would be the most robust way to implement the example below?
E.g.:
data = [1, 2, 3, {'say', 'knights', 'ni', 'who'}, {'key': 'value'}, 3.14]
data_dumped = dump(data, ...) # Magic dump function
print(data_dumped)
[1, 2, 3, !!set {knights: null, ni: null, say: null, who: null}, {"key": "value"}, 3.14]
data_loaded = load(data_dumped, ...) # Magic load function
assert data == data_loaded
UPDATE: I have linked answers showcasing monkey patched JSONEncoder
making set()
(and other types) serializable using pickle
, which is not human readable, therefore they do not answer this question. If these kinds of answers were good without modification, this question would be duplicate to the cited ones.