DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on

YAML (and JSONC) failed as a data transfer format with comments

Deserializing to native data structures typically results in comment loss, and you cannot really write comments programmatically. (In most implementations, actually. You can indeed preserve comments in Python's ruamel.yaml.)

Solution? I suggest a half-deserialized format, XML tree (with comments), then traverse via XPath, only to continue to native data structures, if asked to.

Top comments (5)

Collapse
 
stevepryde profile image
Steve Pryde

I generally prefer YAML (or TOML is also ok) for human readable/writable config files and JSON for data.

You're on your own with XML ;P

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

But for YAML, machines cannot preserve comments?

Collapse
 
stevepryde profile image
Steve Pryde

I'm not sure I understand what you mean. YAML is not good as a serialization format. It is for configuration only. The comments are only for humans.

I do wish JSON allowed comments, however. This is the main reason why I prefer YAML over JSON for configuration files that are intended to be edited by humans.

Thread Thread
 
patarapolw profile image
Pacharapol Withayasakpunt

yaml.dump(yaml.load($string)), and comments are lost.

Thread Thread
 
stevepryde profile image
Steve Pryde

Ah ok. Yes I can see why this would be annoying.

I believe there are yaml parsers that do preserve comments but probably not for every language.