jbeder / yaml-cpp

A YAML parser and emitter in C++
MIT License
5.04k stars 1.81k forks source link

Preserve comments after parsing and re-emit them #457

Open tambry opened 7 years ago

tambry commented 7 years ago

For example I have the following code:

std::fstream config_file("config.yaml", std::ios::in | std::ios::out);
YAML::Node config_node = YAML::Load(config_file);
config_file.close();

// For example parse over the config, confirm it's correct, maybe correct mistakes, update the config to a new version, etc...

std::fstream config_file_write("config.yaml", std::ios::out | std::ios::trunc);
YAML::Emitter emitter;
emitter << config_node;
config_file_write << emitter.c_str();

Now maybe the user has written some comments in the config file to remind themselves why they set an option to that value. If the config is for example loaded through a GUI and modified or the config is loaded by the application and modified any way and then the config is saved again, the user's comments are lost.

Is there any way to preserve the comments when emitting the original parsed object?

SimplyKnownAsG commented 6 years ago

related to #154