1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
/*
* Copyright (c) 2012, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include "murphy/config.h"
#include <murphy/common/macros.h>
#include <murphy/common/log.h>
#include <murphy/common/debug.h>
#include <murphy/common/json.h>
/** Type for a JSON parser. */
typedef struct json_tokener mrp_json_parser_t;
static mrp_json_parser_t *parser;
mrp_json_t *mrp_json_create(mrp_json_type_t type, ...)
{
mrp_json_t *o;
const char *s;
bool b;
int i, l;
double d;
va_list ap;
va_start(ap, type);
switch (type) {
case MRP_JSON_STRING:
s = va_arg(ap, const char *);
l = va_arg(ap, int);
if (l < 0)
o = json_object_new_string(s);
else
o = json_object_new_string_len(s, l);
break;
case MRP_JSON_BOOLEAN:
b = va_arg(ap, int);
o = json_object_new_boolean(b);
break;
case MRP_JSON_INTEGER:
i = va_arg(ap, int);
o = json_object_new_int(i);
break;
case MRP_JSON_DOUBLE:
d = va_arg(ap, double);
o = json_object_new_double(d);
break;
case MRP_JSON_OBJECT:
o = json_object_new_object();
break;
case MRP_JSON_ARRAY:
o = json_object_new_array();
break;
default:
o = NULL;
}
va_end(ap);
return o;
}
mrp_json_t *mrp_json_clone(mrp_json_t *o)
{
if (o != NULL)
return mrp_json_string_to_object(mrp_json_object_to_string(o), -1);
else
return NULL;
}
mrp_json_t *mrp_json_string_to_object(const char *s, int len)
{
if (parser == NULL) {
parser = json_tokener_new();
if (parser == NULL)
return NULL;
}
else
json_tokener_reset(parser);
if (len < 0)
len = strlen(s);
return json_tokener_parse_ex(parser, s, len);
}
const char *mrp_json_object_to_string(mrp_json_t *o)
{
if (o != NULL)
return json_object_to_json_string(o);
else
return "{}";
}
mrp_json_t *mrp_json_ref(mrp_json_t *o)
{
return json_object_get(o);
}
void mrp_json_unref(mrp_json_t *o)
{
json_object_put(o);
}
mrp_json_type_t mrp_json_get_type(mrp_json_t *o)
{
return json_object_get_type(o);
}
int mrp_json_is_type(mrp_json_t *o, mrp_json_type_t type)
{
return json_object_is_type(o, type);
}
void mrp_json_add(mrp_json_t *o, const char *key, mrp_json_t *m)
{
json_object_object_add(o, key, m);
}
mrp_json_t *mrp_json_add_member(mrp_json_t *o, const char *key,
mrp_json_type_t type, ...)
{
mrp_json_t *m;
const char *s;
bool b;
int i, l;
double d;
va_list ap;
va_start(ap, type);
switch (type) {
case MRP_JSON_STRING:
s = va_arg(ap, const char *);
l = va_arg(ap, int);
if (l < 0)
m = json_object_new_string(s);
else
m = json_object_new_string_len(s, l);
break;
case MRP_JSON_BOOLEAN:
b = va_arg(ap, int);
m = json_object_new_boolean(b);
break;
case MRP_JSON_INTEGER:
i = va_arg(ap, int);
m = json_object_new_int(i);
break;
case MRP_JSON_DOUBLE:
d = va_arg(ap, double);
m = json_object_new_double(d);
break;
case MRP_JSON_OBJECT:
m = json_object_new_object();
break;
case MRP_JSON_ARRAY:
m = json_object_new_array();
break;
default:
m = NULL;
errno = EINVAL;
}
va_end(ap);
if (m != NULL)
json_object_object_add(o, key, m);
return m;
}
mrp_json_t *mrp_json_add_array(mrp_json_t *o, const char *key,
mrp_json_type_t type, ...)
{
va_list ap;
void *arr;
size_t cnt, i;
mrp_json_t *a;
va_start(ap, type);
arr = va_arg(ap, void *);
cnt = va_arg(ap, size_t);
a = mrp_json_create(MRP_JSON_ARRAY);
if (a == NULL)
goto fail;
switch (type) {
case MRP_JSON_STRING:
for (i = 0; i < cnt; i++) {
if (!mrp_json_array_append_string(a, ((char **)arr)[i]))
goto fail;
}
break;
case MRP_JSON_INTEGER:
for (i = 0; i < cnt; i++) {
if (!mrp_json_array_append_integer(a, ((int *)arr)[i]))
goto fail;
}
break;
case MRP_JSON_DOUBLE:
for (i = 0; i < cnt; i++) {
if (!mrp_json_array_append_double(a, ((double *)arr)[i]))
goto fail;
}
break;
case MRP_JSON_BOOLEAN:
for (i = 0; i < cnt; i++) {
if (!mrp_json_array_append_boolean(a, ((bool *)arr)[i]))
goto fail;
}
break;
default:
goto fail;
}
va_end(ap);
mrp_json_add(o, key, a);
return a;
fail:
va_end(ap);
mrp_json_unref(a);
return NULL;
}
mrp_json_t *mrp_json_get(mrp_json_t *o, const char *key)
{
mrp_json_iter_t it;
const char *k;
mrp_json_t *v;
mrp_json_foreach_member(o, k, v, it) {
if (!strcmp(k, key))
return v;
}
return NULL;
}
int mrp_json_get_member(mrp_json_t *o, const char *key,
mrp_json_type_t type, ...)
{
const char **s;
bool *b;
int *i;
double *d;
mrp_json_t *m, **mp;
int success;
va_list ap;
success = FALSE;
va_start(ap, type);
m = mrp_json_get(o, key);
if (m != NULL) {
if (json_object_is_type(m, type)) {
success = TRUE;
switch (type) {
case MRP_JSON_STRING:
s = va_arg(ap, const char **);
*s = json_object_get_string(m);
break;
case MRP_JSON_BOOLEAN:
b = va_arg(ap, bool *);
*b = json_object_get_boolean(m);
break;
case MRP_JSON_INTEGER:
i = va_arg(ap, int *);
*i = json_object_get_int(m);
break;
case MRP_JSON_DOUBLE:
d = va_arg(ap, double *);
*d = json_object_get_double(m);
break;
case MRP_JSON_OBJECT:
mp = va_arg(ap, mrp_json_t **);
*mp = m;
break;
case MRP_JSON_ARRAY:
mp = va_arg(ap, mrp_json_t **);
*mp = m;
break;
default:
success = FALSE;
}
}
else
errno = EINVAL;
}
else {
errno = ENOENT;
success = FALSE;
}
va_end(ap);
return success;
}
void mrp_json_del_member(mrp_json_t *o, const char *key)
{
json_object_object_del(o, key);
}
int mrp_json_array_length(mrp_json_t *a)
{
return json_object_array_length(a);
}
int mrp_json_array_append(mrp_json_t *a, mrp_json_t *v)
{
return json_object_array_add(a, v) == 0;
}
mrp_json_t *mrp_json_array_append_item(mrp_json_t *a, mrp_json_type_t type, ...)
{
mrp_json_t *v;
const char *s;
bool b;
int i, l;
double d;
va_list ap;
va_start(ap, type);
switch (type) {
case MRP_JSON_STRING:
s = va_arg(ap, const char *);
l = va_arg(ap, int);
if (l < 0)
v = json_object_new_string(s);
else
v = json_object_new_string_len(s, l);
break;
case MRP_JSON_BOOLEAN:
b = va_arg(ap, int);
v = json_object_new_boolean(b);
break;
case MRP_JSON_INTEGER:
i = va_arg(ap, int);
v = json_object_new_int(i);
break;
case MRP_JSON_DOUBLE:
d = va_arg(ap, double);
v = json_object_new_double(d);
break;
case MRP_JSON_OBJECT:
v = va_arg(ap, mrp_json_t *);
break;
case MRP_JSON_ARRAY:
v = va_arg(ap, mrp_json_t *);
break;
default:
v = NULL;
errno = EINVAL;
}
va_end(ap);
if (v != NULL) {
if (json_object_array_add(a, v) == 0)
return v;
else {
mrp_json_unref(v);
errno = ENOMEM;
}
}
return NULL;
}
int mrp_json_array_set(mrp_json_t *a, int idx, mrp_json_t *v)
{
return json_object_array_put_idx(a, idx, v);
}
int mrp_json_array_set_item(mrp_json_t *a, int idx, mrp_json_type_t type, ...)
{
mrp_json_t *v;
const char *s;
bool b;
int i, l;
double d;
va_list ap;
va_start(ap, type);
switch (type) {
case MRP_JSON_STRING:
s = va_arg(ap, const char *);
l = va_arg(ap, int);
if (l < 0)
v = json_object_new_string(s);
else
v = json_object_new_string_len(s, l);
break;
case MRP_JSON_BOOLEAN:
b = va_arg(ap, int);
v = json_object_new_boolean(b);
break;
case MRP_JSON_INTEGER:
i = va_arg(ap, int);
v = json_object_new_int(i);
break;
case MRP_JSON_DOUBLE:
d = va_arg(ap, double);
v = json_object_new_double(d);
break;
case MRP_JSON_OBJECT:
v = va_arg(ap, mrp_json_t *);
break;
case MRP_JSON_ARRAY:
v = va_arg(ap, mrp_json_t *);
break;
default:
v = NULL;
errno = EINVAL;
}
va_end(ap);
if (v != NULL)
return json_object_array_put_idx(a, idx, v);
else {
errno = ENOMEM;
return FALSE;
}
}
mrp_json_t *mrp_json_array_get(mrp_json_t *a, int idx)
{
return json_object_array_get_idx(a, idx);
}
int mrp_json_array_get_item(mrp_json_t *a, int idx, mrp_json_type_t type, ...)
{
const char **s;
bool *b;
int *i;
double *d;
mrp_json_t *v, **vp;
int success;
va_list ap;
success = FALSE;
va_start(ap, type);
v = json_object_array_get_idx(a, idx);
if (v != NULL) {
if (json_object_is_type(v, type)) {
success = TRUE;
switch (type) {
case MRP_JSON_STRING:
s = va_arg(ap, const char **);
*s = json_object_get_string(v);
break;
case MRP_JSON_BOOLEAN:
b = va_arg(ap, bool *);
*b = json_object_get_boolean(v);
break;
case MRP_JSON_INTEGER:
i = va_arg(ap, int *);
*i = json_object_get_int(v);
break;
case MRP_JSON_DOUBLE:
d = va_arg(ap, double *);
*d = json_object_get_double(v);
break;
case MRP_JSON_OBJECT:
vp = va_arg(ap, mrp_json_t **);
*vp = v;
break;
case MRP_JSON_ARRAY:
vp = va_arg(ap, mrp_json_t **);
*vp = v;
break;
default:
success = FALSE;
errno = EINVAL;
}
}
else
errno = EINVAL;
}
else
errno = ENOENT;
va_end(ap);
return success;
}
int mrp_json_parse_object(char **strp, int *lenp, mrp_json_t **op)
{
char *str;
int len;
mrp_json_t *o = NULL;
json_tokener *tok = NULL;
int res = -1;
if (strp == NULL || *strp == NULL) {
*op = NULL;
if (lenp != NULL)
*lenp = 0;
return 0;
}
str = *strp;
len = lenp ? *lenp : 0;
if (len <= 0)
len = strlen(str);
tok = json_tokener_new();
if (tok != NULL) {
o = json_tokener_parse_ex(tok, str, len);
if (o != NULL) {
*strp += tok->char_offset;
if (lenp != NULL)
*lenp -= tok->char_offset;
res = 0;
}
else {
#ifdef HAVE_JSON_TOKENER_GET_ERROR
if (json_tokener_get_error(tok) != json_tokener_success)
errno = EINVAL;
#else
if (tok->err != json_tokener_success)
errno = EINVAL;
#endif
else
res = 0;
}
json_tokener_free(tok);
}
else
errno = ENOMEM;
*op = o;
return res;
}
|