@@ -180,45 +180,88 @@ enum json_type json_object_get_type(struct json_object *jso)
180
180
return jso -> o_type ;
181
181
}
182
182
183
- /* json_object_to_json_string */
183
+ /* extended conversion to string */
184
+
185
+ const char * json_object_to_json_string_ext (struct json_object * jso , int flags )
186
+ {
187
+ if (!jso )
188
+ return "null" ;
189
+
190
+ if ((!jso -> _pb ) && !(jso -> _pb = printbuf_new ()))
191
+ return NULL ;
192
+
193
+ printbuf_reset (jso -> _pb );
194
+
195
+ if (jso -> _to_json_string (jso , jso -> _pb , 0 , flags ) < 0 )
196
+ return NULL ;
197
+
198
+ return jso -> _pb -> buf ;
199
+ }
200
+
201
+ /* backwards-compatible conversion to string */
184
202
185
203
const char * json_object_to_json_string (struct json_object * jso )
186
204
{
187
- if (!jso ) return "null" ;
188
- if (!jso -> _pb ) {
189
- if (!(jso -> _pb = printbuf_new ())) return NULL ;
190
- } else {
191
- printbuf_reset (jso -> _pb );
192
- }
193
- if (jso -> _to_json_string (jso , jso -> _pb ) < 0 ) return NULL ;
194
- return jso -> _pb -> buf ;
205
+ return json_object_to_json_string_ext (jso , JSON_C_TO_STRING_SPACED );
195
206
}
196
207
208
+ static void indent (struct printbuf * pb , int level , int flags )
209
+ {
210
+ if (flags & JSON_C_TO_STRING_PRETTY )
211
+ {
212
+ printbuf_memset (pb , -1 , ' ' , level * 2 );
213
+ }
214
+ }
197
215
198
216
/* json_object_object */
199
217
200
218
static int json_object_object_to_json_string (struct json_object * jso ,
201
- struct printbuf * pb )
202
- {
203
- int i = 0 ;
204
- struct json_object_iter iter ;
205
- sprintbuf (pb , "{" );
206
-
207
- /* CAW: scope operator to make ANSI correctness */
208
- /* CAW: switched to json_object_object_foreachC which uses an iterator struct */
209
- json_object_object_foreachC (jso , iter ) {
210
- if (i ) sprintbuf (pb , "," );
211
- sprintbuf (pb , " \"" );
212
- json_escape_str (pb , iter .key , strlen (iter .key ));
219
+ struct printbuf * pb ,
220
+ int level ,
221
+ int flags )
222
+ {
223
+ int had_children = 0 ;
224
+ struct json_object_iter iter ;
225
+
226
+ sprintbuf (pb , "{" /*}*/ );
227
+ if (flags & JSON_C_TO_STRING_PRETTY )
228
+ sprintbuf (pb , "\n" );
229
+ json_object_object_foreachC (jso , iter )
230
+ {
231
+ if (had_children )
232
+ {
233
+ sprintbuf (pb , "," );
234
+ if (flags & JSON_C_TO_STRING_PRETTY )
235
+ sprintbuf (pb , "\n" );
236
+ }
237
+ had_children = 1 ;
238
+ if (flags & JSON_C_TO_STRING_SPACED )
239
+ sprintbuf (pb , " " );
240
+ indent (pb , level + 1 , flags );
241
+ sprintbuf (pb , "\"" );
242
+ json_escape_str (pb , iter .key , strlen (iter .key ));
243
+ if (flags & JSON_C_TO_STRING_SPACED )
213
244
sprintbuf (pb , "\": " );
214
- if (iter .val == NULL ) sprintbuf (pb , "null" );
215
- else iter .val -> _to_json_string (iter .val , pb );
216
- i ++ ;
245
+ else
246
+ sprintbuf (pb , "\":" );
247
+ if (iter .val == NULL )
248
+ sprintbuf (pb , "null" );
249
+ else
250
+ iter .val -> _to_json_string (iter .val , pb , level + 1 ,flags );
217
251
}
218
-
219
- return sprintbuf (pb , " }" );
252
+ if (flags & JSON_C_TO_STRING_PRETTY )
253
+ {
254
+ if (had_children )
255
+ sprintbuf (pb , "\n" );
256
+ indent (pb ,level ,flags );
257
+ }
258
+ if (flags & JSON_C_TO_STRING_SPACED )
259
+ return sprintbuf (pb , /*{*/ " }" );
260
+ else
261
+ return sprintbuf (pb , /*{*/ "}" );
220
262
}
221
263
264
+
222
265
static void json_object_lh_entry_free (struct lh_entry * ent )
223
266
{
224
267
free (ent -> k );
@@ -291,7 +334,9 @@ void json_object_object_del(struct json_object* jso, const char *key)
291
334
/* json_object_boolean */
292
335
293
336
static int json_object_boolean_to_json_string (struct json_object * jso ,
294
- struct printbuf * pb )
337
+ struct printbuf * pb ,
338
+ int level ,
339
+ int flags )
295
340
{
296
341
if (jso -> o .c_boolean ) return sprintbuf (pb , "true" );
297
342
else return sprintbuf (pb , "false" );
@@ -327,7 +372,9 @@ json_bool json_object_get_boolean(struct json_object *jso)
327
372
/* json_object_int */
328
373
329
374
static int json_object_int_to_json_string (struct json_object * jso ,
330
- struct printbuf * pb )
375
+ struct printbuf * pb ,
376
+ int level ,
377
+ int flags )
331
378
{
332
379
return sprintbuf (pb , "%" PRId64 , jso -> o .c_int64 );
333
380
}
@@ -412,7 +459,9 @@ int64_t json_object_get_int64(struct json_object *jso)
412
459
/* json_object_double */
413
460
414
461
static int json_object_double_to_json_string (struct json_object * jso ,
415
- struct printbuf * pb )
462
+ struct printbuf * pb ,
463
+ int level ,
464
+ int flags )
416
465
{
417
466
return sprintbuf (pb , "%lf" , jso -> o .c_double );
418
467
}
@@ -449,7 +498,9 @@ double json_object_get_double(struct json_object *jso)
449
498
/* json_object_string */
450
499
451
500
static int json_object_string_to_json_string (struct json_object * jso ,
452
- struct printbuf * pb )
501
+ struct printbuf * pb ,
502
+ int level ,
503
+ int flags )
453
504
{
454
505
sprintbuf (pb , "\"" );
455
506
json_escape_str (pb , jso -> o .c_string .str , jso -> o .c_string .len );
@@ -511,20 +562,45 @@ int json_object_get_string_len(struct json_object *jso) {
511
562
/* json_object_array */
512
563
513
564
static int json_object_array_to_json_string (struct json_object * jso ,
514
- struct printbuf * pb )
515
- {
516
- int i ;
517
- sprintbuf (pb , "[" );
518
- for (i = 0 ; i < json_object_array_length (jso ); i ++ ) {
519
- struct json_object * val ;
520
- if (i ) { sprintbuf (pb , ", " ); }
521
- else { sprintbuf (pb , " " ); }
522
-
523
- val = json_object_array_get_idx (jso , i );
524
- if (val == NULL ) { sprintbuf (pb , "null" ); }
525
- else { val -> _to_json_string (val , pb ); }
526
- }
527
- return sprintbuf (pb , " ]" );
565
+ struct printbuf * pb ,
566
+ int level ,
567
+ int flags )
568
+ {
569
+ int had_children = 0 ;
570
+ int ii ;
571
+ sprintbuf (pb , "[" );
572
+ if (flags & JSON_C_TO_STRING_PRETTY )
573
+ sprintbuf (pb , "\n" );
574
+ for (ii = 0 ; ii < json_object_array_length (jso ); ii ++ )
575
+ {
576
+ struct json_object * val ;
577
+ if (had_children )
578
+ {
579
+ sprintbuf (pb , "," );
580
+ if (flags & JSON_C_TO_STRING_PRETTY )
581
+ sprintbuf (pb , "\n" );
582
+ }
583
+ had_children = 1 ;
584
+ if (flags & JSON_C_TO_STRING_SPACED )
585
+ sprintbuf (pb , " " );
586
+ indent (pb , level + 1 , flags );
587
+ val = json_object_array_get_idx (jso , ii );
588
+ if (val == NULL )
589
+ sprintbuf (pb , "null" );
590
+ else
591
+ val -> _to_json_string (val , pb , level + 1 , flags );
592
+ }
593
+ if (flags & JSON_C_TO_STRING_PRETTY )
594
+ {
595
+ if (had_children )
596
+ sprintbuf (pb , "\n" );
597
+ indent (pb ,level ,flags );
598
+ }
599
+
600
+ if (flags & JSON_C_TO_STRING_SPACED )
601
+ return sprintbuf (pb , " ]" );
602
+ else
603
+ return sprintbuf (pb , "]" );
528
604
}
529
605
530
606
static void json_object_array_entry_free (void * data )
0 commit comments