@@ -12,46 +12,44 @@ It's not well tested, but I thought to "formally" release it here in case someon
1212[spoiler]
1313If you wish to handle things more manually, you can use a thiner parser that is also faster:
1414
15- [code=ags] String json_string = "{ \"name\":\"John\", \"age\":30, \"car\":null }";
16- JsonParser* parser = new JsonParser;
17-
18- int token_count = 8;
19- JsonToken* t[] = JsonToken.NewArray(token_count);
20-
21- int r = parser.Parse(json_string, t, token_count);
22-
23- // now that you have the Tokens, you can use them to parse as you wish!
24- if (r < 0) Display("Failed to parse JSON: %d\n", r);
25- if (r < 1 || t[0].type != eJSON_Tok_OBJECT) Display("Object expected\n");
26-
27- for(int i=0; i<r ; i++){
28- JsonToken* tok = t[i];
29- Display(String.Format("%d ; %s ; %d ; %s ; %d ; %d ; %d",
30- i, tok.ToString(json_string), tok.size , tok.TypeAsString, tok.start , tok.end , tok.parent ));
31- }
32-
33- Display("JSON Parsing has FINISHED for string\n\n%s", json_string);[/code]
15+ [code=ags]String json_string = "{ \"name\":\"John\", \"age\":30, \"car\":null }";
16+ JsonParser* parser = new JsonParser;
17+
18+ int token_count = 8;
19+ JsonToken* t[] = JsonToken.NewArray(token_count);
20+
21+ int r = parser.Parse(json_string, t, token_count);
22+
23+ // now that you have the Tokens, you can use them to parse as you wish!
24+ if (r < 0) Display("Failed to parse JSON: %d\n", r);
25+ if (r < 1 || t[0].type != eJSON_Tok_OBJECT) Display("Object expected\n");
26+
27+ for(int i=0; i<r ; i++){
28+ JsonToken* tok = t[i];
29+ Display(String.Format("%d ; %s ; %d ; %s ; %d ; %d ; %d",
30+ i, tok.ToString(json_string), tok.size , tok.TypeAsString, tok.start , tok.end , tok.parent ));
31+ }
32+
33+ Display("JSON Parsing has FINISHED for string\n\n%s", json_string);[/code]
3434This module also packs a more approacheable (but less tested) parser:
3535
36- [code=ags]
37- String json_string = "";
38- json_string = json_string.Append("{\"squadName\":\"Super squad\",\"formed\":2016,\"active\":true,\"members\":[");
39- json_string = json_string.Append("{\"name\":\"Molecule Man\",\"age\":29,\"secretIdentity\":\"Dan Jukes\",\"powers\":[\"Radiation resistance\",\"Radiation blast\"]},");
40- json_string = json_string.Append("{\"name\":\"Madam Uppercut\",\"age\":39,\"secretIdentity\":\"Jane Wilson\",\"powers\":[\"Million punch\",\"Super reflexes\"]},");
41- json_string = json_string.Append("{\"name\":\"Eternal Flame\",\"age\":100,\"secretIdentity\":\"Unknown\",\"powers\":[\"Immortality\",\"Heat Immunity\",\"Interdimensional jump\"]}]}");
42-
43- MiniJsonParser jp;
44- jp.Init(json_string); // parse json_string and internally generate the tokens
45-
46- while(jp.NextToken()) // advance the current token and exit when there are no tokens left
47- {
48- if(jp.CurrentTokenIsLeaf) // usually the interesting information is on the leafs
49- {
50- Display(String.Format("%s: %s", jp.CurrentFullKey, jp.CurrentTokenAsString));
51- }
52- }
53-
54- Display("JSON Parsing has FINISHED for string\n\n%s", json_string);[/code][/spoiler]
36+ [code=ags]String json_string = ""; json_string = json_string.Append("{\"squadName\":\"Super squad\",\"formed\":2016,\"active\":true,\"members\":[");
37+ json_string = json_string.Append("{\"name\":\"Molecule Man\",\"age\":29,\"secretIdentity\":\"Dan Jukes\",\"powers\":[\"Radiation resistance\",\"Radiation blast\"]},");
38+ json_string = json_string.Append("{\"name\":\"Madam Uppercut\",\"age\":39,\"secretIdentity\":\"Jane Wilson\",\"powers\":[\"Million punch\",\"Super reflexes\"]},");
39+ json_string = json_string.Append("{\"name\":\"Eternal Flame\",\"age\":100,\"secretIdentity\":\"Unknown\",\"powers\":[\"Immortality\",\"Heat Immunity\",\"Interdimensional jump\"]}]}");
40+
41+ MiniJsonParser jp;
42+ jp.Init(json_string); // parse json_string and internally generate the tokens
43+
44+ while(jp.NextToken()) // advance the current token and exit when there are no tokens left
45+ {
46+ if(jp.CurrentTokenIsLeaf) // usually the interesting information is on the leafs
47+ {
48+ Display(String.Format("%s: %s", jp.CurrentFullKey, jp.CurrentTokenAsString));
49+ }
50+ }
51+
52+ Display("JSON Parsing has FINISHED for string\n\n%s", json_string);[/code][/spoiler]
5553[size=14pt][b]Script API[/b][/size]
5654
5755[spoiler]
0 commit comments