-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.h
More file actions
254 lines (221 loc) · 6.78 KB
/
Copy pathjson_parser.h
File metadata and controls
254 lines (221 loc) · 6.78 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <variant>
#include <stdexcept>
using namespace std;
enum TokenType {
LEFT_BRACE, // {
RIGHT_BRACE, // }
LEFT_BRACKET, // [
RIGHT_BRACKET, // ]
COMMA, // ,
COLON, // :
STRING, // "abc"
NUMBER, // 123
BOOLEAN, // true or false
NUL, // null
END_OF_FILE // end of file
};
struct Token {
TokenType type;
string value;
};
class Lexer {
public:
Lexer(const string& input) : input(input), position(0) {}
Token nextToken() {
skipWhitespace();
if (position >= input.size()) return {END_OF_FILE, ""};
char currentChar = input[position];
if (currentChar == '{') return advanceToken(LEFT_BRACE, "{");
if (currentChar == '}') return advanceToken(RIGHT_BRACE, "}");
if (currentChar == '[') return advanceToken(LEFT_BRACKET, "[");
if (currentChar == ']') return advanceToken(RIGHT_BRACKET, "]");
if (currentChar == ',') return advanceToken(COMMA, ",");
if (currentChar == ':') return advanceToken(COLON, ":");
if (currentChar == '"') return stringToken();
if (isdigit(currentChar) || currentChar == '-') return numberToken();
if (isalpha(currentChar)) return keywordToken();
throw runtime_error("Unexpected character in JSON input");
}
private:
string input;
size_t position;
Token advanceToken(TokenType type, const string& value) {
position++;
return {type, value};
}
void skipWhitespace() {
while (position < input.size() && isspace(input[position])) {
position++;
}
}
Token stringToken() {
position++; // skip the opening quote
size_t start = position;
while (position < input.size() && input[position] != '"') {
position++;
}
if (position >= input.size()) throw runtime_error("Unterminated string in JSON input");
string value = input.substr(start, position - start);
position++; // skip the closing quote
return {STRING, value};
}
Token numberToken() {
size_t start = position;
while (position < input.size() && (isdigit(input[position]) || input[position] == '.' || input[position] == '-')) {
position++;
}
return {NUMBER, input.substr(start, position - start)};
}
Token keywordToken() {
size_t start = position;
while (position < input.size() && isalpha(input[position])) {
position++;
}
string value = input.substr(start, position - start);
if (value == "true" || value == "false") return {BOOLEAN, value};
if (value == "null") return {NUL, value};
throw runtime_error("Unexpected keyword in JSON input");
}
};
class JsonNode {
public:
using JsonObject = map<string, shared_ptr<JsonNode>>;
using JsonArray = vector<shared_ptr<JsonNode>>;
using JsonValue = variant<JsonObject, JsonArray, string, double, bool, nullptr_t>;
JsonNode() = default;
explicit JsonNode(JsonValue value) : value(move(value)) {}
shared_ptr<JsonNode> operator[](const string& key) const {
if (auto obj = get_if<JsonObject>(&value)) {
auto it = obj->find(key);
if (it != obj->end()) {
return it->second;
}
}
throw runtime_error("Key not found in JSON object");
}
shared_ptr<JsonNode> operator[](size_t index) const {
if (auto arr = get_if<JsonArray>(&value)) {
if (index < arr->size()) {
return (*arr)[index];
}
}
throw runtime_error("Index out of bounds in JSON array");
}
const JsonValue& getValue() const {
return value;
}
private:
JsonValue value;
};
class Parser {
public:
Parser(const string& input) : lexer(input) {}
shared_ptr<JsonNode> parse() {
currentToken = lexer.nextToken();
return parseValue();
}
private:
Lexer lexer;
Token currentToken;
shared_ptr<JsonNode> parseValue() {
switch (currentToken.type) {
case STRING: return parseString();
case NUMBER: return parseNumber();
case BOOLEAN: return parseBoolean();
case NUL: return parseNull();
case LEFT_BRACE: return parseObject();
case LEFT_BRACKET: return parseArray();
default: throw runtime_error("Unexpected value in JSON input");
}
}
shared_ptr<JsonNode> parseString() {
if (currentToken.type != STRING) throw runtime_error("Expected a string");
auto node = make_shared<JsonNode>(currentToken.value);
currentToken = lexer.nextToken();
return node;
}
shared_ptr<JsonNode> parseNumber() {
if (currentToken.type != NUMBER) throw runtime_error("Expected a number");
auto node = make_shared<JsonNode>(stod(currentToken.value));
currentToken = lexer.nextToken();
return node;
}
shared_ptr<JsonNode> parseBoolean() {
if (currentToken.type != BOOLEAN) throw runtime_error("Expected a boolean");
auto node = make_shared<JsonNode>(currentToken.value == "true");
currentToken = lexer.nextToken();
return node;
}
shared_ptr<JsonNode> parseNull() {
if (currentToken.type != NUL) throw runtime_error("Expected null");
auto node = make_shared<JsonNode>(nullptr);
currentToken = lexer.nextToken();
return node;
}
shared_ptr<JsonNode> parseObject() {
map<string, shared_ptr<JsonNode>> jsonObject;
consumeToken(LEFT_BRACE);
while (currentToken.type != RIGHT_BRACE) {
auto keyNode = parseString();
if (keyNode->getValue().index() != 2) {
throw runtime_error("Expected a string as key");
}
string key = get<string>(keyNode->getValue());
consumeToken(COLON);
auto value = parseValue();
jsonObject[key] = value;
if (currentToken.type == COMMA) consumeToken(COMMA);
}
consumeToken(RIGHT_BRACE);
return make_shared<JsonNode>(jsonObject);
}
shared_ptr<JsonNode> parseArray() {
vector<shared_ptr<JsonNode>> jsonArray;
consumeToken(LEFT_BRACKET);
while (currentToken.type != RIGHT_BRACKET) {
jsonArray.push_back(parseValue());
if (currentToken.type == COMMA) consumeToken(COMMA);
}
consumeToken(RIGHT_BRACKET);
return make_shared<JsonNode>(jsonArray);
}
void consumeToken(TokenType expectedType) {
if (currentToken.type != expectedType) {
throw runtime_error("Unexpected token in JSON input");
}
currentToken = lexer.nextToken();
}
};
void printJson(shared_ptr<JsonNode> node, int indent = 0) {
const auto& value = node->getValue();
if (auto obj = get_if<JsonNode::JsonObject>(&value)) {
cout << string(indent, ' ') << "{\n";
for (const auto& [key, val] : *obj) {
cout << string(indent + 2, ' ') << "\"" << key << "\": ";
printJson(val, indent + 2);
}
cout << string(indent, ' ') << "}\n";
} else if (auto arr = get_if<JsonNode::JsonArray>(&value)) {
cout << string(indent, ' ') << "[\n";
for (const auto& val : *arr) {
printJson(val, indent + 2);
}
cout << string(indent, ' ') << "]\n";
} else if (auto str = get_if<string>(&value)) {
cout << "\"" << *str << "\"\n";
} else if (auto num = get_if<double>(&value)) {
cout << *num << "\n";
} else if (auto boolean = get_if<bool>(&value)) {
cout << (*boolean ? "true" : "false") << "\n";
} else if (holds_alternative<nullptr_t>(value)) {
cout << "null\n";
}
}