1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| #include <iostream> #include <fstream> #include <string> #include <vector> #include <map> #include <stdexcept>
class JSONValue;
using JSONObject = std::map<std::string, JSONValue>; using JSONArray = std::vector<JSONValue>;
class JSONValue { public: enum Type { Null, Boolean, Number, String, Array, Object };
JSONValue() : type(Null) {} JSONValue(bool b) : type(Boolean), boolean(b) {} JSONValue(double n) : type(Number), number(n) {} JSONValue(const std::string& s) : type(String), str(s) {} JSONValue(const JSONArray& a) : type(Array), array(a) {} JSONValue(const JSONObject& o) : type(Object), object(o) {}
Type getType() const { return type; } bool isNull() const { return type == Null; } bool isBoolean() const { return type == Boolean; } bool isNumber() const { return type == Number; } bool isString() const { return type == String; } bool isArray() const { return type == Array; } bool isObject() const { return type == Object; }
bool asBoolean() const { return boolean; } double asNumber() const { return number; } const std::string& asString() const { return str; } const JSONArray& asArray() const { return array; } const JSONObject& asObject() const { return object; }
private: Type type; bool boolean; double number; std::string str; JSONArray array; JSONObject object; };
class JSONParser { public: static JSONValue parse(const std::string& json) { size_t index = 0; return parseValue(json, index); }
private: static JSONValue parseValue(const std::string& json, size_t& index) { skipWhitespace(json, index); if (index >= json.length()) { throw std::runtime_error("Unexpected end of input"); }
char c = json[index]; if (c == '{') { return parseObject(json, index); } else if (c == '[') { return parseArray(json, index); } else if (c == '"') { return parseString(json, index); } else if (c == 't' || c == 'f') { return parseBoolean(json, index); } else if (c == 'n') { return parseNull(json, index); } else if (c == '-' || (c >= '0' && c <= '9')) { return parseNumber(json, index); }
throw std::runtime_error("Unexpected character"); }
static JSONObject parseObject(const std::string& json, size_t& index) { JSONObject obj; index++;
while (index < json.length()) { skipWhitespace(json, index); if (json[index] == '}') { index++; return obj; }
std::string key = parseString(json, index).asString(); skipWhitespace(json, index);
if (index >= json.length() || json[index] != ':') { throw std::runtime_error("Expected ':' after key in object"); } index++;
JSONValue value = parseValue(json, index); obj[key] = value;
skipWhitespace(json, index); if (index < json.length() && json[index] == ',') { index++; } }
throw std::runtime_error("Unterminated object"); }
static JSONArray parseArray(const std::string& json, size_t& index) { JSONArray arr; index++;
while (index < json.length()) { skipWhitespace(json, index); if (json[index] == ']') { index++; return arr; }
arr.push_back(parseValue(json, index));
skipWhitespace(json, index); if (index < json.length() && json[index] == ',') { index++; } }
throw std::runtime_error("Unterminated array"); }
static JSONValue parseString(const std::string& json, size_t& index) { std::string result; index++;
while (index < json.length()) { char c = json[index++]; if (c == '"') { return JSONValue(result); } else if (c == '\\') { if (index >= json.length()) { throw std::runtime_error("Unterminated string"); } c = json[index++]; switch (c) { case '"': case '\\': case '/': result += c; break; case 'b': result += '\b'; break; case 'f': result += '\f'; break; case 'n': result += '\n'; break; case 'r': result += '\r'; break; case 't': result += '\t'; break; default: throw std::runtime_error("Invalid escape sequence"); } } else { result += c; } }
throw std::runtime_error("Unterminated string"); }
static JSONValue parseNumber(const std::string& json, size_t& index) { size_t start = index; while (index < json.length() && (json[index] == '-' || json[index] == '+' || json[index] == '.' || (json[index] >= '0' && json[index] <= '9') || json[index] == 'e' || json[index] == 'E')) { index++; } std::string numStr = json.substr(start, index - start); return JSONValue(std::stod(numStr)); }
static JSONValue parseBoolean(const std::string& json, size_t& index) { if (json.substr(index, 4) == "true") { index += 4; return JSONValue(true); } else if (json.substr(index, 5) == "false") { index += 5; return JSONValue(false); } throw std::runtime_error("Invalid boolean value"); }
static JSONValue parseNull(const std::string& json, size_t& index) { if (json.substr(index, 4) == "null") { index += 4; return JSONValue(); } throw std::runtime_error("Invalid null value"); }
static void skipWhitespace(const std::string& json, size_t& index) { while (index < json.length() && (json[index] == ' ' || json[index] == '\t' || json[index] == '\n' || json[index] == '\r')) { index++; } } };
|