-
Notifications
You must be signed in to change notification settings - Fork 5
/
Json.h
238 lines (205 loc) · 5.85 KB
/
Json.h
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
#ifndef JSON_H
#define JSON_H
#include <WProgram.h>
#include "Core.h"
#include "TokenParser.h"
#include "StringList.h"
class Json {
public:
#define maxPositions 32
typedef enum {
NotJson = 0,
Array,
Object
} JsonType;
Json(String *string, s8 tail = -1, s8 head = -1)
{
jsonString = string;
tailLimit = -1;
headLimit = -1;
jsonType = NotJson;
jsonSize = 0;
if(string == 0) {
return;
}
us8 arrayLevel = 0;
us8 objectLevel = 0;
us8 index;
if(tail == -1) {
tail = 0;
}
if(head == -1) {
head = jsonString->length();
}
index = tail;
for(us8 i = 0; i < maxPositions; i++) {
positions[i] = -1;
}
while(index <= head) {
s8 value = TokenParser::match(jsonString->charAt(index), "{}[],");
if((value == 0) || (value == 2)) {
if(arrayLevel == 0 && objectLevel == 0) {
jsonType = (value == 0) ? Object : Array;
positions[jsonSize++] = index;
tail = index;
}
if(value == 0) {
objectLevel++;
}
else {
arrayLevel++;
}
}
else if((value == 1) || (value == 3)) {
if(value == 1) {
objectLevel--;
}
else {
arrayLevel--;
}
if((arrayLevel == 0) && (objectLevel == 0)) {
head = index;
break;
}
}
else if(value == 4) {
bool valueFound = false;
if(jsonType == Array) {
if((arrayLevel == 1) && (objectLevel == 0)) {
valueFound = true;
}
}
else {
if((objectLevel == 1) && (arrayLevel == 0)) {
valueFound = true;
}
}
if(valueFound) {
positions[jsonSize++] = index;
}
}
index++;
}
if(jsonSize == 0) {
positions[jsonSize++] = tail;
}
if((0 <= tail) && (tail <= head)) {
tailLimit = tail;
headLimit = head;
positions[jsonSize] = head;
}
}
JsonType type()
{
return jsonType;
}
us8 size()
{
return jsonSize;
}
String key(us8 index)
{
if(jsonType == Object) {
if((index < jsonSize) && (positions[index] != -1)) {
us8 length = positions[index + 1];
us8 tail = 0;
us8 head = 0;
for(us8 i = positions[index]; i < length; i++) {
if(jsonString->charAt(i) == '"') {
tail = ++i;
break;
}
}
for(us8 i = tail; i < length; i++) {
if(jsonString->charAt(i) == '"') {
head = i;
break;
}
}
return jsonString->substring(tail, head);
}
}
return "";
}
StringList keys()
{
StringList list;
if(jsonType == Object) {
for(us8 i = 0; i < jsonSize; i++) {
list << key(i);
}
}
return list;
}
Json value(int index)
{
if((index < jsonSize) && (positions[index] != -1)) {
us8 tail = positions[index] + 1;
us8 head = positions[index + 1] - 1;
if(jsonType == Object) {
for(us8 i = tail; i <= head; i++) {
if(jsonString->charAt(i) == ':') {
tail = i + 1;
break;
}
}
}
if((jsonType == Array) || (jsonType == Object)) {
for(us8 i = tail; i <= head; i++) {
if(TokenParser::match(jsonString->charAt(i), " ,\"") == -1) {
tail = i;
break;
}
}
for(us8 i = head; tail <= head;) {
if(TokenParser::match(jsonString->charAt(i), " ,\"") == -1) {
head = i;
break;
}
i--;
}
return Json(jsonString, tail, head);
}
}
return Json(0);
}
Json value(const char *data)
{
String string(data);
for(us8 i = 0; i < jsonSize; i++) {
if(key(i) == string) {
return value(i);
}
}
return Json(0);
}
int contains(const char *data)
{
String string(data);
for(s8 i = 0; i < jsonSize; i++) {
if(key(i) == string) {
return i;
}
}
return -1;
}
String toString()
{
s8 tail = tailLimit;
s8 head = headLimit + 1;
if((0 <= tailLimit) && (tailLimit < head)) {
if(head <= jsonString->length()) {
return jsonString->substring(tail, head);
}
}
return "";
}
private:
String *jsonString;
s8 tailLimit;
s8 headLimit;
JsonType jsonType;
us8 jsonSize;
s8 positions[maxPositions];
};
#endif // JSON_H