18
18
#include " sql_string.h"
19
19
#include " my_json_writer.h"
20
20
21
+ #ifndef NDEBUG
22
+ bool Json_writer::named_item_expected () const
23
+ {
24
+ return named_items_expectation.size ()
25
+ && named_items_expectation.back ();
26
+ }
27
+ #endif
28
+
21
29
void Json_writer::append_indent ()
22
30
{
23
31
if (!document_start)
@@ -26,9 +34,27 @@ void Json_writer::append_indent()
26
34
output.append (' ' );
27
35
}
28
36
29
- void Json_writer::start_object ()
37
+ inline void Json_writer::on_start_object ()
30
38
{
39
+ #ifndef NDEBUG
40
+ if (!is_on_fmt_helper_call)
41
+ {
42
+ DBUG_ASSERT (got_name == named_item_expected ());
43
+ named_items_expectation.push_back (true );
44
+ }
45
+
46
+ bool was_on_fmt_helper_call= is_on_fmt_helper_call;
47
+ is_on_fmt_helper_call= true ;
48
+ #endif
31
49
fmt_helper.on_start_object ();
50
+ #ifndef NDEBUG
51
+ is_on_fmt_helper_call= was_on_fmt_helper_call;
52
+ #endif
53
+ }
54
+
55
+ void Json_writer::start_object ()
56
+ {
57
+ on_start_object ();
32
58
33
59
if (!element_started)
34
60
start_element ();
@@ -38,11 +64,36 @@ void Json_writer::start_object()
38
64
first_child=true ;
39
65
element_started= false ;
40
66
document_start= false ;
67
+ #ifndef NDEBUG
68
+ got_name= false ;
69
+ #endif
70
+ }
71
+
72
+ bool Json_writer::on_start_array ()
73
+ {
74
+ #ifndef NDEBUG
75
+ bool was_on_fmt_helper_call= is_on_fmt_helper_call;
76
+ is_on_fmt_helper_call= true ;
77
+ #endif
78
+ bool helped= fmt_helper.on_start_array ();
79
+ #ifndef NDEBUG
80
+ is_on_fmt_helper_call= was_on_fmt_helper_call;
81
+ #endif
82
+ return helped;
41
83
}
42
84
43
85
void Json_writer::start_array ()
44
86
{
45
- if (fmt_helper.on_start_array ())
87
+ #ifndef NDEBUG
88
+ if (!is_on_fmt_helper_call)
89
+ {
90
+ DBUG_ASSERT (got_name == named_item_expected ());
91
+ named_items_expectation.push_back (false );
92
+ got_name= false ;
93
+ }
94
+ #endif
95
+
96
+ if (on_start_array ())
46
97
return ;
47
98
48
99
if (!element_started)
@@ -58,6 +109,11 @@ void Json_writer::start_array()
58
109
59
110
void Json_writer::end_object ()
60
111
{
112
+ #ifndef NDEBUG
113
+ named_items_expectation.pop_back ();
114
+ DBUG_ASSERT (!got_name);
115
+ got_name= false ;
116
+ #endif
61
117
indent_level-=INDENT_SIZE;
62
118
if (!first_child)
63
119
append_indent ();
@@ -68,6 +124,10 @@ void Json_writer::end_object()
68
124
69
125
void Json_writer::end_array ()
70
126
{
127
+ #ifndef NDEBUG
128
+ named_items_expectation.pop_back ();
129
+ got_name= false ;
130
+ #endif
71
131
if (fmt_helper.on_end_array ())
72
132
return ;
73
133
indent_level-=INDENT_SIZE;
@@ -80,31 +140,25 @@ void Json_writer::end_array()
80
140
Json_writer& Json_writer::add_member (const char *name)
81
141
{
82
142
size_t len= strlen (name);
83
- if (fmt_helper.on_add_member (name, len))
84
- return *this ; // handled
85
-
86
- // assert that we are in an object
87
- DBUG_ASSERT (!element_started);
88
- start_element ();
89
-
90
- output.append (' "' );
91
- output.append (name, len);
92
- output.append (" \" : " , 3 );
93
- return *this ;
143
+ return add_member (name, len);
94
144
}
95
145
96
146
Json_writer& Json_writer::add_member (const char *name, size_t len)
97
147
{
98
- if (fmt_helper.on_add_member (name, len))
99
- return *this ; // handled
100
-
101
- // assert that we are in an object
102
- DBUG_ASSERT (!element_started);
103
- start_element ();
148
+ if (!fmt_helper.on_add_member (name, len))
149
+ {
150
+ // assert that we are in an object
151
+ DBUG_ASSERT (!element_started);
152
+ start_element ();
104
153
105
- output.append (' "' );
106
- output.append (name, len);
107
- output.append (" \" : " );
154
+ output.append (' "' );
155
+ output.append (name, len);
156
+ output.append (" \" : " , 3 );
157
+ }
158
+ #ifndef NDEBUG
159
+ if (!is_on_fmt_helper_call)
160
+ got_name= true ;
161
+ #endif
108
162
return *this ;
109
163
}
110
164
@@ -200,19 +254,13 @@ void Json_writer::add_null()
200
254
void Json_writer::add_unquoted_str (const char * str)
201
255
{
202
256
size_t len= strlen (str);
203
- if (fmt_helper.on_add_str (str, len))
204
- return ;
205
-
206
- if (!element_started)
207
- start_element ();
208
-
209
- output.append (str, len);
210
- element_started= false ;
257
+ add_unquoted_str (str, len);
211
258
}
212
259
213
260
void Json_writer::add_unquoted_str (const char * str, size_t len)
214
261
{
215
- if (fmt_helper.on_add_str (str, len))
262
+ DBUG_ASSERT (is_on_fmt_helper_call || got_name == named_item_expected ());
263
+ if (on_add_str (str, len))
216
264
return ;
217
265
218
266
if (!element_started)
@@ -222,19 +270,24 @@ void Json_writer::add_unquoted_str(const char* str, size_t len)
222
270
element_started= false ;
223
271
}
224
272
273
+ inline bool Json_writer::on_add_str (const char *str, size_t num_bytes)
274
+ {
275
+ #ifndef NDEBUG
276
+ got_name= false ;
277
+ bool was_on_fmt_helper_call= is_on_fmt_helper_call;
278
+ is_on_fmt_helper_call= true ;
279
+ #endif
280
+ bool helped= fmt_helper.on_add_str (str, num_bytes);
281
+ #ifndef NDEBUG
282
+ is_on_fmt_helper_call= was_on_fmt_helper_call;
283
+ #endif
284
+ return helped;
285
+ }
286
+
225
287
void Json_writer::add_str (const char *str)
226
288
{
227
289
size_t len= strlen (str);
228
- if (fmt_helper.on_add_str (str, len))
229
- return ;
230
-
231
- if (!element_started)
232
- start_element ();
233
-
234
- output.append (' "' );
235
- output.append (str, len);
236
- output.append (' "' );
237
- element_started= false ;
290
+ add_str (str, len);
238
291
}
239
292
240
293
/*
@@ -243,7 +296,8 @@ void Json_writer::add_str(const char *str)
243
296
244
297
void Json_writer::add_str (const char * str, size_t num_bytes)
245
298
{
246
- if (fmt_helper.on_add_str (str, num_bytes))
299
+ DBUG_ASSERT (is_on_fmt_helper_call || got_name == named_item_expected ());
300
+ if (on_add_str (str, num_bytes))
247
301
return ;
248
302
249
303
if (!element_started)
0 commit comments