5 #ifndef GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_
6 #define GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_
12 #include <yaml-cpp/yaml.h>
14 #include <ginkgo/core/config/property_tree.hpp>
28 auto parse_array = [](
const auto& arr) {
29 gko::config::pnode::array_type nodes;
30 for (
const auto& it : arr) {
31 nodes.emplace_back(parse_yaml(it));
35 auto parse_map = [](
const auto& map) {
36 gko::config::pnode::map_type nodes;
38 for (
const auto& yaml_item : map) {
39 std::string key = yaml_item.first.template as<std::string>();
43 auto node = parse_yaml(yaml_item.second);
44 if (node.get_tag() == gko::config::pnode::tag_t::array) {
45 for (
const auto& arr : node.get_array()) {
46 if (arr.get_tag() != gko::config::pnode::tag_t::map) {
47 throw std::runtime_error(
48 "YAML only accepts merge key << to merge item "
50 YAML::Dump(yaml_item.second));
52 for (
const auto& item : arr.get_map()) {
53 nodes[item.first] = item.second;
56 }
else if (node.get_tag() == gko::config::pnode::tag_t::map) {
57 for (
const auto& item : node.get_map()) {
58 nodes[item.first] = item.second;
61 throw std::runtime_error(
"can not handle this alias: " +
62 YAML::Dump(yaml_item.second));
65 nodes[key] = parse_yaml(yaml_item.second);
71 auto parse_data = [](
const auto& data) {
72 if (std::int64_t value;
73 YAML::convert<std::int64_t>::decode(data, value)) {
76 if (
bool value; YAML::convert<bool>::decode(data, value)) {
79 if (
double value; YAML::convert<double>::decode(data, value)) {
82 if (std::string value;
83 YAML::convert<std::string>::decode(data, value)) {
86 std::string content = YAML::Dump(data);
87 throw std::runtime_error(
88 "property_tree can not handle the node with content: " + content);
91 if (input.IsSequence()) {
92 return parse_array(input);
95 return parse_map(input);
97 return parse_data(input);
127 return parse_yaml(YAML::LoadFile(filename));
136 #endif // GKO_PUBLIC_EXTENSIONS_CONFIG_JSON_CONFIG_HPP_