ocean.text.json.JsonExtractor

Toolkit to extract values from JSON content of an expected structure.

Usage example:

1 
2 const content =
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 alias JsonExtractor.Parser    Parser;   // actually aliases JsonParserIter
45 alias JsonExtractor.GetField  GetField;
46 alias JsonExtractor.GetObject GetObject;
47 alias JsonExtractor.GetArray  GetArray;
48 alias JsonExtractor.Main      Main;
49 alias JsonExtractor.Type      Type;     // actually aliases JsonParser.Token
50 
51 // Create JSON parser instance.
52 
53 Parser json = new Parser;
54 
55 // Create one GetField instance for each JSON object field to extract.
56 
57 GetField id    = new GetField,
58          impid = new GetField,
59          page  = new GetField,
60          uid   = new GetField,
61          h     = new GetField,
62          w     = new GetField;
63 
64 // Create one GetObject instance for each JSON subobject that contains
65 // fields to extract and pass an associative array of name/GetField
66 // instance pairs to define the fields that should be extracted in this
67 // subobject.
68 
69 GetObject site = new GetObject(json, ["page": page]),
70           user = new GetObject(json, ["uid": uid]),
71                         // cast needed to prevent array type inference error
72    imp_element = new GetObject(json, ["impid"[]: impid, "w": w, "h": h]);
73 
74 
75 // Create one IterateArray instance for each JSON array that contains
76 // members to extract.
77 
78 GetArray imp = new GetArray(json, [imp_element]
79                            (uint i, Type type, cstring value)
80                            {
81                                // This delegate will be called for each
82                                // "imp" array element with i as index. Note
83                                // that value is meaningful only if type is
84                                // type.String or type.Number.
85                                // We are interested in the first array
86                                // element only, which we expect to be an
87                                // object, so we call imp_element.set() when
88                                // i is 0. We return true if we handle the
89                                // element or false to make imp skip it.
90 
91                                bool handled = i == 0;
92 
93                                if (handled)
94                                {
95                                    if (type == type.BeginObject)
96                                    {
97                                        imp_element.set(type);
98                                    }
99                                    else throw new Exception
100                                    (
101                                        "\"imp\" array element is not an "
102                                        "object as expected!"
103                                    );
104                                }
105 
106                                return handled;
107                            });
108 
109 // Create a Main (GetObject subclass) instance for the main JSON object and
110 // pass the top level getters.
111 
112 Main main = new Main(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.String
120 // id.value is now "8c97472e-098e-4baa-aa63-4a3f2aab10c6"
121 
122 // impid.type  is now Type.String
123 // impid.value is now "7682f6f1-810c-49b0-8388-f91ba4a00c1d"
124 
125 // page.type  is now Type.String
126 // page.value is now "http://www.example.com/"
127 
128 // uid.type  is now Type.String
129 // uid.value is now "45FB778"
130 
131 // h.type  is now Type.Number
132 // h.value is now "480"
133 
134 // w.type  is now Type.Number
135 // w.value is now "640"

Members

Structs

JsonExtractor
struct JsonExtractor
Undocumented in source.

Meta

License

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).