Toolkit to extract values from JSON content of an expected structure.
Usage example:
1 2 constcontent =
3 `{`4 `"id":"8c97472e-098e-4baa-aa63-4a3f2aab10c6",`5 `"imp":`6 `[`7 `{`8 `"impid":"7682f6f1-810c-49b0-8388-f91ba4a00c1d",`9 `"h":480,`10 `"w":640,`11 `"btype": [ 1,2,3 ],`12 `"battr": [ 3,4,5 ]`13 `}`14 `],`15 `"site":`16 `{`17 18 `"sid":"1",`19 `"name":"MySite",`20 `"pub":"MyPublisher",`21 `"cat": [ "IAB1", "IAB2" ],`22 `"page":"http://www.example.com/"`23 `},`24 `"user":`25 `{`26 `"uid":"45FB778",`27 `"buyeruid":"100"`28 `},`29 `"device":`30 `{`31 `"ip":"192.168.0.1",`32 `"ua":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 `33 `(KHTML, like Gecko) Chrome/12.0.742.53 Safari/534.30"`34 `},`35 `"cud":`36 `{`37 `"age":"23",`38 `"gender":"female"`39 `}`40 `}`;
41 42 // Aliases to avoid polluting this example with dozens of "JsonExtractor.".43 44 aliasJsonExtractor.ParserParser; // actually aliases JsonParserIter45 aliasJsonExtractor.GetFieldGetField;
46 aliasJsonExtractor.GetObjectGetObject;
47 aliasJsonExtractor.GetArrayGetArray;
48 aliasJsonExtractor.MainMain;
49 aliasJsonExtractor.TypeType; // actually aliases JsonParser.Token50 51 // Create JSON parser instance.52 53 Parserjson = newParser;
54 55 // Create one GetField instance for each JSON object field to extract.56 57 GetFieldid = newGetField,
58 impid = newGetField,
59 page = newGetField,
60 uid = newGetField,
61 h = newGetField,
62 w = newGetField;
63 64 // Create one GetObject instance for each JSON subobject that contains65 // fields to extract and pass an associative array of name/GetField66 // instance pairs to define the fields that should be extracted in this67 // subobject.68 69 GetObjectsite = newGetObject(json, ["page": page]),
70 user = newGetObject(json, ["uid": uid]),
71 // cast needed to prevent array type inference error72 imp_element = newGetObject(json, ["impid"[]: impid, "w": w, "h": h]);
73 74 75 // Create one IterateArray instance for each JSON array that contains76 // members to extract.77 78 GetArrayimp = newGetArray(json, [imp_element]
79 (uinti, Typetype, cstringvalue)
80 {
81 // This delegate will be called for each82 // "imp" array element with i as index. Note83 // that value is meaningful only if type is84 // type.String or type.Number.85 // We are interested in the first array86 // element only, which we expect to be an87 // object, so we call imp_element.set() when88 // i is 0. We return true if we handle the89 // element or false to make imp skip it.90 91 boolhandled = i == 0;
92 93 if (handled)
94 {
95 if (type == type.BeginObject)
96 {
97 imp_element.set(type);
98 }
99 elsethrownewException100 (
101 "\"imp\" array element is not an "102 "object as expected!"103 );
104 }
105 106 returnhandled;
107 });
108 109 // Create a Main (GetObject subclass) instance for the main JSON object and110 // pass the top level getters.111 112 Mainmain = newMain(json, ["id"[]: id, "imp": imp, "site": site,
113 "user": user]);
114 115 // Here we go.116 117 main.parse(content);
118 119 // id.type is now Type.String120 // id.value is now "8c97472e-098e-4baa-aa63-4a3f2aab10c6"121 122 // impid.type is now Type.String123 // impid.value is now "7682f6f1-810c-49b0-8388-f91ba4a00c1d"124 125 // page.type is now Type.String126 // page.value is now "http://www.example.com/"127 128 // uid.type is now Type.String129 // uid.value is now "45FB778"130 131 // h.type is now Type.Number132 // h.value is now "480"133 134 // w.type is now Type.Number135 // w.value is now "640"
Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
Alternatively, this file may be distributed under the terms of the Tango
3-Clause BSD License (see LICENSE_BSD.txt for details).
Toolkit to extract values from JSON content of an expected structure.
Usage example: