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.

dlazesz
  • 168
  • 17
  • Have you checked-out my answer to [Making object JSON serializable with regular encoder](https://stackoverflow.com/questions/18478287/making-object-json-serializable-with-regular-encoder)? – martineau Oct 17 '20 at 22:06
  • Does this answer your question? [Making object JSON serializable with regular encoder](https://stackoverflow.com/questions/18478287/making-object-json-serializable-with-regular-encoder) – AMC Oct 18 '20 at 00:19
  • The most distinctive part of this question is that the serialized output MUST be human readable as e.g. repr() in Python. Your suggested question/answer does not fulfill this requirement (it uses escaped pickled stuff, like the other linked answers). However, they are nice patches to the Encoder/Decoder framework, which could be improved to be human readable. Since it involves monkey patching, the answer below is better for the current question, even though it is YAML, not JSON. – dlazesz Oct 19 '20 at 10:21

1 Answers1

1

The "YAML printed in one line" format similar to JSONL is called "inline syntax" and is achievable with default_flow_style=True, however, this is very badly documented. The result is not JSON, but still standard compliant and does not require custom encoder/decoder.

See the example:

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
dlazesz
  • 168
  • 17