-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathjsonParser.cpp
More file actions
180 lines (154 loc) · 5.31 KB
/
Copy pathjsonParser.cpp
File metadata and controls
180 lines (154 loc) · 5.31 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
/*
Usage:
char value[];
String json = http.getString();
const char* var = "find";
getJsonValue(json.c_str(), var, value, 2);
// 'value' contains returned value associated with variable
s60sc 2026
*/
#include "appGlobals.h"
struct Slice {
const char* ptr;
size_t len;
};
static Slice extractNestedValue(const char* obj, size_t len, const char* nestedKey, size_t nestedKeyLen);
static inline void skipWhitespace(const char*& p, const char* end) {
while (p < end && isspace(static_cast<unsigned char>(*p))) p++;
}
static Slice parseString(const char*& p, const char* end) {
if (p >= end || *p != '"') return {nullptr, 0};
p++; // skip opening quote
const char* start = p;
while (p < end) {
if (*p == '\\') p += 2; // skip escaped char
else if (*p == '"') {
size_t len = p - start;
p++; // skip closing quote
return {start, len};
} else p++;
}
return {nullptr, 0};
}
static void skipValue(const char*& p, const char* end) {
if (p >= end) return;
if (*p == '"') {
(void)parseString(p, end);
return;
}
if (*p == '{') {
int depth = 1;
p++;
while (p < end && depth > 0) {
if (*p == '"') parseString(p, end);
else if (*p == '{') { depth++; p++; }
else if (*p == '}') { depth--; p++; }
else p++;
}
return;
}
if (*p == '[') {
int depth = 1;
p++;
while (p < end && depth > 0) {
if (*p == '"') parseString(p, end);
else if (*p == '[') { depth++; p++; }
else if (*p == ']') { depth--; p++; }
else p++;
}
return;
}
// Number, boolean, null
while (p < end && *p != ',' && *p != '}' && *p != ']') p++;
}
static Slice parseValue(const char*& p, const char* end) {
if (p >= end) return {nullptr, 0};
const char* start = p;
if (*p == '"') return parseString(p, end);
if (*p == '{' || *p == '[') {
skipValue(p, end);
return {start, size_t(p - start)};
}
// Number, boolean, null - trim whitespace
while (p < end && *p != ',' && *p != '}' &&*p != ']' && !isspace(static_cast<unsigned char>(*p))) p++;
// Trim trailing whitespace
const char* finish = p;
while (finish > start && isspace(static_cast<unsigned char>(*(finish - 1)))) finish--;
return {start, size_t(finish - start)};
}
static Slice findNthOccurrence(const char*& p, const char* end, const char* key, size_t keyLen, int& occurrence, int targetOccurrence, bool extractNested, const char* nestedKey, size_t nestedKeyLen) {
// Recursively search through entire JSON structure for nth occurrence
skipWhitespace(p, end);
if (p >= end) return {nullptr, 0};
if (*p == '{') {
p++;
while (p < end) {
skipWhitespace(p, end);
if (p >= end) break;
if (*p == '}') { p++; break; }
Slice k = parseString(p, end);
skipWhitespace(p, end);
if (p >= end || *p != ':') return {nullptr, 0};
p++;
skipWhitespace(p, end);
bool match = (k.len == keyLen && memcmp(k.ptr, key, keyLen) == 0);
if (match) {
occurrence++;
if (occurrence == targetOccurrence) {
Slice v = parseValue(p, end);
if (extractNested && v.ptr && v.len && *v.ptr == '{') return extractNestedValue(v.ptr, v.len, nestedKey, nestedKeyLen);
return v;
} else skipValue(p, end);
} else {
if (*p == '{' || *p == '[') {
Slice r = findNthOccurrence(p, end, key, keyLen, occurrence, targetOccurrence, extractNested, nestedKey, nestedKeyLen);
if (r.ptr) return r;
} else skipValue(p, end);
}
skipWhitespace(p, end);
if (p < end && *p == ',') p++;
}
}
else if (*p == '[') {
p++;
// Recursively search array elements
while (p < end) {
skipWhitespace(p, end);
if (p >= end) break;
if (*p == ']') { p++; break; }
if (*p == '{' || *p == '[') {
Slice r = findNthOccurrence(p, end, key, keyLen, occurrence, targetOccurrence, extractNested, nestedKey, nestedKeyLen);
if (r.ptr) return r;
} else skipValue(p, end);
skipWhitespace(p, end);
if (p < end && *p == ',') p++;
}
}
return {nullptr, 0};
}
static Slice extractNestedValue(const char* obj, size_t len, const char* nestedKey, size_t nestedKeyLen) {
const char* p = obj;
const char* end = obj + len;
int count = 0;
return findNthOccurrence(p, end, nestedKey, nestedKeyLen, count, 1, false, nullptr, 0);
}
bool getJsonValue(const char* json, const char* key, char* value, const char* nestedKey, int occurrence) {
// Returns nth occurrence (1-indexed, default is 1)
// If nestedKey is provided, extracts that field from the object value
if (!json || !key || !value) return false;
const char* p = json;
const char* end = json + strlen(json);
size_t keyLen = strlen(key);
size_t nestedKeyLen = nestedKey ? strlen(nestedKey) : 0;
if (occurrence < 1) occurrence = 1;
int count = 0;
Slice result = findNthOccurrence(p, end, key, keyLen, count, occurrence, nestedKey && nestedKey[0], nestedKey, nestedKeyLen);
if (!result.ptr) {
value[0] = '\0';
return false;
}
size_t len = min(result.len, size_t(FILE_NAME_LEN - 1));
memcpy(value, result.ptr, len);
value[len] = '\0';
return true;
}