I have a project that needs to read a well documented yaml
file, modify a couple of values, and write it back out. The trouble is that yaml-cpp
completely strips out all comments and "eats" them. The interesting thing is that the YAML::Emitter
class allows one to add comments to the output. Is there a way to preserve the comments in the input and write them back in the library that I'm not seeing? Because as it stands right now, I can't see any way using the YAML::Parser
class (which uses the YAML::Scanner
class, where the comments themselves are actually "eaten").
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
According to the YAML spec
So you need to make the parser non-compliant to preserve comments, and if yaml-cpp did that, they should clearly state so in the documentation.
I did this for Python in ruamel.yaml. If embedding and calling Python from your C++ program is acceptible you could do something like the following (I used Python 3.5 for this under Linux Mint):
pythonyaml.cpp
:Create a
Makefile
(adapt the path to your Python3.5 installation, which needs to have the headers installed, as is normal if compiled from source, otherwise you need the packagepython3-dev
installed):compile the program with
make
.Create
update_yaml.py
which will be loaded bypythonyaml
:Create
input.yaml
:If you have
ruamel.yaml
installed in your python3.5 and run./python_yaml
it will printOld value: -999
, and the new fileoutput.yaml
will contain:42
has only two characters where-999
has four, the comment still aligns with the one below itabc.1.klm
you can create a Python list in C++, and hand that toload_update_save()
as third parameter. In that case you can have keys that are other items than strings, or keys that are a string that contains a dotPyLong_FromLong
for the fourth parameter) for the value. The python program doesn't need updating for that.ruamel.yaml