json.cc (5830B)
1 #include "json.hh" 2 3 #include "exception.hh" 4 #include "string_utils.hh" 5 #include "unit_tests.hh" 6 #include "utils.hh" 7 #include "ranges.hh" 8 9 #include <algorithm> 10 #include <cstdio> 11 12 namespace Kakoune 13 { 14 15 String to_json(int i) { return to_string(i); } 16 String to_json(bool b) { return b ? "true" : "false"; } 17 String to_json(StringView str) 18 { 19 String res; 20 res.reserve(str.length() + 4); 21 res += '"'; 22 for (auto it = str.begin(), end = str.end(); it != end; ) 23 { 24 auto next = std::find_if(it, end, [](char c) { 25 return c == '\\' or c == '"' or (unsigned char) c <= 0x1F; 26 }); 27 28 res += StringView{it, next}; 29 if (next == end) 30 break; 31 32 char buf[7] = {'\\', *next, 0}; 33 if ((unsigned char) *next <= 0x1F) 34 format_to(buf, "\\u{:04}", hex(*next)); 35 36 res += buf; 37 it = next+1; 38 } 39 res += '"'; 40 return res; 41 } 42 43 static bool is_digit(char c) { return c >= '0' and c <= '9'; } 44 45 static constexpr size_t max_parsing_depth = 100; 46 47 JsonResult parse_json_impl(const char* pos, const char* end, size_t depth) 48 { 49 if (not skip_while(pos, end, is_blank)) 50 return {}; 51 52 if (depth >= max_parsing_depth) 53 throw runtime_error("maximum parsing depth reached"); 54 55 if (is_digit(*pos) or *pos == '-') 56 { 57 auto digit_end = pos + 1; 58 skip_while(digit_end, end, is_digit); 59 return { Value{str_to_int({pos, digit_end})}, digit_end }; 60 } 61 if (end - pos > 4 and StringView{pos, pos+4} == "true") 62 return { Value{true}, pos+4 }; 63 if (end - pos > 5 and StringView{pos, pos+5} == "false") 64 return { Value{false}, pos+5 }; 65 if (*pos == '"') 66 { 67 String value; 68 bool escaped = false; 69 ++pos; 70 for (auto string_end = pos; string_end != end; ++string_end) 71 { 72 if (escaped) 73 { 74 escaped = false; 75 value += StringView{pos, string_end}; 76 value.back() = *string_end; 77 pos = string_end+1; 78 continue; 79 } 80 if (*string_end == '\\') 81 escaped = true; 82 if (*string_end == '"') 83 { 84 value += StringView{pos, string_end}; 85 return {std::move(value), string_end+1}; 86 } 87 } 88 return {}; 89 } 90 if (*pos == '[') 91 { 92 JsonArray array; 93 if (++pos == end) 94 throw runtime_error("unable to parse array"); 95 if (*pos == ']') 96 return {std::move(array), pos+1}; 97 98 while (true) 99 { 100 auto [element, new_pos] = parse_json_impl(pos, end, depth+1); 101 if (not element) 102 return {}; 103 pos = new_pos; 104 array.push_back(std::move(element)); 105 if (not skip_while(pos, end, is_blank)) 106 return {}; 107 108 if (*pos == ',') 109 ++pos; 110 else if (*pos == ']') 111 return {std::move(array), pos+1}; 112 else 113 throw runtime_error("unable to parse array, expected ',' or ']'"); 114 } 115 } 116 if (*pos == '{') 117 { 118 if (++pos == end) 119 throw runtime_error("unable to parse object"); 120 JsonObject object; 121 if (*pos == '}') 122 return {std::move(object), pos+1}; 123 124 while (true) 125 { 126 auto [name_value, name_end] = parse_json_impl(pos, end, depth+1); 127 if (not name_value) 128 return {}; 129 pos = name_end; 130 String& name = name_value.as<String>(); 131 if (not skip_while(pos, end, is_blank)) 132 return {}; 133 if (*pos++ != ':') 134 throw runtime_error("expected :"); 135 136 auto [element, element_end] = parse_json_impl(pos, end, depth+1); 137 if (not element) 138 return {}; 139 pos = element_end; 140 object.insert({ std::move(name), std::move(element) }); 141 if (not skip_while(pos, end, is_blank)) 142 return {}; 143 144 if (*pos == ',') 145 ++pos; 146 else if (*pos == '}') 147 return {std::move(object), pos+1}; 148 else 149 throw runtime_error("unable to parse object, expected ',' or '}'"); 150 } 151 } 152 throw runtime_error("unable to parse json"); 153 } 154 155 JsonResult parse_json(const char* pos, const char* end) { return parse_json_impl(pos, end, 0); } 156 JsonResult parse_json(StringView json) { return parse_json_impl(json.begin(), json.end(), 0); } 157 158 UnitTest test_json_parser{[]() 159 { 160 { 161 auto value = parse_json(R"({ "jsonrpc": "2.0", "method": "keys", "params": [ "b", "l", "a", "h" ] })").value; 162 kak_assert(value); 163 } 164 165 { 166 auto value = parse_json("[10,20]").value; 167 kak_assert(value and value.is_a<JsonArray>()); 168 kak_assert(value.as<JsonArray>().at(1).as<int>() == 20); 169 } 170 171 { 172 auto value = parse_json("-1").value; 173 kak_assert(value.as<int>() == -1); 174 } 175 176 { 177 auto value = parse_json("{}").value; 178 kak_assert(value and value.is_a<JsonObject>()); 179 kak_assert(value.as<JsonObject>().empty()); 180 } 181 182 { 183 char big_nested_array[max_parsing_depth*2+2+1] = {}; 184 for (size_t i = 0; i < max_parsing_depth+1; i++) 185 { 186 big_nested_array[i] = '['; 187 big_nested_array[i+max_parsing_depth+1] = ']'; 188 } 189 kak_expect_throw(runtime_error, parse_json(big_nested_array)); 190 } 191 }}; 192 193 UnitTest test_to_json{[]() 194 { 195 kak_assert(to_json(true) == "true"); 196 kak_assert(to_json(false) == "false"); 197 kak_assert(to_json(HashMap<String, Vector<int>>{{"foo", {1,2,3}}, {"\033", {3, 4, 5}}}) == R"({"foo": [1, 2, 3],"\u001b": [3, 4, 5]})"); 198 }}; 199 200 }