Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yaml Parser Support #110

Open
sai-koushik-macha opened this issue Sep 20, 2023 · 1 comment
Open

Yaml Parser Support #110

sai-koushik-macha opened this issue Sep 20, 2023 · 1 comment

Comments

@sai-koushik-macha
Copy link

So, will there be support for the yaml parser just like json parser. The requirement I am using json parser for reading purpose by reading json file and after reading I think it interally converts it into ptree. So, is it possible to do same with yaml

@ljluestc
Copy link

ljluestc commented Feb 8, 2025

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/yaml_parser.hpp>
#include <iostream>
#include <fstream>

int main() {
    try {
        // Create a property tree
        boost::property_tree::ptree pt;

        // Read YAML from a file
        std::ifstream yaml_input("input.yaml");
        if (!yaml_input) {
            throw std::runtime_error("Failed to open input.yaml");
        }
        boost::property_tree::read_yaml(yaml_input, pt);

        // Access and print data from the YAML file
        std::cout << "Name: " << pt.get<std::string>("name") << std::endl;
        std::cout << "Age: " << pt.get<int>("age") << std::endl;
        std::cout << "Address: " << pt.get<std::string>("address.city") << ", " << pt.get<std::string>("address.state") << std::endl;

        // Modify the property tree
        pt.put("age", 31); // Update age
        pt.put("address.zip", "12345"); // Add a new field

        // Write the modified property tree to a new YAML file
        std::ofstream yaml_output("output.yaml");
        if (!yaml_output) {
            throw std::runtime_error("Failed to open output.yaml");
        }
        boost::property_tree::write_yaml(yaml_output, pt);

        std::cout << "Modified YAML written to output.yaml" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants