JsonExtractor.IterateAggregate

JSON object or array iterator.

Constructors

this
this(Parser json, Type start_type, Type end_type, bool skip_null)

Constructor

Members

Functions

setField
bool setField(uint i, Type type, cstring name, cstring value)

Abstract iteration method, must either use an appropriate GetField (or subclass) instance to handle and move the parser to the end of the field or indicate that this field is ignored and unhandled.

set_
void set_()

Invoked by super.set() to iterate over the JSON object or array. Expects the type of the current token to be - the start type if this.skip_null is false or - the start type or null if this.skip_null is true.

Variables

end_type
Type end_type;

Start and end token type, usually BeginObject/EndObject or BeginArray/EndArray.

exception
JsonException exception;

Exception throw to indicate errors during parsing.

start_type
Type start_type;

Start and end token type, usually BeginObject/EndObject or BeginArray/EndArray.

Inherited Members

From GetField

type
Type type;

Field type

value
cstring value;

Field value, meaningful only for certain types, especially Type.String and Type.Number. Corresponds to the value returned by JsonParser.value() for this field.

set
void set(Type type, cstring value)

Sets type and value for the field represented by this instance.

reset
void reset()

Resets type and value.

set_
void set_()

To be overridden, called when set() has finished.

reset_
void reset_()

To be overridden, called when reset() has finished.

Examples

1 enum content =
2 `{
3     "id":"8c97472e-098e-4baa-aa63-4a3f2aab10c6",
4     "imp":
5     [
6         {
7              "impid":"7682f6f1-810c-49b0-8388-f91ba4a00c1d",
8              "h":480,
9              "w":640,
10              "btype": [ 1,2,3 ],
11              "battr": [ 3,4,5 ]
12         },
13         {
14             "Hello": "World!"
15         },
16         12345
17     ],
18     "site":
19     {
20         "sid":"1",
21         "name":"MySite",
22         "pub":"MyPublisher",
23         "cat": [ "IAB1", "IAB2" ],
24         "page":"http://www.example.com/"
25     },
26     "bcat": null,
27     "user":
28     {
29         "uid":"45FB778",
30         "buyeruid":"100"
31     },
32     "device":
33     {
34         "ip":"192.168.0.1",
35         "ua":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 `
36            ~ `(KHTML, like Gecko) Chrome/12.0.742.53 Safari/534.30"
37     },
38     "cud":
39     {
40         "age":"23",
41         "gender":"female"
42     }
43 }`;
44 
45 auto t = new NamedTest("JsonExtractor");
46 
47 
48 scope json        = new Parser,
49       id          = new GetField,
50       impid       = new GetField,
51       page        = new GetField,
52       uid         = new GetField,
53       h           = new GetField,
54       w           = new GetField,
55       not         = new GetField,
56       site        = new GetObject(json, ["page": page]),
57       user        = new GetObject(json, ["uid": uid]),
58       imp_element = new GetObject(json, ["impid"[]: impid,
59                                          "w": w]),
60       bcat        = new GetObject(json, true, ["not":not]),
61       imp         = new GetArray(json, [imp_element],
62                                (uint i, Type type, cstring value)
63                                {
64                                    bool handled = i == 0;
65 
66                                    if (handled)
67                                    {
68                                        t.test!("==")(type,
69                                                      type.BeginObject);
70                                        imp_element.set(type);
71                                    }
72 
73                                    return handled;
74                                }),
75    main          = new Main(json, ["id"[]: id, "imp": imp,
76                                    "site": site, "user": user]);
77 
78 imp_element.addNamedField("h", h);
79 
80 bool ok = main.parse(content);
81 
82 t.test(ok, "parse didn't return true");
83 
84 t.test!("==")(id.type, Type.String);
85 t.test!("==")(id.value, "8c97472e-098e-4baa-aa63-4a3f2aab10c6"[]);
86 
87 t.test!("==")(impid.type, Type.String);
88 t.test!("==")(impid.value, "7682f6f1-810c-49b0-8388-f91ba4a00c1d"[]);
89 
90 t.test!("==")(page.type, Type.String);
91 t.test!("==")(page.value, "http://www.example.com/"[]);
92 
93 t.test!("==")(uid.type, Type.String);
94 t.test!("==")(uid.value, "45FB778"[]);
95 
96 t.test!("==")(not.type, Type.Empty);
97 t.test!("==")(not.value, ""[]);
98 
99 t.test!("==")(h.type, Type.Number);
100 t.test!("==")(h.value, "480"[]);
101 
102 t.test!("==")(w.type, Type.Number);
103 t.test!("==")(w.value, "640"[]);
104 
105 imp_element.removeNamedField("h");
106 h.reset();
107 
108 ok = main.parse(content);
109 
110 t.test(ok, "parse didn't return true"[]);
111 
112 t.test!("==")(h.type, Type.Empty);
113 t.test!("==")(h.value, ""[]);
114 
115 
116 ok = main.parse("{}");
117 
118 t.test(ok, "parse didn't return true"[]);
119 
120 t.test!("==")(id.value, ""[]);
121 t.test!("==")(id.type, Type.Empty);
122 
123 t.test!("==")(impid.value, ""[]);
124 t.test!("==")(impid.type, Type.Empty);
125 
126 t.test!("==")(page.value, ""[]);
127 t.test!("==")(page.type, Type.Empty);
128 
129 t.test!("==")(uid.value, ""[]);
130 t.test!("==")(uid.type, Type.Empty);
131 
132 t.test!("==")(not.type, Type.Empty);
133 t.test!("==")(not.value, ""[]);
134 
135 t.test!("==")(h.value, ""[]);
136 t.test!("==")(h.type, Type.Empty);
137 
138 t.test!("==")(w.value, ""[]);
139 t.test!("==")(w.type, Type.Empty);
140 
141 enum content2 = `{"imp":null}`;
142 
143 try
144 {
145     main.parse(content2);
146     t.test(false, "parse didn't throw"[]);
147 }
148 catch (JsonException e)
149 {
150     t.test!("==")(e.message(), "type mismatch"[]);
151 }
152 
153 bool fun (uint i, Type type, cstring value)
154 {
155     return false;
156 }
157 
158 scope imp2  = new GetArray(json, null, &fun, true),
159       main2 = new Main(json, ["imp": imp2]);
160 
161 ok = main2.parse(content2);
162 
163 t.test(ok, "parse didn't return true"[]);
164 

Meta