This was a problem in number of projects that we were using YAML. We wanted to update some values in YAML files without loosing the formatting and comments.
I wrote the YAWN YAML library to solve this problem. YAWN YAML uses the Abstract Syntax Tree(AST) of a YAML document to rebuild the file structure upon changes. This makes it possible to change a value in the YAML document without loosing structure of the document.
Here is an example of how it works:
Please note that you need to replace
.json value of the
yawn instance object. This is because the setter function is looking at the new JSON and reconstruct the YAML structure.
This library is heavily tested and can be used in browser and Node.js environments. Please file a bug if you found one.