blob: 9dad8fb089933a870d8d1bc5600b04d13d57ed37 [file] [log] [blame]
Radek Krejci5449d472015-10-26 14:35:56 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON data parser for libyang
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci5449d472015-10-26 14:35:56 +010013 */
14
Radek Krejci9a5daea2016-03-02 16:49:40 +010015#define _GNU_SOURCE
Radek Krejci5449d472015-10-26 14:35:56 +010016#include <assert.h>
17#include <ctype.h>
18#include <limits.h>
Michal Vasko1e676ca2016-06-24 15:23:54 +020019#include <errno.h>
Radek Krejci5449d472015-10-26 14:35:56 +010020#include <stdlib.h>
21#include <string.h>
22
23#include "libyang.h"
24#include "common.h"
25#include "context.h"
26#include "parser.h"
27#include "printer.h"
28#include "tree_internal.h"
29#include "validation.h"
Radek Krejci9a5daea2016-03-02 16:49:40 +010030#include "xml_internal.h"
Radek Krejci5449d472015-10-26 14:35:56 +010031
Radek Krejci5449d472015-10-26 14:35:56 +010032static int
33lyjson_isspace(int c)
34{
35 switch(c) {
36 case 0x20: /* space */
37 case 0x09: /* horizontal tab */
38 case 0x0a: /* line feed or new line */
39 case 0x0d: /* carriage return */
40 return 1;
41 default:
42 return 0;
43 }
44}
45
46static unsigned int
47skip_ws(const char *data)
48{
49 unsigned int len = 0;
50
51 /* skip leading whitespaces */
52 while (data[len] && lyjson_isspace(data[len])) {
Radek Krejci5449d472015-10-26 14:35:56 +010053 len++;
54 }
55
56 return len;
57}
58
Radek Krejci5449d472015-10-26 14:35:56 +010059static char *
60lyjson_parse_text(const char *data, unsigned int *len)
61{
62#define BUFSIZE 1024
63
64 char buf[BUFSIZE];
65 char *result = NULL, *aux;
Radek Krejci967e4bf2015-11-28 10:06:40 +010066 int o, size = 0;
Radek Krejci5449d472015-10-26 14:35:56 +010067 unsigned int r, i;
68 int32_t value;
69
70 for (*len = o = 0; data[*len] && data[*len] != '"'; o++) {
71 if (o > BUFSIZE - 3) {
72 /* add buffer into the result */
73 if (result) {
74 size = size + o;
Michal Vasko253035f2015-12-17 16:58:13 +010075 aux = ly_realloc(result, size + 1);
76 if (!aux) {
77 LOGMEM;
78 return NULL;
79 }
Radek Krejci5449d472015-10-26 14:35:56 +010080 result = aux;
81 } else {
82 size = o;
83 result = malloc((size + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +010084 if (!result) {
85 LOGMEM;
86 return NULL;
87 }
Radek Krejci5449d472015-10-26 14:35:56 +010088 }
89 memcpy(&result[size - o], buf, o);
90
91 /* write again into the beginning of the buffer */
92 o = 0;
93 }
94
95 if (data[*len] == '\\') {
96 /* parse escape sequence */
97 (*len)++;
98 i = 1;
99 switch (data[(*len)]) {
100 case '"':
101 /* quotation mark */
102 value = 0x22;
103 break;
104 case '\\':
105 /* reverse solidus */
106 value = 0x5c;
107 break;
108 case '/':
109 /* solidus */
110 value = 0x2f;
111 break;
112 case 'b':
113 /* backspace */
114 value = 0x08;
115 break;
116 case 'f':
117 /* form feed */
118 value = 0x0c;
119 break;
120 case 'n':
121 /* line feed */
122 value = 0x0a;
123 break;
124 case 'r':
125 /* carriage return */
126 value = 0x0d;
127 break;
128 case 't':
129 /* tab */
130 value = 0x09;
131 break;
132 case 'u':
133 /* Basic Multilingual Plane character \uXXXX */
134 (*len)++;
135 for (value = i = 0; i < 4; i++) {
136 if (isdigit(data[(*len) + i])) {
137 r = (data[(*len) + i] - '0');
138 } else if (data[(*len) + i] > 'F') {
139 r = 10 + (data[(*len) + i] - 'a');
140 } else {
141 r = 10 + (data[(*len) + i] - 'A');
142 }
143 value = (16 * value) + r;
144 }
145 break;
146 default:
147 /* invalid escape sequence */
Radek Krejci48464ed2016-03-17 15:44:09 +0100148 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character escape sequence");
Radek Krejci5449d472015-10-26 14:35:56 +0100149 goto error;
150
151 }
Radek Krejci48464ed2016-03-17 15:44:09 +0100152 r = pututf8(&buf[o], value);
Radek Krejci5449d472015-10-26 14:35:56 +0100153 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100154 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character UTF8 character");
Radek Krejci5449d472015-10-26 14:35:56 +0100155 goto error;
156 }
157 o += r - 1; /* o is ++ in for loop */
158 (*len) += i; /* number of read characters */
zyinter20087fa65732016-11-17 10:55:12 +0800159 } else if ((data[*len] >= 0 && data[*len] < 0x20) || data[*len] == 0x5c) {
Radek Krejci5449d472015-10-26 14:35:56 +0100160 /* control characters must be escaped */
Radek Krejci48464ed2016-03-17 15:44:09 +0100161 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "control character (unescaped)");
Radek Krejci5449d472015-10-26 14:35:56 +0100162 goto error;
163 } else {
164 /* unescaped character */
Radek Krejcideee60e2016-09-23 15:21:14 +0200165 r = copyutf8(&buf[o], &data[*len]);
166 if (!r) {
167 goto error;
168 }
169
170 o += r - 1; /* o is ++ in for loop */
171 (*len) += r;
Radek Krejci5449d472015-10-26 14:35:56 +0100172 }
173 }
174
175#undef BUFSIZE
176
177 if (o) {
178 if (result) {
179 size = size + o;
Michal Vasko253035f2015-12-17 16:58:13 +0100180 aux = ly_realloc(result, size + 1);
181 if (!aux) {
182 LOGMEM;
183 return NULL;
184 }
Radek Krejci5449d472015-10-26 14:35:56 +0100185 result = aux;
186 } else {
187 size = o;
188 result = malloc((size + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +0100189 if (!result) {
190 LOGMEM;
191 return NULL;
192 }
Radek Krejci5449d472015-10-26 14:35:56 +0100193 }
194 memcpy(&result[size - o], buf, o);
195 }
196 if (result) {
197 result[size] = '\0';
198 } else {
199 size = 0;
200 result = strdup("");
201 }
202
203 return result;
204
205error:
206 free(result);
207 return NULL;
208}
209
210static unsigned int
211lyjson_parse_number(const char *data)
212{
Michal Vasko8f32c112016-05-18 13:22:59 +0200213 unsigned int len = 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100214
Michal Vasko8f32c112016-05-18 13:22:59 +0200215 if (data[len] == '-') {
216 ++len;
217 }
Radek Krejci5449d472015-10-26 14:35:56 +0100218
Michal Vasko8f32c112016-05-18 13:22:59 +0200219 if (data[len] == '0') {
220 ++len;
221 } else if (isdigit(data[len])) {
222 ++len;
223 while (isdigit(data[len])) {
224 ++len;
225 }
226 } else {
227 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]);
228 return 0;
229 }
230
231 if (data[len] == '.') {
232 ++len;
233 if (!isdigit(data[len])) {
234 if (data[len]) {
235 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]);
236 } else {
237 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value (EOF).");
Radek Krejci5449d472015-10-26 14:35:56 +0100238 }
Radek Krejci5449d472015-10-26 14:35:56 +0100239 return 0;
240 }
Michal Vasko8f32c112016-05-18 13:22:59 +0200241 while (isdigit(data[len])) {
242 ++len;
243 }
244 }
245
Michal Vasko1e676ca2016-06-24 15:23:54 +0200246 if ((data[len] == 'e') || (data[len] == 'E')) {
247 ++len;
248 if ((data[len] == '+') || (data[len] == '-')) {
249 ++len;
250 }
251 while (isdigit(data[len])) {
252 ++len;
253 }
254 }
255
Michal Vasko8f32c112016-05-18 13:22:59 +0200256 if (data[len] && (data[len] != ',') && (data[len] != ']') && (data[len] != '}') && !lyjson_isspace(data[len])) {
257 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]);
258 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100259 }
260
261 return len;
262}
263
Michal Vasko1e676ca2016-06-24 15:23:54 +0200264static char *
265lyjson_convert_enumber(const char *number, unsigned int num_len, char *e_ptr)
266{
267 char *ptr, *num;
268 const char *number_ptr;
269 long int e_val;
270 int dot_pos, chars_to_dot, minus;
271 unsigned int num_len_no_e;
272
273 if (*number == '-') {
274 minus = 1;
275 ++number;
276 --num_len;
277 } else {
278 minus = 0;
279 }
280
281 num_len_no_e = e_ptr - number;
282
283 errno = 0;
284 ++e_ptr;
285 e_val = strtol(e_ptr, &ptr, 10);
286 if (errno) {
287 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Exponent out-of-bounds in a JSON Number value (%.*s).",
288 num_len - (e_ptr - number), e_ptr);
289 return NULL;
290 } else if (ptr != number + num_len) {
291 /* we checked this already */
292 LOGINT;
293 return NULL;
294 }
295
296 if ((ptr = strnchr(number, '.', num_len_no_e))) {
297 dot_pos = ptr - number;
298 } else {
299 dot_pos = num_len_no_e;
300 }
301
302 dot_pos += e_val;
303
304 /* allocate enough memory */
305 if (dot_pos < 1) {
306 /* (.XXX)XXX[.]XXXX */
307 num = malloc((minus ? 1 : 0) + -dot_pos + 2 + (num_len_no_e - (ptr ? 1 : 0)) + 1);
308 } else if (dot_pos < (signed)num_len_no_e) {
309 /* XXXX(.)XX.XXX */
310 num = malloc((minus ? 1 : 0) + num_len_no_e + (ptr ? 0 : 1) + 1);
311 } else {
312 /* XXX[.]XXXX(XXX.) */
313 num = malloc((minus ? 1 : 0) + (dot_pos - (ptr ? 2 : 1)) + 1);
314 }
315
316 if (!num) {
317 LOGMEM;
318 return NULL;
319 }
320 if (minus) {
321 strcpy(num, "-");
322 } else {
323 num[0] = '\0';
324 }
325
326 if (dot_pos < 1) {
327 strcat(num, "0.");
328 }
329 if (dot_pos < 0) {
330 sprintf(num + strlen(num), "%0*d", -dot_pos, 0);
331 }
332
333 chars_to_dot = dot_pos;
Michal Vasko9d1593d2016-09-30 12:33:34 +0200334 for (ptr = num + strlen(num), number_ptr = number; (unsigned)(number_ptr - number) < num_len_no_e; ) {
Michal Vasko1e676ca2016-06-24 15:23:54 +0200335 if (!chars_to_dot) {
336 *ptr = '.';
337 ++ptr;
338 chars_to_dot = -1;
339 } else if (isdigit(*number_ptr)) {
340 *ptr = *number_ptr;
341 ++ptr;
342 ++number_ptr;
343 if (chars_to_dot > 0) {
344 --chars_to_dot;
345 }
346 } else if (*number_ptr == '.') {
347 ++number_ptr;
348 } else {
349 LOGINT;
350 free(num);
351 return NULL;
352 }
353 }
354 *ptr = '\0';
355
356 if (dot_pos > (signed)num_len_no_e) {
357 sprintf(num + strlen(num), "%0*d", dot_pos - num_len_no_e, 0);
358 }
359
360 return num;
361}
362
Radek Krejci5449d472015-10-26 14:35:56 +0100363static unsigned int
364lyjson_parse_boolean(const char *data)
365{
Radek Krejci6b47b502015-10-30 15:52:41 +0100366 unsigned int len = 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100367
368 if (!strncmp(data, "false", 5)) {
369 len = 5;
370 } else if (!strncmp(data, "true", 4)) {
371 len = 4;
372 }
373
374 if (data[len] && data[len] != ',' && data[len] != ']' && data[len] != '}' && !lyjson_isspace(data[len])) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100375 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON literal value (expected true or false)");
Radek Krejci5449d472015-10-26 14:35:56 +0100376 return 0;
377 }
378
379 return len;
380}
381
382static unsigned int
Radek Krejci4ae82942016-09-19 16:41:06 +0200383json_get_anydata(struct lyd_node_anydata *any, const char *data)
Radek Krejci5449d472015-10-26 14:35:56 +0100384{
Radek Krejcia7177672016-11-17 14:53:03 +0900385 unsigned int len = 0, start, stop, c = 0;
386 char *str;
Radek Krejci5449d472015-10-26 14:35:56 +0100387
Radek Krejcia7177672016-11-17 14:53:03 +0900388 /* anydata (as well as meaningful anyxml) is supposed to be encoded as object,
389 * anyxml can be a string value, other JSON types are not supported since it is
390 * not clear how they are supposed to be represented/converted into an internal representation */
391 if (data[len] == '"' && any->schema->nodetype == LYS_ANYXML) {
392 len = 1;
393 str = lyjson_parse_text(&data[len], &c);
394 if (!str) {
395 return 0;
396 }
397 if (data[len + c] != '"') {
Radek Krejci7d6d2842016-11-28 11:21:38 +0100398 free(str);
Radek Krejcia7177672016-11-17 14:53:03 +0900399 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any,
400 "JSON data (missing quotation-mark at the end of string)");
401 return 0;
402 }
403
404 any->value.str = lydict_insert_zc(any->schema->module->ctx, str);
405 any->value_type = LYD_ANYDATA_CONSTSTRING;
406 return len + c + 1;
407 } else if (data[len] != '{') {
408 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any, "Unsupported Anydata/anyxml content (not an object nor string)");
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200409 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100410 }
411
Radek Krejcia7177672016-11-17 14:53:03 +0900412 /* count opening '{' and closing '}' brackets to get the end of the object without its parsing */
413 c = len = 1;
Radek Krejci4ae82942016-09-19 16:41:06 +0200414 len += skip_ws(&data[len]);
Radek Krejcia7177672016-11-17 14:53:03 +0900415 start = len;
416 stop = start - 1;
Radek Krejci4ae82942016-09-19 16:41:06 +0200417 while (data[len] && c) {
418 switch (data[len]) {
419 case '{':
420 c++;
421 break;
422 case '}':
423 c--;
424 break;
425 default:
426 if (!isspace(data[len])) {
427 stop = len;
428 }
429 }
430 len++;
431 }
432 if (c) {
433 LOGVAL(LYE_EOF, LY_VLOG_LYD, any);
434 return 0;
435 }
436 any->value_type = LYD_ANYDATA_JSON;
Radek Krejcia7177672016-11-17 14:53:03 +0900437 if (stop >= start) {
438 any->value.str = lydict_insert(any->schema->module->ctx, &data[start], stop - start + 1);
439 } /* else no data */
Radek Krejci4ae82942016-09-19 16:41:06 +0200440
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200441 return len;
Radek Krejci5449d472015-10-26 14:35:56 +0100442}
443
444static unsigned int
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100445json_get_value(struct lyd_node_leaf_list *leaf, struct lyd_node *first_sibling, const char *data)
Radek Krejci5449d472015-10-26 14:35:56 +0100446{
Radek Krejcibd930122016-08-10 13:28:26 +0200447 struct lyd_node_leaf_list *new;
Radek Krejci37b756f2016-01-18 10:15:03 +0100448 struct lys_type *stype;
Radek Krejci5449d472015-10-26 14:35:56 +0100449 struct ly_ctx *ctx;
450 unsigned int len = 0, r;
Radek Krejci5449d472015-10-26 14:35:56 +0100451 char *str;
452
Radek Krejci0b7704f2016-03-18 12:16:14 +0100453 assert(leaf && data);
Radek Krejci5449d472015-10-26 14:35:56 +0100454 ctx = leaf->schema->module->ctx;
455
456 stype = &((struct lys_node_leaf *)leaf->schema)->type;
Radek Krejci23238922015-10-27 17:13:34 +0100457
Radek Krejci5449d472015-10-26 14:35:56 +0100458 if (leaf->schema->nodetype == LYS_LEAFLIST) {
459 /* expecting begin-array */
460 if (data[len++] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100461 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expected begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100462 return 0;
463 }
464
465repeat:
466 len += skip_ws(&data[len]);
467 }
468
469 /* will be changed in case of union */
470 leaf->value_type = stype->base;
471
472 if (data[len] == '"') {
473 /* string representations */
Michal Vasko6baed1c2016-05-18 13:24:44 +0200474 ++len;
Radek Krejci5449d472015-10-26 14:35:56 +0100475 str = lyjson_parse_text(&data[len], &r);
Radek Krejci23238922015-10-27 17:13:34 +0100476 if (!str) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100477 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100478 return 0;
479 }
480 leaf->value_str = lydict_insert_zc(ctx, str);
481 if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100482 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf,
Radek Krejciadb57612016-02-16 13:34:34 +0100483 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100484 return 0;
485 }
486 len += r + 1;
487 } else if (data[len] == '-' || isdigit(data[len])) {
488 /* numeric type */
489 r = lyjson_parse_number(&data[len]);
490 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100491 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100492 return 0;
493 }
Michal Vasko1e676ca2016-06-24 15:23:54 +0200494 /* if it's a number with 'e' or 'E', get rid of it first */
495 if ((str = strnchr(&data[len], 'e', r)) || (str = strnchr(&data[len], 'E', r))) {
496 str = lyjson_convert_enumber(&data[len], r, str);
497 if (!str) {
498 return 0;
499 }
500 leaf->value_str = lydict_insert_zc(ctx, str);
501 } else {
502 leaf->value_str = lydict_insert(ctx, &data[len], r);
503 }
Radek Krejci5449d472015-10-26 14:35:56 +0100504 len += r;
505 } else if (data[len] == 'f' || data[len] == 't') {
506 /* boolean */
507 r = lyjson_parse_boolean(&data[len]);
508 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100509 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100510 return 0;
511 }
512 leaf->value_str = lydict_insert(ctx, &data[len], r);
513 len += r;
514 } else if (!strncmp(&data[len], "[null]", 6)) {
515 /* empty */
Michal Vasko44913842016-04-13 14:20:41 +0200516 leaf->value_str = lydict_insert(ctx, "", 0);
Radek Krejci5449d472015-10-26 14:35:56 +0100517 len += 6;
518 } else {
519 /* error */
Radek Krejci48464ed2016-03-17 15:44:09 +0100520 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (unexpected value)");
Radek Krejci5449d472015-10-26 14:35:56 +0100521 return 0;
522 }
523
Radek Krejci1899d6a2016-11-03 13:48:07 +0100524 /* the value is here converted to a JSON format if needed in case of LY_TYPE_IDENT and LY_TYPE_INST or to a
525 * canonical form of the value */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100526 if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, NULL, leaf, 1, 0)) {
Radek Krejci23238922015-10-27 17:13:34 +0100527 ly_errno = LY_EVALID;
528 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100529 }
530
531 if (leaf->schema->nodetype == LYS_LEAFLIST) {
532 /* repeat until end-array */
533 len += skip_ws(&data[len]);
534 if (data[len] == ',') {
535 /* another instance of the leaf-list */
536 new = calloc(1, sizeof(struct lyd_node_leaf_list));
Michal Vasko253035f2015-12-17 16:58:13 +0100537 if (!new) {
538 LOGMEM;
539 return 0;
540 }
Radek Krejci5449d472015-10-26 14:35:56 +0100541 new->parent = leaf->parent;
542 new->prev = (struct lyd_node *)leaf;
543 leaf->next = (struct lyd_node *)new;
544
545 /* fix the "last" pointer */
Radek Krejcibd930122016-08-10 13:28:26 +0200546 first_sibling->prev = (struct lyd_node *)new;
Radek Krejci5449d472015-10-26 14:35:56 +0100547
548 new->schema = leaf->schema;
549
550 /* repeat value parsing */
551 leaf = new;
552 len++;
553 goto repeat;
554 } else if (data[len] == ']') {
555 len++;
556 len += skip_ws(&data[len]);
557 } else {
558 /* something unexpected */
Radek Krejci48464ed2016-03-17 15:44:09 +0100559 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expecting value-separator or end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100560 return 0;
561 }
562 }
563
564 len += skip_ws(&data[len]);
565 return len;
566}
567
568static unsigned int
Radek Krejci88f29302015-10-30 15:42:33 +0100569json_parse_attr(struct lys_module *parent_module, struct lyd_attr **attr, const char *data)
570{
571 unsigned int len = 0, r;
572 char *str = NULL, *name, *prefix, *value;
573 struct lys_module *module = parent_module;
574 struct lyd_attr *attr_new, *attr_last = NULL;
575
Radek Krejcide9d92c2015-10-30 15:59:59 +0100576 *attr = NULL;
577
Radek Krejci88f29302015-10-30 15:42:33 +0100578 if (data[len] != '{') {
579 if (!strncmp(&data[len], "null", 4)) {
Radek Krejci88f29302015-10-30 15:42:33 +0100580 len += 4;
581 len += skip_ws(&data[len]);
582 return len;
583 }
Radek Krejci48464ed2016-03-17 15:44:09 +0100584 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing begin-object)");
Radek Krejci88f29302015-10-30 15:42:33 +0100585 goto error;
586 }
587
588repeat:
589 len++;
590 len += skip_ws(&data[len]);
591
592 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100593 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the begining of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100594 return 0;
595 }
596 len++;
597 str = lyjson_parse_text(&data[len], &r);
598 if (!r) {
599 goto error;
600 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100601 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100602 goto error;
603 }
604 if ((name = strchr(str, ':'))) {
605 *name = '\0';
606 name++;
607 prefix = str;
Michal Vasko1e62a092015-12-01 12:27:20 +0100608 module = (struct lys_module *)ly_ctx_get_module(parent_module->ctx, prefix, NULL);
Radek Krejci88f29302015-10-30 15:42:33 +0100609 if (!module) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100610 LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name);
Radek Krejci88f29302015-10-30 15:42:33 +0100611 goto error;
612 }
613 } else {
614 name = str;
615 }
616
617 /* prepare data for parsing node content */
618 len += r + 1;
619 len += skip_ws(&data[len]);
620 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100621 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing name-separator)");
Radek Krejci88f29302015-10-30 15:42:33 +0100622 goto error;
623 }
624 len++;
625 len += skip_ws(&data[len]);
626
627 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100628 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the beginning of string)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100629 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100630 }
631 len++;
632 value = lyjson_parse_text(&data[len], &r);
633 if (!r) {
634 goto error;
635 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100636 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100637 free(value);
Radek Krejci88f29302015-10-30 15:42:33 +0100638 goto error;
639 }
640 len += r + 1;
641 len += skip_ws(&data[len]);
642
643 attr_new = malloc(sizeof **attr);
Michal Vasko253035f2015-12-17 16:58:13 +0100644 if (!attr_new) {
645 LOGMEM;
646 goto error;
647 }
Radek Krejci88f29302015-10-30 15:42:33 +0100648 attr_new->module = module;
649 attr_new->next = NULL;
650 attr_new->name = lydict_insert(module->ctx, name, 0);
651 attr_new->value = lydict_insert_zc(module->ctx, value);
652 if (!attr_last) {
653 *attr = attr_last = attr_new;
654 } else {
655 attr_last->next = attr_new;
656 attr_last = attr_new;
657 }
658
659 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100660 str = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100661
662 if (data[len] == ',') {
663 goto repeat;
664 } else if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100665 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing end-object)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100666 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100667 }
668 len++;
669 len += skip_ws(&data[len]);
670
671 return len;
672
673error:
674 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100675 if (*attr) {
676 lyd_free_attr((*attr)->module->ctx, NULL, *attr, 1);
677 *attr = NULL;
678 }
Radek Krejci88f29302015-10-30 15:42:33 +0100679 return 0;
680}
681
682struct attr_cont {
683 struct attr_cont *next;
684 struct lyd_attr *attr;
685 struct lys_node *schema;
686 unsigned int index; /** non-zero only in case of leaf-list */
687};
688
Radek Krejcic4831272015-11-01 19:26:34 +0100689static int
690store_attrs(struct ly_ctx *ctx, struct attr_cont *attrs, struct lyd_node *first)
691{
692 struct lyd_node *diter;
693 struct attr_cont *iter;
694 unsigned int flag_leaflist = 0;
695
696 while (attrs) {
697 iter = attrs;
698 attrs = attrs->next;
699
700 if (iter->index) {
701 flag_leaflist = 1;
702 }
703
704 LY_TREE_FOR(first, diter) {
705 if (iter->schema != diter->schema) {
706 continue;
707 }
708
709 if (flag_leaflist && flag_leaflist != iter->index) {
710 flag_leaflist++;
711 continue;
712 }
713
714 /* we have match */
715 if (diter->attr) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100716 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, diter,
Radek Krejciadb57612016-02-16 13:34:34 +0100717 "attribute (multiple attribute definitions belong to a single element)");
Radek Krejcic4831272015-11-01 19:26:34 +0100718 free(iter);
719 goto error;
720 }
721
722 diter->attr = iter->attr;
723 break;
724 }
725
726 if (!diter) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100727 LOGVAL(LYE_XML_MISS, LY_VLOG_NONE, NULL, "element for the specified attribute", iter->attr->name);
Radek Krejcic4831272015-11-01 19:26:34 +0100728 lyd_free_attr(iter->schema->module->ctx, NULL, iter->attr, 1);
729 free(iter);
730 goto error;
731 }
732 free(iter);
733 }
734
735 return 0;
736
737error:
738
739 while (attrs) {
740 iter = attrs;
741 attrs = attrs->next;
742
743 lyd_free_attr(ctx, NULL, iter->attr, 1);
744 free(iter);
745 }
746
747 return -1;
748}
749
Radek Krejci88f29302015-10-30 15:42:33 +0100750static unsigned int
Michal Vasko36ef6932015-12-01 14:30:17 +0100751json_parse_data(struct ly_ctx *ctx, const char *data, const struct lys_node *schema_parent, struct lyd_node **parent,
Radek Krejcibd930122016-08-10 13:28:26 +0200752 struct lyd_node *first_sibling, struct lyd_node *prev, struct attr_cont **attrs, int options,
Michal Vaskoafa7a642016-10-18 15:11:38 +0200753 struct unres_data *unres, struct lyd_node **act_notif)
Radek Krejci5449d472015-10-26 14:35:56 +0100754{
755 unsigned int len = 0;
756 unsigned int r;
Radek Krejcic4831272015-11-01 19:26:34 +0100757 unsigned int flag_leaflist = 0;
Radek Krejci61767ca2016-09-19 14:21:55 +0200758 int i, pos;
Radek Krejci88f29302015-10-30 15:42:33 +0100759 char *name, *prefix = NULL, *str = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100760 const struct lys_module *module = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +0100761 struct lys_node *schema = NULL;
Radek Krejcibd930122016-08-10 13:28:26 +0200762 struct lyd_node *result = NULL, *new, *list, *diter = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100763 struct lyd_attr *attr;
Radek Krejcic4831272015-11-01 19:26:34 +0100764 struct attr_cont *attrs_aux;
Radek Krejci5449d472015-10-26 14:35:56 +0100765
766 /* each YANG data node representation starts with string (node identifier) */
767 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100768 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100769 "JSON data (missing quotation-mark at the beginning of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100770 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100771 }
772 len++;
773
Radek Krejci23238922015-10-27 17:13:34 +0100774 str = lyjson_parse_text(&data[len], &r);
Radek Krejci5449d472015-10-26 14:35:56 +0100775 if (!r) {
776 goto error;
777 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100778 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100779 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100780 goto error;
781 }
Radek Krejci23238922015-10-27 17:13:34 +0100782 if ((name = strchr(str, ':'))) {
783 *name = '\0';
784 name++;
785 prefix = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100786 if (prefix[0] == '@') {
787 prefix++;
788 }
Radek Krejci23238922015-10-27 17:13:34 +0100789 } else {
790 name = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100791 if (name[0] == '@') {
792 name++;
793 }
Radek Krejci5449d472015-10-26 14:35:56 +0100794 }
795
Radek Krejci5449d472015-10-26 14:35:56 +0100796 /* prepare data for parsing node content */
797 len += r + 1;
798 len += skip_ws(&data[len]);
799 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100800 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing name-separator)");
Radek Krejci5449d472015-10-26 14:35:56 +0100801 goto error;
802 }
803 len++;
804 len += skip_ws(&data[len]);
805
Radek Krejci88f29302015-10-30 15:42:33 +0100806 if (str[0] == '@' && !str[1]) {
807 /* process attribute of the parent object (container or list) */
Radek Krejci2a0efef2016-03-24 15:10:40 +0100808 if (!(*parent)) {
809 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute with no corresponding element to belongs to");
Radek Krejci88f29302015-10-30 15:42:33 +0100810 goto error;
811 }
812
813 r = json_parse_attr((*parent)->schema->module, &attr, &data[len]);
814 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100815 LOGPATH(LY_VLOG_LYD, (*parent));
Radek Krejci88f29302015-10-30 15:42:33 +0100816 goto error;
817 }
818 len += r;
819
820 if ((*parent)->attr) {
821 lyd_free_attr(ctx, NULL, attr, 1);
822 } else {
823 (*parent)->attr = attr;
824 }
Radek Krejcic4831272015-11-01 19:26:34 +0100825 free(str);
826 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100827 }
828
Radek Krejci5449d472015-10-26 14:35:56 +0100829 /* find schema node */
830 if (!(*parent)) {
831 /* starting in root */
832 /* get the proper schema */
833 module = ly_ctx_get_module(ctx, prefix, NULL);
834 if (module) {
835 /* get the proper schema node */
Radek Krejci919a9242016-07-27 08:17:13 +0200836 while ((schema = (struct lys_node *)lys_getnext(schema, NULL, module, 0))) {
Radek Krejci5449d472015-10-26 14:35:56 +0100837 if (!strcmp(schema->name, name)) {
838 break;
839 }
840 }
Radek Krejci88f29302015-10-30 15:42:33 +0100841 } else {
Radek Krejci2a0efef2016-03-24 15:10:40 +0100842 LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name);
Radek Krejci88f29302015-10-30 15:42:33 +0100843 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100844 }
845 } else {
846 /* parsing some internal node, we start with parent's schema pointer */
847 if (prefix) {
848 /* get the proper schema */
849 module = ly_ctx_get_module(ctx, prefix, NULL);
850 if (!module) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100851 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci88f29302015-10-30 15:42:33 +0100852 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100853 }
854 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100855
856 /* go through RPC's input/output following the options' data type */
857 if ((*parent)->schema->nodetype == LYS_RPC) {
858 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, module, LYS_GETNEXT_WITHINOUT))) {
Michal Vaskoafa7a642016-10-18 15:11:38 +0200859 if ((options & LYD_OPT_RPC) && (schema->nodetype == LYS_INPUT)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100860 break;
Michal Vaskoafa7a642016-10-18 15:11:38 +0200861 } else if ((options & LYD_OPT_RPCREPLY) && (schema->nodetype == LYS_OUTPUT)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100862 break;
863 }
864 }
865 if (!schema) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100866 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100867 goto error;
868 }
869 schema_parent = schema;
870 schema = NULL;
871 }
872
Michal Vasko36ef6932015-12-01 14:30:17 +0100873 if (schema_parent) {
874 while ((schema = (struct lys_node *)lys_getnext(schema, schema_parent, module, 0))) {
875 if (!strcmp(schema->name, name)) {
876 break;
877 }
878 }
879 } else {
880 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, module, 0))) {
881 if (!strcmp(schema->name, name)) {
882 break;
883 }
Radek Krejci5449d472015-10-26 14:35:56 +0100884 }
885 }
886 }
Radek Krejci27fe55e2016-09-13 17:13:35 +0200887 if (!schema || !lys_node_module(schema)->implemented) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100888 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci88f29302015-10-30 15:42:33 +0100889 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100890 }
Radek Krejci88f29302015-10-30 15:42:33 +0100891
892 if (str[0] == '@') {
Radek Krejci88f29302015-10-30 15:42:33 +0100893 /* attribute for some sibling node */
894 if (data[len] == '[') {
895 flag_leaflist = 1;
896 len++;
897 len += skip_ws(&data[len]);
898 }
899
900attr_repeat:
901 r = json_parse_attr(schema->module, &attr, &data[len]);
902 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100903 LOGPATH(LY_VLOG_LYD, (*parent));
Radek Krejci88f29302015-10-30 15:42:33 +0100904 goto error;
905 }
906 len += r;
907
908 if (attr) {
Radek Krejcic4831272015-11-01 19:26:34 +0100909 attrs_aux = malloc(sizeof *attrs_aux);
Michal Vasko253035f2015-12-17 16:58:13 +0100910 if (!attrs_aux) {
911 LOGMEM;
912 goto error;
913 }
Radek Krejcic4831272015-11-01 19:26:34 +0100914 attrs_aux->attr = attr;
915 attrs_aux->index = flag_leaflist;
916 attrs_aux->schema = schema;
917 attrs_aux->next = *attrs;
918 *attrs = attrs_aux;
Radek Krejci88f29302015-10-30 15:42:33 +0100919 } else if (!flag_leaflist) {
920 /* error */
Radek Krejci48464ed2016-03-17 15:44:09 +0100921 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "attribute data");
Radek Krejci88f29302015-10-30 15:42:33 +0100922 goto error;
923 }
924
925 if (flag_leaflist) {
926 if (data[len] == ',') {
927 len++;
928 len += skip_ws(&data[len]);
929 flag_leaflist++;
930 goto attr_repeat;
931 } else if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100932 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing end-array)");
Radek Krejci88f29302015-10-30 15:42:33 +0100933 goto error;
934 }
935 len++;
936 len += skip_ws(&data[len]);
937 }
938
Radek Krejcic4831272015-11-01 19:26:34 +0100939 free(str);
940 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100941 }
942
Radek Krejci5449d472015-10-26 14:35:56 +0100943 switch (schema->nodetype) {
944 case LYS_CONTAINER:
945 case LYS_LIST:
946 case LYS_NOTIF:
947 case LYS_RPC:
Michal Vaskoafa7a642016-10-18 15:11:38 +0200948 case LYS_ACTION:
Radek Krejci5449d472015-10-26 14:35:56 +0100949 result = calloc(1, sizeof *result);
950 break;
951 case LYS_LEAF:
952 case LYS_LEAFLIST:
953 result = calloc(1, sizeof(struct lyd_node_leaf_list));
954 break;
955 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200956 case LYS_ANYDATA:
957 result = calloc(1, sizeof(struct lyd_node_anydata));
Radek Krejci5449d472015-10-26 14:35:56 +0100958 break;
959 default:
960 LOGINT;
Radek Krejci595060f2015-10-30 16:29:58 +0100961 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100962 }
Michal Vasko253035f2015-12-17 16:58:13 +0100963 if (!result) {
964 LOGMEM;
965 goto error;
966 }
967
Radek Krejci61767ca2016-09-19 14:21:55 +0200968 result->prev = result;
Radek Krejci5449d472015-10-26 14:35:56 +0100969 result->schema = schema;
Radek Krejci61767ca2016-09-19 14:21:55 +0200970 result->parent = *parent;
971 diter = NULL;
972 if (*parent && (*parent)->child && schema->nodetype == LYS_LEAF && (*parent)->schema->nodetype == LYS_LIST &&
973 (pos = lys_is_key((struct lys_node_list *)(*parent)->schema, (struct lys_node_leaf *)schema))) {
974 /* it is key and we need to insert it into a correct place */
975 for (i = 0, diter = (*parent)->child;
976 diter && i < (pos - 1) && diter->schema->nodetype == LYS_LEAF &&
977 lys_is_key((struct lys_node_list *)(*parent)->schema, (struct lys_node_leaf *)diter->schema);
978 i++, diter = diter->next);
979 if (diter) {
980 /* out of order insertion - insert list's key to the correct position, before the diter */
981 if ((*parent)->child == diter) {
982 (*parent)->child = result;
983 /* update first_sibling */
984 first_sibling = result;
985 }
986 if (diter->prev->next) {
987 diter->prev->next = result;
988 }
989 result->prev = diter->prev;
990 diter->prev = result;
991 result->next = diter;
992 }
993 }
994 if (!diter) {
995 /* simplified (faster) insert as the last node */
996 if (*parent && !(*parent)->child) {
997 (*parent)->child = result;
998 }
999 if (prev) {
1000 result->prev = prev;
1001 prev->next = result;
1002
1003 /* fix the "last" pointer */
1004 first_sibling->prev = result;
1005 } else {
1006 result->prev = result;
1007 first_sibling = result;
1008 }
1009 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001010 result->validity = ly_new_node_validity(result->schema);
Radek Krejci46165822016-08-26 14:06:27 +02001011 if (resolve_applies_when(schema, 0, NULL)) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01001012 result->when_status = LYD_WHEN;
1013 }
Radek Krejci5449d472015-10-26 14:35:56 +01001014
Radek Krejci5449d472015-10-26 14:35:56 +01001015 /* type specific processing */
1016 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1017 /* type detection and assigning the value */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001018 r = json_get_value((struct lyd_node_leaf_list *)result, first_sibling, &data[len]);
Radek Krejci5449d472015-10-26 14:35:56 +01001019 if (!r) {
1020 goto error;
1021 }
Radek Krejci88f29302015-10-30 15:42:33 +01001022 while(result->next) {
1023 result = result->next;
1024 }
1025
Radek Krejci5449d472015-10-26 14:35:56 +01001026 len += r;
1027 len += skip_ws(&data[len]);
Radek Krejcibf2abff2016-08-23 15:51:52 +02001028 } else if (schema->nodetype & LYS_ANYDATA) {
Radek Krejci4ae82942016-09-19 16:41:06 +02001029 r = json_get_anydata((struct lyd_node_anydata *)result, &data[len]);
Radek Krejci5449d472015-10-26 14:35:56 +01001030 if (!r) {
1031 goto error;
1032 }
1033 len += r;
1034 len += skip_ws(&data[len]);
Michal Vaskoafa7a642016-10-18 15:11:38 +02001035 } else if (schema->nodetype & (LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1036 if (schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1037 if (!(options & LYD_OPT_RPC) || *act_notif) {
1038 LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name);
1039 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Unexpected %s node \"%s\".",
1040 (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name);
1041 goto error;
1042 }
1043 *act_notif = result;
1044 } else if (schema->nodetype == LYS_NOTIF) {
1045 if (!(options & LYD_OPT_NOTIF) || *act_notif) {
1046 LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name);
1047 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Unexpected notification node \"%s\".", schema->name);
1048 goto error;
1049 }
1050 *act_notif = result;
1051 }
1052
Radek Krejci5449d472015-10-26 14:35:56 +01001053 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001054 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001055 goto error;
1056 }
1057 len++;
1058 len += skip_ws(&data[len]);
1059
1060 if (data[len] != '}') {
1061 /* non-empty container */
Radek Krejcic4831272015-11-01 19:26:34 +01001062 len--;
1063 diter = NULL;
1064 attrs_aux = NULL;
1065 do {
1066 len++;
1067 len += skip_ws(&data[len]);
1068
Michal Vaskoafa7a642016-10-18 15:11:38 +02001069 r = json_parse_data(ctx, &data[len], NULL, &result, result->child, diter, &attrs_aux, options, unres, act_notif);
Radek Krejcic4831272015-11-01 19:26:34 +01001070 if (!r) {
1071 goto error;
1072 }
1073 len += r;
1074
1075 if (result->child) {
1076 diter = result->child->prev;
1077 }
1078 } while(data[len] == ',');
1079
1080 /* store attributes */
1081 if (store_attrs(ctx, attrs_aux, result->child)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001082 goto error;
1083 }
Radek Krejci5449d472015-10-26 14:35:56 +01001084 }
1085
1086 if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001087 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001088 goto error;
1089 }
1090 len++;
1091 len += skip_ws(&data[len]);
1092
Radek Krejcifb7156e2016-10-27 13:39:56 +02001093 /* if we have empty non-presence container, mark it as default */
Radek Krejci2537fd32016-09-07 16:22:41 +02001094 if (schema->nodetype == LYS_CONTAINER && !result->child &&
Radek Krejcid3e73722016-05-23 12:24:55 +02001095 !result->attr && !((struct lys_node_container *)schema)->presence) {
Radek Krejcifb7156e2016-10-27 13:39:56 +02001096 result->dflt = 1;
Radek Krejci0c0086a2016-03-24 15:20:28 +01001097 }
1098
Radek Krejci5449d472015-10-26 14:35:56 +01001099 } else if (schema->nodetype == LYS_LIST) {
1100 if (data[len] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001101 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001102 goto error;
1103 }
1104
1105 list = result;
1106 do {
1107 len++;
1108 len += skip_ws(&data[len]);
Radek Krejci23238922015-10-27 17:13:34 +01001109
Radek Krejcic4831272015-11-01 19:26:34 +01001110 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001111 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001112 "JSON data (missing list instance's begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001113 goto error;
1114 }
Radek Krejcic4831272015-11-01 19:26:34 +01001115 diter = NULL;
1116 attrs_aux = NULL;
1117 do {
1118 len++;
1119 len += skip_ws(&data[len]);
1120
Michal Vaskoafa7a642016-10-18 15:11:38 +02001121 r = json_parse_data(ctx, &data[len], NULL, &list, list->child, diter, &attrs_aux, options, unres, act_notif);
Radek Krejcic4831272015-11-01 19:26:34 +01001122 if (!r) {
1123 goto error;
1124 }
1125 len += r;
1126
Radek Krejci52934692015-11-01 20:08:15 +01001127 if (list->child) {
Radek Krejcic4831272015-11-01 19:26:34 +01001128 diter = list->child->prev;
1129 }
1130 } while(data[len] == ',');
1131
1132 /* store attributes */
Radek Krejci52934692015-11-01 20:08:15 +01001133 if (store_attrs(ctx, attrs_aux, list->child)) {
Radek Krejcic4831272015-11-01 19:26:34 +01001134 goto error;
1135 }
1136
1137 if (data[len] != '}') {
1138 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001139 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001140 "JSON data (missing list instance's end-object)");
Radek Krejcic4831272015-11-01 19:26:34 +01001141 goto error;
1142 }
1143 len++;
Radek Krejci5449d472015-10-26 14:35:56 +01001144 len += skip_ws(&data[len]);
1145
1146 if (data[len] == ',') {
Radek Krejci93fab982016-02-03 15:58:19 +01001147 /* various validation checks */
Radek Krejci00a0e712016-10-26 10:24:46 +02001148 ly_err_clean(1);
Michal Vaskoad2e44a2017-01-03 10:31:35 +01001149 if (lyv_data_content(list, options, unres) ||
1150 lyv_multicases(list, NULL, prev ? &first_sibling : NULL, 0, NULL)) {
Radek Krejci93fab982016-02-03 15:58:19 +01001151 if (ly_errno) {
1152 goto error;
1153 }
1154 }
Radek Krejci93fab982016-02-03 15:58:19 +01001155
Radek Krejci5449d472015-10-26 14:35:56 +01001156 /* another instance of the list */
1157 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001158 if (!new) {
1159 goto error;
1160 }
Radek Krejci5449d472015-10-26 14:35:56 +01001161 new->parent = list->parent;
1162 new->prev = list;
1163 list->next = new;
1164
1165 /* fix the "last" pointer */
Radek Krejci2d5525d2016-04-04 15:43:30 +02001166 first_sibling->prev = new;
Radek Krejci5449d472015-10-26 14:35:56 +01001167
1168 new->schema = list->schema;
Radek Krejci5449d472015-10-26 14:35:56 +01001169 list = new;
1170 }
1171 } while (data[len] == ',');
Michal Vaskod171d4e2016-06-06 13:33:19 +02001172 result = first_sibling;
Radek Krejci5449d472015-10-26 14:35:56 +01001173
1174 if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001175 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001176 goto error;
1177 }
1178 len++;
1179 len += skip_ws(&data[len]);
1180 }
1181
1182 /* various validation checks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001183 if (lyv_data_context(result, options, unres)) {
Michal Vasko10e586f2016-05-18 13:25:30 +02001184 goto error;
1185 }
1186
Radek Krejci00a0e712016-10-26 10:24:46 +02001187 ly_err_clean(1);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001188 if (lyv_data_content(result, options, unres) ||
1189 lyv_multicases(result, NULL, prev ? &first_sibling : NULL, 0, NULL)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001190 if (ly_errno) {
1191 goto error;
1192 }
1193 }
1194
Radek Krejcica7efb72016-01-18 13:06:01 +01001195 /* validation successful */
Radek Krejci63b79c82016-08-10 10:09:33 +02001196 if (result->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001197 /* postpone checking of unique when there will be all list/leaflist instances */
1198 result->validity |= LYD_VAL_UNIQUE;
Radek Krejci63b79c82016-08-10 10:09:33 +02001199 }
Radek Krejcica7efb72016-01-18 13:06:01 +01001200
Radek Krejci88f29302015-10-30 15:42:33 +01001201 if (!(*parent)) {
1202 *parent = result;
1203 }
1204
Radek Krejcide9d92c2015-10-30 15:59:59 +01001205 free(str);
Radek Krejci5449d472015-10-26 14:35:56 +01001206 return len;
1207
1208error:
Radek Krejci0c0086a2016-03-24 15:20:28 +01001209 len = 0;
Radek Krejci0c0086a2016-03-24 15:20:28 +01001210 /* cleanup */
1211 for (i = unres->count - 1; i >= 0; i--) {
1212 /* remove unres items connected with the node being removed */
1213 if (unres->node[i] == result) {
1214 unres_data_del(unres, i);
1215 }
Radek Krejci88f29302015-10-30 15:42:33 +01001216 }
1217 while (*attrs) {
Radek Krejcic4831272015-11-01 19:26:34 +01001218 attrs_aux = *attrs;
Radek Krejci88f29302015-10-30 15:42:33 +01001219 *attrs = (*attrs)->next;
1220
Radek Krejcic4831272015-11-01 19:26:34 +01001221 lyd_free_attr(ctx, NULL, attrs_aux->attr, 1);
1222 free(attrs_aux);
Radek Krejci88f29302015-10-30 15:42:33 +01001223 }
1224
Radek Krejci5449d472015-10-26 14:35:56 +01001225 lyd_free(result);
Radek Krejci88f29302015-10-30 15:42:33 +01001226 free(str);
1227
Radek Krejci0c0086a2016-03-24 15:20:28 +01001228 return len;
Radek Krejci5449d472015-10-26 14:35:56 +01001229}
1230
1231struct lyd_node *
Michal Vasko945b96b2016-10-18 11:49:12 +02001232lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act,
1233 const struct lyd_node *data_tree)
Radek Krejci5449d472015-10-26 14:35:56 +01001234{
Michal Vaskoafa7a642016-10-18 15:11:38 +02001235 struct lyd_node *result = NULL, *next, *iter, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001236 struct unres_data *unres = NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001237 unsigned int len = 0, r;
Michal Vaskoafa7a642016-10-18 15:11:38 +02001238 int i, act_cont = 0;
Radek Krejcic4831272015-11-01 19:26:34 +01001239 struct attr_cont *attrs = NULL;
Radek Krejci63b79c82016-08-10 10:09:33 +02001240 struct ly_set *set;
Radek Krejci5449d472015-10-26 14:35:56 +01001241
Radek Krejci00a0e712016-10-26 10:24:46 +02001242 ly_err_clean(1);
Radek Krejci2342cf62016-01-29 16:48:23 +01001243
Michal Vaskoafa7a642016-10-18 15:11:38 +02001244 if (!ctx || !data) {
Radek Krejci5449d472015-10-26 14:35:56 +01001245 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1246 return NULL;
1247 }
1248
Radek Krejcic4831272015-11-01 19:26:34 +01001249 /* skip leading whitespaces */
1250 len += skip_ws(&data[len]);
1251
Michal Vasko24d982f2016-04-18 15:13:58 +02001252 /* no data (or whitespaces only) are fine */
1253 if (!data[len]) {
1254 lyd_validate(&result, options, ctx);
1255 return result;
1256 }
1257
Radek Krejcic4831272015-11-01 19:26:34 +01001258 /* expect top-level { */
1259 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001260 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level begin-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001261 return NULL;
1262 }
1263
1264 unres = calloc(1, sizeof *unres);
1265 if (!unres) {
1266 LOGMEM;
1267 return NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001268 }
1269
Michal Vaskoafa7a642016-10-18 15:11:38 +02001270 /* create RPC/action reply part that is not in the parsed data */
1271 if (rpc_act) {
1272 assert(options & LYD_OPT_RPCREPLY);
1273 if (rpc_act->schema->nodetype == LYS_RPC) {
1274 /* RPC request */
1275 reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0);
1276 } else {
1277 /* action request */
1278 reply_top = lyd_dup(rpc_act, 1);
1279 LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) {
1280 if (reply_parent->schema->nodetype == LYS_ACTION) {
1281 break;
1282 }
1283 LY_TREE_DFS_END(reply_top, iter, reply_parent);
1284 }
1285 if (!reply_parent) {
1286 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__);
1287 lyd_free_withsiblings(reply_top);
1288 goto error;
1289 }
1290 lyd_free_withsiblings(reply_parent->child);
1291 }
1292 }
1293
1294 iter = NULL;
1295 next = reply_parent;
Radek Krejcic4831272015-11-01 19:26:34 +01001296 do {
1297 len++;
1298 len += skip_ws(&data[len]);
1299
Michal Vaskoafa7a642016-10-18 15:11:38 +02001300 if (!act_cont) {
1301 if (!strncmp(&data[len], "\"yang:action\"", 13)) {
1302 len += 13;
1303 len += skip_ws(&data[len]);
1304 if (data[len] != ':') {
1305 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level begin-object)");
1306 lyd_free_withsiblings(reply_top);
1307 goto error;
1308 }
1309 ++len;
1310 len += skip_ws(&data[len]);
1311 if (data[len] != '{') {
1312 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level yang:action object)");
1313 lyd_free_withsiblings(reply_top);
1314 goto error;
1315 }
1316 ++len;
1317 len += skip_ws(&data[len]);
1318
1319 act_cont = 1;
1320 } else {
1321 act_cont = -1;
1322 }
1323 }
1324
1325 r = json_parse_data(ctx, &data[len], NULL, &next, result, iter, &attrs, options, unres, &act_notif);
Radek Krejcic4831272015-11-01 19:26:34 +01001326 if (!r) {
Michal Vaskoafa7a642016-10-18 15:11:38 +02001327 lyd_free_withsiblings(reply_top);
Michal Vasko24d982f2016-04-18 15:13:58 +02001328 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001329 }
1330 len += r;
1331
1332 if (!result) {
1333 result = next;
1334 }
1335 if (next) {
1336 iter = next;
1337 }
1338 next = NULL;
Michal Vaskoafa7a642016-10-18 15:11:38 +02001339 } while (data[len] == ',');
Radek Krejcic4831272015-11-01 19:26:34 +01001340
1341 if (data[len] != '}') {
1342 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001343 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001344 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001345 }
1346 len++;
1347 len += skip_ws(&data[len]);
1348
Michal Vaskoafa7a642016-10-18 15:11:38 +02001349 if (act_cont == 1) {
1350 if (data[len] != '}') {
1351 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)");
1352 goto error;
1353 }
1354 len++;
1355 len += skip_ws(&data[len]);
1356 }
1357
Radek Krejcic4831272015-11-01 19:26:34 +01001358 /* store attributes */
1359 if (store_attrs(ctx, attrs, result)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001360 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001361 }
Radek Krejci5449d472015-10-26 14:35:56 +01001362
Michal Vaskoafa7a642016-10-18 15:11:38 +02001363 if (reply_top) {
1364 result = reply_top;
1365 }
1366
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001367 if (!result) {
1368 LOGERR(LY_EVALID, "Model for the data to be linked with not found.");
Michal Vasko24d982f2016-04-18 15:13:58 +02001369 goto error;
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001370 }
1371
Michal Vaskoafa7a642016-10-18 15:11:38 +02001372 if ((options & LYD_OPT_RPCREPLY) && (rpc_act->schema->nodetype != LYS_RPC)) {
1373 /* action reply */
1374 act_notif = reply_parent;
1375 } else if ((options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) && !act_notif) {
1376 ly_vecode = LYVE_INELEM;
1377 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing %s node.", (options & LYD_OPT_RPC ? "action" : "notification"));
1378 goto error;
1379 }
1380
Radek Krejci63b79c82016-08-10 10:09:33 +02001381 /* check for uniquness of top-level lists/leaflists because
1382 * only the inner instances were tested in lyv_data_content() */
1383 set = ly_set_new();
1384 LY_TREE_FOR(result, iter) {
1385 if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) {
1386 continue;
1387 }
1388
1389 /* check each list/leaflist only once */
1390 i = set->number;
1391 if (ly_set_add(set, iter->schema, 0) != i) {
1392 /* already checked */
1393 continue;
1394 }
1395
1396 if (lyv_data_unique(iter, result)) {
1397 ly_set_free(set);
1398 goto error;
1399 }
1400 }
1401 ly_set_free(set);
1402
Radek Krejci46165822016-08-26 14:06:27 +02001403 /* add/validate default values, unres */
Michal Vaskoafa7a642016-10-18 15:11:38 +02001404 if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001405 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001406 }
1407
Radek Krejci46165822016-08-26 14:06:27 +02001408 /* check for missing top level mandatory nodes */
Michal Vaskoad2e44a2017-01-03 10:31:35 +01001409 if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER))
1410 && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001411 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001412 }
1413
Radek Krejci5449d472015-10-26 14:35:56 +01001414 free(unres->node);
1415 free(unres->type);
Radek Krejci5449d472015-10-26 14:35:56 +01001416 free(unres);
1417
1418 return result;
Michal Vasko24d982f2016-04-18 15:13:58 +02001419
1420error:
1421 lyd_free_withsiblings(result);
1422 free(unres->node);
1423 free(unres->type);
1424 free(unres);
1425
1426 return NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001427}