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)
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
But for YAML, machines cannot preserve comments?
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.
yaml.dump(yaml.load($string))
, and comments are lost.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.