| 46 | } // namespace |
| 47 | |
| 48 | ConfigMap parse_flat_json_object(const std::string & text) { |
| 49 | const auto root = json::parse(text); |
| 50 | const auto & object = root.as_object(); |
| 51 | ConfigMap config; |
| 52 | config.reserve(object.size()); |
| 53 | for (const auto & [key, value] : object) { |
| 54 | if (value.is_string()) { |
| 55 | config[key] = value.as_string(); |
| 56 | continue; |
| 57 | } |
| 58 | if (value.is_bool()) { |
| 59 | config[key] = value.as_bool() ? "true" : "false"; |
| 60 | continue; |
| 61 | } |
| 62 | if (value.is_null()) { |
| 63 | config[key] = "null"; |
| 64 | continue; |
| 65 | } |
| 66 | if (value.is_number()) { |
| 67 | const double number = value.as_number(); |
| 68 | if (std::isfinite(number) && std::floor(number) == number) { |
| 69 | config[key] = std::to_string(static_cast<long long>(number)); |
| 70 | } else { |
| 71 | std::ostringstream stream; |
| 72 | stream << number; |
| 73 | config[key] = stream.str(); |
| 74 | } |
| 75 | continue; |
| 76 | } |
| 77 | throw std::runtime_error("flat json config values must be scalar"); |
| 78 | } |
| 79 | return config; |
| 80 | } |
| 81 | |
| 82 | ConfigMap parse_key_value_text(const std::string & text, char separator) { |
| 83 | return parse_lines(text, separator); |