-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonObject.java
More file actions
325 lines (274 loc) · 7.85 KB
/
JsonObject.java
File metadata and controls
325 lines (274 loc) · 7.85 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package JoweCore.Middle.Base.Json;
import JoweCore.Inner.Base.StringUtil;
import java.lang.Collection;
import User;
import JoweCore.Middle.Base.Http.Http;
//:#cplusplus
//:$
//: #include <stdio.h>
//: #include <stdlib.h>
//: #include <string.h>
//: #include "cJSON.h"
//:$
public class JsonObject extends Object {
private Unknown cJSONdelegate;
public static JsonObject parse(String js){
JsonObject ret;
ret = new JsonObject();
if(ret.setJson(js)){
return ret;
}
return null;
}
private void setDelegate(Unknown delegate){
this.cJSONdelegate = delegate;
}
private boolean setJson(String del){
//:$
//: cJSON *j;
//: j = cJSON_Parse(kStrToChar($del$));
//:$
//: self:cJSONdelegate := $kPtrToObj(j)$
return this.cJSONdelegate != null;
}
public String stringValue(){
String ret;
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: char *c = j->valuestring
//:
//:$
//: ret := $kPtrToStr(c, strlen(c))$;
return ret;
}
public double doubleValue(){
double ret;
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: double c = j->valuedouble
//:
//:$
//: ret := $kDoubleToNum(c)$;
return ret;
}
public int intValue(){
int ret;
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: int c = j->valueint
//:
//:$
//: ret := $kIntToNum(c)$;
return ret;
}
public int booleanValue(){
return this.intValue() == 1;
}
public JsonObject getItem(String name){
JsonObject ret;
Unknown childDelegate;
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON *child = cJSON_GetObjectItem(j, kStrToChar($name$))
//:$
//: childDelegate := $kPtrToObj(child)$
if(childDelegate == null){
return null;
}
ret = JsonObject.neew();
ret.setDelegate(childDelegate);
return ret;
}
public JsonObject getAtArrayIndex(int i){
JsonObject ret;
Unknown childDelegate;
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON *child = cJSON_GetArrayItem(j, kNumToInt($i$))
//:$
//: childDelegate := $kPtrToObj(child)$
if(childDelegate == null){
return null;
}
ret = JsonObject.neew();
ret.setDelegate(childDelegate);
return ret;
}
public int arraySize(){
int ret;
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: int c = cJSON_GetArraySize(j)
//:
//:$
//: ret := $kIntToNum(c)$;
return ret;
}
/**
* Evaluates a path on i.e. form "myObj.subObj.[3].child" by traversing recursively.
* path - the path to evaluate. Includes list [] and delimiter
* del - the delimiter to use between parent / child. Useful if key contains the default delimiter "."
*/
public JsonObject eval(String path, String del){
JsonObject ret;
Collection delimiter;
String currentName;
delimiter = Collection.neew();
if(del == null){
del = ".";
}
delimiter.add(del);
Collection itemNames;
ret = this;
itemNames = StringUtil.split(path, delimiter);
int i;
for(i = 1; i <= itemNames.size(); i++){
currentName = itemNames.getAt(i);
User.tell("At: " + currentName);
if(currentName.beginsWith("[") && currentName.endsWith("]")){
int x;
x = currentName.replace("[", "").replace("]", "").asInteger();
ret = ret.getAtArrayIndex(x);
}else{
ret = ret.getItem(itemNames.getAt(i));
}
}
return ret;
}
public String jsonText(){
String ret;
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: char *c = cJSON_PrintUnformatted(j)
//:
//:$
//: ret := $kPtrToStr(c, strlen(c))$;
//: $ free(c); $
return ret;
}
public static JsonObject rootObject(){
JsonObject ret;
ret = JsonObject.neew();
//: ret:setDelegate($kPtrToObj(cJSON_CreateObject())$);
return ret;
}
public static JsonObject rootArray(){
JsonObject ret;
ret = JsonObject.neew();
//: ret:setDelegate($kPtrToObj(cJSON_CreateArray())$);
return ret;
}
public void addString(String name, String value){
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON_AddStringToObject(j, kStrToChar($name$), kStrToChar($value$))
//:$
}
public void addInt(String name, int value){
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON_AddNumberToObject(j, kStrToChar($name$), kNumToInt($value$))
//:$
}
public void addDouble(String name, double value){
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON_AddNumberToObject(j, kStrToChar($name$), kNumToDouble($value$))
//:$
}
public void addTrue(String name){
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON_AddTrueToObject(j, kStrToChar($name$))
//:$
}
public void addFalse(String name){
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON_AddFalseToObject(j, kStrToChar($name$))
//:$
}
public void addBoolean(String name, boolean b){
if(b){
this.addTrue(name);
}else{
this.addFalse(name);
}
}
public void addNull(String name){
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON_AddNullToObject(j, kStrToChar($name$))
//:$
}
public JsonObject addObject(String name){
JsonObject ret;
ret = JsonObject.neew();
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON *child = cJSON_CreateObject();
//: cJSON_AddItemToObject(j, kStrToChar($name$), child);
//:$
//: ret:setDelegate($kPtrToObj(child)$);
return ret;
}
public JsonObject addArray(String name){
JsonObject ret;
ret = JsonObject.neew();
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON *child = cJSON_CreateArray();
//: cJSON_AddItemToObject(j, kStrToChar($name$), child);
//:$
//: ret:setDelegate($kPtrToObj(child)$);
return ret;
}
public JsonObject addObjectToArray(){
JsonObject ret;
ret = JsonObject.neew();
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON *child = cJSON_CreateObject();
//: cJSON_AddItemToArray(j, child);
//:$
//: ret:setDelegate($kPtrToObj(child)$);
return ret;
}
public void free(){
//:$
//: cJSON *j = (cJSON *) kObjToPtr( $self:cJSONdelegate$ );
//: cJSON_Delete(j);
//:$
this.cJSONdelegate = null;
}
public static void test(){
JsonObject.testJsonConstruct();
}
public static void testGet(){
JsonObject jsObj;
jsObj = JsonObject.parse(Http.get("http://localhost:2000/api/person/8006130135?mock=true"));
User.tell(
"name = " + jsObj.getItem("firstName").stringValue() + "\n" +
"secret = " + jsObj.getItem("secretIdentity").booleanValue() + "\n" +
"street = " + jsObj.eval("address.street").stringValue()
);
}
public static void testJsonConstruct(){
JsonObject jsObj;
jsObj = JsonObject.rootObject();
jsObj.addString("name", "Billy");
jsObj.addInt("age", 34);
jsObj.addDouble("length", 1.73);
jsObj.addTrue("hasDaughter");
jsObj.addFalse("hasSon");
jsObj.addBoolean("hasCat", true);
jsObj.addBoolean("hasDog", false);
jsObj.addNull("cares_about_sports");
jsObj.addObject("spouse").addString("name", "Maria");
jsObj.addArray("children").addObjectToArray().addString("name", "Alva");
jsObj.getItem("children").addObjectToArray().addString("name", "Viggo");
jsObj.eval("children.[1]").addObject("room").addString("color","green"); // Index starts at 0
User.tell("json : " + jsObj.jsonText());
Http.post("http://localhost:2000/person", jsObj.jsonText());
}
}