0

There are the three well-known format:

It is well-known, that

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.

4

1 回答 1

1

类似于 JSONL 的“YAML 以一行打印”格式称为“内联语法”,可以default_flow_style=True通过. 结果不是 JSON,但仍符合标准并且不需要自定义编码器/解码器。

请参阅示例:

from yaml import dump, load
data = [1, 2, 3, {'say', 'knights', 'ni', 'who'}, {'key': 'value'}, 3.14]
data_dumped = dump(data, default_flow_style=True)
print(data_dumped)
[1, 2, 3, !!set {knights: null, ni: null, say: null, who: null}, {"key": "value"}, 3.14]
data_loaded = load(data_dumped)
assert data == data_loaded
于 2020-10-17T22:09:29.247 回答