7#include <yaml-cpp/yaml.h>
13 inline nlohmann::json parse_scalar(
const YAML::Node &node)
21 if (YAML::convert<std::string>::decode(node, s) && s ==
"y")
24 if (YAML::convert<int>::decode(node, i))
26 if (YAML::convert<double>::decode(node, d))
28 if (YAML::convert<bool>::decode(node, b))
30 if (YAML::convert<std::string>::decode(node, s))
37 inline nlohmann::json yaml_to_json(
const YAML::Node &root)
43 case YAML::NodeType::Null:
45 case YAML::NodeType::Scalar:
46 return parse_scalar(root);
47 case YAML::NodeType::Sequence:
48 for (
auto &&node : root)
49 j.emplace_back(yaml_to_json(node));
51 case YAML::NodeType::Map:
52 for (
auto &&it : root)
53 j[it.first.as<std::string>()] = yaml_to_json(it.second);
64 YAML::Node root = YAML::Load(yaml_str);
65 return yaml_to_json(root);
70 YAML::Node root = YAML::LoadFile(yaml_filepath);
71 return yaml_to_json(root);
json yaml_file_to_json(const std::string &yaml_filepath)
Load a YAML file to JSON.
json yaml_string_to_json(const std::string &yaml_str)
Convert YAML string to JSON.
void log_and_throw_error(const std::string &msg)