| 27 | using namespace std; |
| 28 | |
| 29 | int get_json_str_map( |
| 30 | const string &str, |
| 31 | ostream &ss, |
| 32 | str_map_t *str_map, |
| 33 | bool fallback_to_plain) |
| 34 | { |
| 35 | json_spirit::mValue json; |
| 36 | try { |
| 37 | // try json parsing first |
| 38 | |
| 39 | json_spirit::read_or_throw(str, json); |
| 40 | |
| 41 | if (json.type() != json_spirit::obj_type) { |
| 42 | ss << str << " must be a JSON object but is of type " |
| 43 | << json.type() << " instead"; |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | |
| 47 | json_spirit::mObject o = json.get_obj(); |
| 48 | |
| 49 | for (map<string, json_spirit::mValue>::iterator i = o.begin(); |
| 50 | i != o.end(); |
| 51 | ++i) { |
| 52 | (*str_map)[i->first] = i->second.get_str(); |
| 53 | } |
| 54 | } catch (json_spirit::Error_position &e) { |
| 55 | if (fallback_to_plain) { |
| 56 | // fallback to key=value format |
| 57 | get_str_map(str, str_map, "\t\n "); |
| 58 | } else { |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static std::string_view trim(std::string_view str) |
| 66 | { |