blob: f3b62496c7e9ae715acb20435a5db28f44a2e639 [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 */
159 } else if (data[*len] < 0x20 || data[*len] == 0x5c) {
160 /* 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] != '"') {
398 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any,
399 "JSON data (missing quotation-mark at the end of string)");
400 return 0;
401 }
402
403 any->value.str = lydict_insert_zc(any->schema->module->ctx, str);
404 any->value_type = LYD_ANYDATA_CONSTSTRING;
405 return len + c + 1;
406 } else if (data[len] != '{') {
407 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any, "Unsupported Anydata/anyxml content (not an object nor string)");
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200408 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100409 }
410
Radek Krejcia7177672016-11-17 14:53:03 +0900411 /* count opening '{' and closing '}' brackets to get the end of the object without its parsing */
412 c = len = 1;
Radek Krejci4ae82942016-09-19 16:41:06 +0200413 len += skip_ws(&data[len]);
Radek Krejcia7177672016-11-17 14:53:03 +0900414 start = len;
415 stop = start - 1;
Radek Krejci4ae82942016-09-19 16:41:06 +0200416 while (data[len] && c) {
417 switch (data[len]) {
418 case '{':
419 c++;
420 break;
421 case '}':
422 c--;
423 break;
424 default:
425 if (!isspace(data[len])) {
426 stop = len;
427 }
428 }
429 len++;
430 }
431 if (c) {
432 LOGVAL(LYE_EOF, LY_VLOG_LYD, any);
433 return 0;
434 }
435 any->value_type = LYD_ANYDATA_JSON;
Radek Krejcia7177672016-11-17 14:53:03 +0900436 if (stop >= start) {
437 any->value.str = lydict_insert(any->schema->module->ctx, &data[start], stop - start + 1);
438 } /* else no data */
Radek Krejci4ae82942016-09-19 16:41:06 +0200439
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200440 return len;
Radek Krejci5449d472015-10-26 14:35:56 +0100441}
442
443static unsigned int
Radek Krejcibd930122016-08-10 13:28:26 +0200444json_get_value(struct lyd_node_leaf_list *leaf, struct lyd_node *first_sibling, const char *data, int options)
Radek Krejci5449d472015-10-26 14:35:56 +0100445{
Radek Krejcibd930122016-08-10 13:28:26 +0200446 struct lyd_node_leaf_list *new;
Radek Krejci37b756f2016-01-18 10:15:03 +0100447 struct lys_type *stype;
Radek Krejci5449d472015-10-26 14:35:56 +0100448 struct ly_ctx *ctx;
449 unsigned int len = 0, r;
Radek Krejci1899d6a2016-11-03 13:48:07 +0100450 int resolvable;
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
Radek Krejci92ece002016-04-04 15:45:05 +0200456 if (options & (LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) {
Radek Krejci1899d6a2016-11-03 13:48:07 +0100457 resolvable = 0;
Radek Krejci23238922015-10-27 17:13:34 +0100458 } else {
Radek Krejci1899d6a2016-11-03 13:48:07 +0100459 resolvable = 1;
Radek Krejci23238922015-10-27 17:13:34 +0100460 }
461
Radek Krejci5449d472015-10-26 14:35:56 +0100462 stype = &((struct lys_node_leaf *)leaf->schema)->type;
Radek Krejci23238922015-10-27 17:13:34 +0100463
Radek Krejci5449d472015-10-26 14:35:56 +0100464 if (leaf->schema->nodetype == LYS_LEAFLIST) {
465 /* expecting begin-array */
466 if (data[len++] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100467 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expected begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100468 return 0;
469 }
470
471repeat:
472 len += skip_ws(&data[len]);
473 }
474
475 /* will be changed in case of union */
476 leaf->value_type = stype->base;
477
478 if (data[len] == '"') {
479 /* string representations */
Michal Vasko6baed1c2016-05-18 13:24:44 +0200480 ++len;
Radek Krejci5449d472015-10-26 14:35:56 +0100481 str = lyjson_parse_text(&data[len], &r);
Radek Krejci23238922015-10-27 17:13:34 +0100482 if (!str) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100483 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100484 return 0;
485 }
486 leaf->value_str = lydict_insert_zc(ctx, str);
487 if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100488 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf,
Radek Krejciadb57612016-02-16 13:34:34 +0100489 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100490 return 0;
491 }
492 len += r + 1;
493 } else if (data[len] == '-' || isdigit(data[len])) {
494 /* numeric type */
495 r = lyjson_parse_number(&data[len]);
496 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100497 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100498 return 0;
499 }
Michal Vasko1e676ca2016-06-24 15:23:54 +0200500 /* if it's a number with 'e' or 'E', get rid of it first */
501 if ((str = strnchr(&data[len], 'e', r)) || (str = strnchr(&data[len], 'E', r))) {
502 str = lyjson_convert_enumber(&data[len], r, str);
503 if (!str) {
504 return 0;
505 }
506 leaf->value_str = lydict_insert_zc(ctx, str);
507 } else {
508 leaf->value_str = lydict_insert(ctx, &data[len], r);
509 }
Radek Krejci5449d472015-10-26 14:35:56 +0100510 len += r;
511 } else if (data[len] == 'f' || data[len] == 't') {
512 /* boolean */
513 r = lyjson_parse_boolean(&data[len]);
514 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100515 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100516 return 0;
517 }
518 leaf->value_str = lydict_insert(ctx, &data[len], r);
519 len += r;
520 } else if (!strncmp(&data[len], "[null]", 6)) {
521 /* empty */
Michal Vasko44913842016-04-13 14:20:41 +0200522 leaf->value_str = lydict_insert(ctx, "", 0);
Radek Krejci5449d472015-10-26 14:35:56 +0100523 len += 6;
524 } else {
525 /* error */
Radek Krejci48464ed2016-03-17 15:44:09 +0100526 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (unexpected value)");
Radek Krejci5449d472015-10-26 14:35:56 +0100527 return 0;
528 }
529
Radek Krejci1899d6a2016-11-03 13:48:07 +0100530 /* the value is here converted to a JSON format if needed in case of LY_TYPE_IDENT and LY_TYPE_INST or to a
531 * canonical form of the value */
532 if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, NULL, NULL, leaf,
533 resolvable, 0)) {
Radek Krejci23238922015-10-27 17:13:34 +0100534 ly_errno = LY_EVALID;
535 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100536 }
537
538 if (leaf->schema->nodetype == LYS_LEAFLIST) {
539 /* repeat until end-array */
540 len += skip_ws(&data[len]);
541 if (data[len] == ',') {
542 /* another instance of the leaf-list */
543 new = calloc(1, sizeof(struct lyd_node_leaf_list));
Michal Vasko253035f2015-12-17 16:58:13 +0100544 if (!new) {
545 LOGMEM;
546 return 0;
547 }
Radek Krejci5449d472015-10-26 14:35:56 +0100548 new->parent = leaf->parent;
549 new->prev = (struct lyd_node *)leaf;
550 leaf->next = (struct lyd_node *)new;
551
552 /* fix the "last" pointer */
Radek Krejcibd930122016-08-10 13:28:26 +0200553 first_sibling->prev = (struct lyd_node *)new;
Radek Krejci5449d472015-10-26 14:35:56 +0100554
555 new->schema = leaf->schema;
556
557 /* repeat value parsing */
558 leaf = new;
559 len++;
560 goto repeat;
561 } else if (data[len] == ']') {
562 len++;
563 len += skip_ws(&data[len]);
564 } else {
565 /* something unexpected */
Radek Krejci48464ed2016-03-17 15:44:09 +0100566 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expecting value-separator or end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100567 return 0;
568 }
569 }
570
571 len += skip_ws(&data[len]);
572 return len;
573}
574
575static unsigned int
Radek Krejci88f29302015-10-30 15:42:33 +0100576json_parse_attr(struct lys_module *parent_module, struct lyd_attr **attr, const char *data)
577{
578 unsigned int len = 0, r;
579 char *str = NULL, *name, *prefix, *value;
580 struct lys_module *module = parent_module;
581 struct lyd_attr *attr_new, *attr_last = NULL;
582
Radek Krejcide9d92c2015-10-30 15:59:59 +0100583 *attr = NULL;
584
Radek Krejci88f29302015-10-30 15:42:33 +0100585 if (data[len] != '{') {
586 if (!strncmp(&data[len], "null", 4)) {
Radek Krejci88f29302015-10-30 15:42:33 +0100587 len += 4;
588 len += skip_ws(&data[len]);
589 return len;
590 }
Radek Krejci48464ed2016-03-17 15:44:09 +0100591 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing begin-object)");
Radek Krejci88f29302015-10-30 15:42:33 +0100592 goto error;
593 }
594
595repeat:
596 len++;
597 len += skip_ws(&data[len]);
598
599 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100600 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 +0100601 return 0;
602 }
603 len++;
604 str = lyjson_parse_text(&data[len], &r);
605 if (!r) {
606 goto error;
607 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100608 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 +0100609 goto error;
610 }
611 if ((name = strchr(str, ':'))) {
612 *name = '\0';
613 name++;
614 prefix = str;
Michal Vasko1e62a092015-12-01 12:27:20 +0100615 module = (struct lys_module *)ly_ctx_get_module(parent_module->ctx, prefix, NULL);
Radek Krejci88f29302015-10-30 15:42:33 +0100616 if (!module) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100617 LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name);
Radek Krejci88f29302015-10-30 15:42:33 +0100618 goto error;
619 }
620 } else {
621 name = str;
622 }
623
624 /* prepare data for parsing node content */
625 len += r + 1;
626 len += skip_ws(&data[len]);
627 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100628 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing name-separator)");
Radek Krejci88f29302015-10-30 15:42:33 +0100629 goto error;
630 }
631 len++;
632 len += skip_ws(&data[len]);
633
634 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100635 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 +0100636 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100637 }
638 len++;
639 value = lyjson_parse_text(&data[len], &r);
640 if (!r) {
641 goto error;
642 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100643 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 +0100644 free(value);
Radek Krejci88f29302015-10-30 15:42:33 +0100645 goto error;
646 }
647 len += r + 1;
648 len += skip_ws(&data[len]);
649
650 attr_new = malloc(sizeof **attr);
Michal Vasko253035f2015-12-17 16:58:13 +0100651 if (!attr_new) {
652 LOGMEM;
653 goto error;
654 }
Radek Krejci88f29302015-10-30 15:42:33 +0100655 attr_new->module = module;
656 attr_new->next = NULL;
657 attr_new->name = lydict_insert(module->ctx, name, 0);
658 attr_new->value = lydict_insert_zc(module->ctx, value);
659 if (!attr_last) {
660 *attr = attr_last = attr_new;
661 } else {
662 attr_last->next = attr_new;
663 attr_last = attr_new;
664 }
665
666 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100667 str = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100668
669 if (data[len] == ',') {
670 goto repeat;
671 } else if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100672 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing end-object)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100673 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100674 }
675 len++;
676 len += skip_ws(&data[len]);
677
678 return len;
679
680error:
681 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100682 if (*attr) {
683 lyd_free_attr((*attr)->module->ctx, NULL, *attr, 1);
684 *attr = NULL;
685 }
Radek Krejci88f29302015-10-30 15:42:33 +0100686 return 0;
687}
688
689struct attr_cont {
690 struct attr_cont *next;
691 struct lyd_attr *attr;
692 struct lys_node *schema;
693 unsigned int index; /** non-zero only in case of leaf-list */
694};
695
Radek Krejcic4831272015-11-01 19:26:34 +0100696static int
697store_attrs(struct ly_ctx *ctx, struct attr_cont *attrs, struct lyd_node *first)
698{
699 struct lyd_node *diter;
700 struct attr_cont *iter;
701 unsigned int flag_leaflist = 0;
702
703 while (attrs) {
704 iter = attrs;
705 attrs = attrs->next;
706
707 if (iter->index) {
708 flag_leaflist = 1;
709 }
710
711 LY_TREE_FOR(first, diter) {
712 if (iter->schema != diter->schema) {
713 continue;
714 }
715
716 if (flag_leaflist && flag_leaflist != iter->index) {
717 flag_leaflist++;
718 continue;
719 }
720
721 /* we have match */
722 if (diter->attr) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100723 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, diter,
Radek Krejciadb57612016-02-16 13:34:34 +0100724 "attribute (multiple attribute definitions belong to a single element)");
Radek Krejcic4831272015-11-01 19:26:34 +0100725 free(iter);
726 goto error;
727 }
728
729 diter->attr = iter->attr;
730 break;
731 }
732
733 if (!diter) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100734 LOGVAL(LYE_XML_MISS, LY_VLOG_NONE, NULL, "element for the specified attribute", iter->attr->name);
Radek Krejcic4831272015-11-01 19:26:34 +0100735 lyd_free_attr(iter->schema->module->ctx, NULL, iter->attr, 1);
736 free(iter);
737 goto error;
738 }
739 free(iter);
740 }
741
742 return 0;
743
744error:
745
746 while (attrs) {
747 iter = attrs;
748 attrs = attrs->next;
749
750 lyd_free_attr(ctx, NULL, iter->attr, 1);
751 free(iter);
752 }
753
754 return -1;
755}
756
Radek Krejci88f29302015-10-30 15:42:33 +0100757static unsigned int
Michal Vasko36ef6932015-12-01 14:30:17 +0100758json_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 +0200759 struct lyd_node *first_sibling, struct lyd_node *prev, struct attr_cont **attrs, int options,
Michal Vaskoafa7a642016-10-18 15:11:38 +0200760 struct unres_data *unres, struct lyd_node **act_notif)
Radek Krejci5449d472015-10-26 14:35:56 +0100761{
762 unsigned int len = 0;
763 unsigned int r;
Radek Krejcic4831272015-11-01 19:26:34 +0100764 unsigned int flag_leaflist = 0;
Radek Krejci61767ca2016-09-19 14:21:55 +0200765 int i, pos;
Radek Krejci88f29302015-10-30 15:42:33 +0100766 char *name, *prefix = NULL, *str = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100767 const struct lys_module *module = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +0100768 struct lys_node *schema = NULL;
Radek Krejcibd930122016-08-10 13:28:26 +0200769 struct lyd_node *result = NULL, *new, *list, *diter = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100770 struct lyd_attr *attr;
Radek Krejcic4831272015-11-01 19:26:34 +0100771 struct attr_cont *attrs_aux;
Radek Krejci5449d472015-10-26 14:35:56 +0100772
773 /* each YANG data node representation starts with string (node identifier) */
774 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100775 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100776 "JSON data (missing quotation-mark at the beginning of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100777 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100778 }
779 len++;
780
Radek Krejci23238922015-10-27 17:13:34 +0100781 str = lyjson_parse_text(&data[len], &r);
Radek Krejci5449d472015-10-26 14:35:56 +0100782 if (!r) {
783 goto error;
784 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100785 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100786 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100787 goto error;
788 }
Radek Krejci23238922015-10-27 17:13:34 +0100789 if ((name = strchr(str, ':'))) {
790 *name = '\0';
791 name++;
792 prefix = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100793 if (prefix[0] == '@') {
794 prefix++;
795 }
Radek Krejci23238922015-10-27 17:13:34 +0100796 } else {
797 name = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100798 if (name[0] == '@') {
799 name++;
800 }
Radek Krejci5449d472015-10-26 14:35:56 +0100801 }
802
Radek Krejci5449d472015-10-26 14:35:56 +0100803 /* prepare data for parsing node content */
804 len += r + 1;
805 len += skip_ws(&data[len]);
806 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100807 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing name-separator)");
Radek Krejci5449d472015-10-26 14:35:56 +0100808 goto error;
809 }
810 len++;
811 len += skip_ws(&data[len]);
812
Radek Krejci88f29302015-10-30 15:42:33 +0100813 if (str[0] == '@' && !str[1]) {
814 /* process attribute of the parent object (container or list) */
Radek Krejci2a0efef2016-03-24 15:10:40 +0100815 if (!(*parent)) {
816 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute with no corresponding element to belongs to");
Radek Krejci88f29302015-10-30 15:42:33 +0100817 goto error;
818 }
819
820 r = json_parse_attr((*parent)->schema->module, &attr, &data[len]);
821 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100822 LOGPATH(LY_VLOG_LYD, (*parent));
Radek Krejci88f29302015-10-30 15:42:33 +0100823 goto error;
824 }
825 len += r;
826
827 if ((*parent)->attr) {
828 lyd_free_attr(ctx, NULL, attr, 1);
829 } else {
830 (*parent)->attr = attr;
831 }
Radek Krejcic4831272015-11-01 19:26:34 +0100832 free(str);
833 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100834 }
835
Radek Krejci5449d472015-10-26 14:35:56 +0100836 /* find schema node */
837 if (!(*parent)) {
838 /* starting in root */
839 /* get the proper schema */
840 module = ly_ctx_get_module(ctx, prefix, NULL);
841 if (module) {
842 /* get the proper schema node */
Radek Krejci919a9242016-07-27 08:17:13 +0200843 while ((schema = (struct lys_node *)lys_getnext(schema, NULL, module, 0))) {
Radek Krejci5449d472015-10-26 14:35:56 +0100844 if (!strcmp(schema->name, name)) {
845 break;
846 }
847 }
Radek Krejci88f29302015-10-30 15:42:33 +0100848 } else {
Radek Krejci2a0efef2016-03-24 15:10:40 +0100849 LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name);
Radek Krejci88f29302015-10-30 15:42:33 +0100850 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100851 }
852 } else {
853 /* parsing some internal node, we start with parent's schema pointer */
854 if (prefix) {
855 /* get the proper schema */
856 module = ly_ctx_get_module(ctx, prefix, NULL);
857 if (!module) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100858 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci88f29302015-10-30 15:42:33 +0100859 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100860 }
861 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100862
863 /* go through RPC's input/output following the options' data type */
864 if ((*parent)->schema->nodetype == LYS_RPC) {
865 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, module, LYS_GETNEXT_WITHINOUT))) {
Michal Vaskoafa7a642016-10-18 15:11:38 +0200866 if ((options & LYD_OPT_RPC) && (schema->nodetype == LYS_INPUT)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100867 break;
Michal Vaskoafa7a642016-10-18 15:11:38 +0200868 } else if ((options & LYD_OPT_RPCREPLY) && (schema->nodetype == LYS_OUTPUT)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100869 break;
870 }
871 }
872 if (!schema) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100873 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100874 goto error;
875 }
876 schema_parent = schema;
877 schema = NULL;
878 }
879
Michal Vasko36ef6932015-12-01 14:30:17 +0100880 if (schema_parent) {
881 while ((schema = (struct lys_node *)lys_getnext(schema, schema_parent, module, 0))) {
882 if (!strcmp(schema->name, name)) {
883 break;
884 }
885 }
886 } else {
887 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, module, 0))) {
888 if (!strcmp(schema->name, name)) {
889 break;
890 }
Radek Krejci5449d472015-10-26 14:35:56 +0100891 }
892 }
893 }
Radek Krejci27fe55e2016-09-13 17:13:35 +0200894 if (!schema || !lys_node_module(schema)->implemented) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100895 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci88f29302015-10-30 15:42:33 +0100896 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100897 }
Radek Krejci88f29302015-10-30 15:42:33 +0100898
899 if (str[0] == '@') {
Radek Krejci88f29302015-10-30 15:42:33 +0100900 /* attribute for some sibling node */
901 if (data[len] == '[') {
902 flag_leaflist = 1;
903 len++;
904 len += skip_ws(&data[len]);
905 }
906
907attr_repeat:
908 r = json_parse_attr(schema->module, &attr, &data[len]);
909 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100910 LOGPATH(LY_VLOG_LYD, (*parent));
Radek Krejci88f29302015-10-30 15:42:33 +0100911 goto error;
912 }
913 len += r;
914
915 if (attr) {
Radek Krejcic4831272015-11-01 19:26:34 +0100916 attrs_aux = malloc(sizeof *attrs_aux);
Michal Vasko253035f2015-12-17 16:58:13 +0100917 if (!attrs_aux) {
918 LOGMEM;
919 goto error;
920 }
Radek Krejcic4831272015-11-01 19:26:34 +0100921 attrs_aux->attr = attr;
922 attrs_aux->index = flag_leaflist;
923 attrs_aux->schema = schema;
924 attrs_aux->next = *attrs;
925 *attrs = attrs_aux;
Radek Krejci88f29302015-10-30 15:42:33 +0100926 } else if (!flag_leaflist) {
927 /* error */
Radek Krejci48464ed2016-03-17 15:44:09 +0100928 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "attribute data");
Radek Krejci88f29302015-10-30 15:42:33 +0100929 goto error;
930 }
931
932 if (flag_leaflist) {
933 if (data[len] == ',') {
934 len++;
935 len += skip_ws(&data[len]);
936 flag_leaflist++;
937 goto attr_repeat;
938 } else if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100939 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing end-array)");
Radek Krejci88f29302015-10-30 15:42:33 +0100940 goto error;
941 }
942 len++;
943 len += skip_ws(&data[len]);
944 }
945
Radek Krejcic4831272015-11-01 19:26:34 +0100946 free(str);
947 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100948 }
949
Radek Krejci5449d472015-10-26 14:35:56 +0100950 switch (schema->nodetype) {
951 case LYS_CONTAINER:
952 case LYS_LIST:
953 case LYS_NOTIF:
954 case LYS_RPC:
Michal Vaskoafa7a642016-10-18 15:11:38 +0200955 case LYS_ACTION:
Radek Krejci5449d472015-10-26 14:35:56 +0100956 result = calloc(1, sizeof *result);
957 break;
958 case LYS_LEAF:
959 case LYS_LEAFLIST:
960 result = calloc(1, sizeof(struct lyd_node_leaf_list));
961 break;
962 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200963 case LYS_ANYDATA:
964 result = calloc(1, sizeof(struct lyd_node_anydata));
Radek Krejci5449d472015-10-26 14:35:56 +0100965 break;
966 default:
967 LOGINT;
Radek Krejci595060f2015-10-30 16:29:58 +0100968 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100969 }
Michal Vasko253035f2015-12-17 16:58:13 +0100970 if (!result) {
971 LOGMEM;
972 goto error;
973 }
974
Radek Krejci61767ca2016-09-19 14:21:55 +0200975 result->prev = result;
Radek Krejci5449d472015-10-26 14:35:56 +0100976 result->schema = schema;
Radek Krejci61767ca2016-09-19 14:21:55 +0200977 result->parent = *parent;
978 diter = NULL;
979 if (*parent && (*parent)->child && schema->nodetype == LYS_LEAF && (*parent)->schema->nodetype == LYS_LIST &&
980 (pos = lys_is_key((struct lys_node_list *)(*parent)->schema, (struct lys_node_leaf *)schema))) {
981 /* it is key and we need to insert it into a correct place */
982 for (i = 0, diter = (*parent)->child;
983 diter && i < (pos - 1) && diter->schema->nodetype == LYS_LEAF &&
984 lys_is_key((struct lys_node_list *)(*parent)->schema, (struct lys_node_leaf *)diter->schema);
985 i++, diter = diter->next);
986 if (diter) {
987 /* out of order insertion - insert list's key to the correct position, before the diter */
988 if ((*parent)->child == diter) {
989 (*parent)->child = result;
990 /* update first_sibling */
991 first_sibling = result;
992 }
993 if (diter->prev->next) {
994 diter->prev->next = result;
995 }
996 result->prev = diter->prev;
997 diter->prev = result;
998 result->next = diter;
999 }
1000 }
1001 if (!diter) {
1002 /* simplified (faster) insert as the last node */
1003 if (*parent && !(*parent)->child) {
1004 (*parent)->child = result;
1005 }
1006 if (prev) {
1007 result->prev = prev;
1008 prev->next = result;
1009
1010 /* fix the "last" pointer */
1011 first_sibling->prev = result;
1012 } else {
1013 result->prev = result;
1014 first_sibling = result;
1015 }
1016 }
Radek Krejcica7efb72016-01-18 13:06:01 +01001017 result->validity = LYD_VAL_NOT;
Radek Krejci46165822016-08-26 14:06:27 +02001018 if (resolve_applies_when(schema, 0, NULL)) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01001019 result->when_status = LYD_WHEN;
1020 }
Radek Krejci5449d472015-10-26 14:35:56 +01001021
Radek Krejci5449d472015-10-26 14:35:56 +01001022 /* type specific processing */
1023 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1024 /* type detection and assigning the value */
Radek Krejcibd930122016-08-10 13:28:26 +02001025 r = json_get_value((struct lyd_node_leaf_list *)result, first_sibling, &data[len], options);
Radek Krejci5449d472015-10-26 14:35:56 +01001026 if (!r) {
1027 goto error;
1028 }
Radek Krejci88f29302015-10-30 15:42:33 +01001029 while(result->next) {
1030 result = result->next;
1031 }
1032
Radek Krejci5449d472015-10-26 14:35:56 +01001033 len += r;
1034 len += skip_ws(&data[len]);
Radek Krejcibf2abff2016-08-23 15:51:52 +02001035 } else if (schema->nodetype & LYS_ANYDATA) {
Radek Krejci4ae82942016-09-19 16:41:06 +02001036 r = json_get_anydata((struct lyd_node_anydata *)result, &data[len]);
Radek Krejci5449d472015-10-26 14:35:56 +01001037 if (!r) {
1038 goto error;
1039 }
1040 len += r;
1041 len += skip_ws(&data[len]);
Michal Vaskoafa7a642016-10-18 15:11:38 +02001042 } else if (schema->nodetype & (LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1043 if (schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1044 if (!(options & LYD_OPT_RPC) || *act_notif) {
1045 LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name);
1046 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Unexpected %s node \"%s\".",
1047 (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name);
1048 goto error;
1049 }
1050 *act_notif = result;
1051 } else if (schema->nodetype == LYS_NOTIF) {
1052 if (!(options & LYD_OPT_NOTIF) || *act_notif) {
1053 LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name);
1054 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Unexpected notification node \"%s\".", schema->name);
1055 goto error;
1056 }
1057 *act_notif = result;
1058 }
1059
Radek Krejci5449d472015-10-26 14:35:56 +01001060 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001061 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001062 goto error;
1063 }
1064 len++;
1065 len += skip_ws(&data[len]);
1066
1067 if (data[len] != '}') {
1068 /* non-empty container */
Radek Krejcic4831272015-11-01 19:26:34 +01001069 len--;
1070 diter = NULL;
1071 attrs_aux = NULL;
1072 do {
1073 len++;
1074 len += skip_ws(&data[len]);
1075
Michal Vaskoafa7a642016-10-18 15:11:38 +02001076 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 +01001077 if (!r) {
1078 goto error;
1079 }
1080 len += r;
1081
1082 if (result->child) {
1083 diter = result->child->prev;
1084 }
1085 } while(data[len] == ',');
1086
1087 /* store attributes */
1088 if (store_attrs(ctx, attrs_aux, result->child)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001089 goto error;
1090 }
Radek Krejci5449d472015-10-26 14:35:56 +01001091 }
1092
1093 if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001094 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001095 goto error;
1096 }
1097 len++;
1098 len += skip_ws(&data[len]);
1099
Radek Krejcifb7156e2016-10-27 13:39:56 +02001100 /* if we have empty non-presence container, mark it as default */
Radek Krejci2537fd32016-09-07 16:22:41 +02001101 if (schema->nodetype == LYS_CONTAINER && !result->child &&
Radek Krejcid3e73722016-05-23 12:24:55 +02001102 !result->attr && !((struct lys_node_container *)schema)->presence) {
Radek Krejcifb7156e2016-10-27 13:39:56 +02001103 result->dflt = 1;
Radek Krejci0c0086a2016-03-24 15:20:28 +01001104 }
1105
Radek Krejci5449d472015-10-26 14:35:56 +01001106 } else if (schema->nodetype == LYS_LIST) {
1107 if (data[len] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001108 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001109 goto error;
1110 }
1111
1112 list = result;
1113 do {
1114 len++;
1115 len += skip_ws(&data[len]);
Radek Krejci23238922015-10-27 17:13:34 +01001116
Radek Krejcic4831272015-11-01 19:26:34 +01001117 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001118 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001119 "JSON data (missing list instance's begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001120 goto error;
1121 }
Radek Krejcic4831272015-11-01 19:26:34 +01001122 diter = NULL;
1123 attrs_aux = NULL;
1124 do {
1125 len++;
1126 len += skip_ws(&data[len]);
1127
Michal Vaskoafa7a642016-10-18 15:11:38 +02001128 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 +01001129 if (!r) {
1130 goto error;
1131 }
1132 len += r;
1133
Radek Krejci52934692015-11-01 20:08:15 +01001134 if (list->child) {
Radek Krejcic4831272015-11-01 19:26:34 +01001135 diter = list->child->prev;
1136 }
1137 } while(data[len] == ',');
1138
1139 /* store attributes */
Radek Krejci52934692015-11-01 20:08:15 +01001140 if (store_attrs(ctx, attrs_aux, list->child)) {
Radek Krejcic4831272015-11-01 19:26:34 +01001141 goto error;
1142 }
1143
1144 if (data[len] != '}') {
1145 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001146 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001147 "JSON data (missing list instance's end-object)");
Radek Krejcic4831272015-11-01 19:26:34 +01001148 goto error;
1149 }
1150 len++;
Radek Krejci5449d472015-10-26 14:35:56 +01001151 len += skip_ws(&data[len]);
1152
1153 if (data[len] == ',') {
Radek Krejci93fab982016-02-03 15:58:19 +01001154 /* various validation checks */
Radek Krejci00a0e712016-10-26 10:24:46 +02001155 ly_err_clean(1);
Radek Krejci2d5525d2016-04-04 15:43:30 +02001156 if (!(options & LYD_OPT_TRUSTED) &&
1157 (lyv_data_content(list, options, unres) ||
Radek Krejci61767ca2016-09-19 14:21:55 +02001158 lyv_multicases(list, NULL, prev ? &first_sibling : NULL, 0, NULL))) {
Radek Krejci93fab982016-02-03 15:58:19 +01001159 if (ly_errno) {
1160 goto error;
1161 }
1162 }
1163 /* validation successful */
1164 list->validity = LYD_VAL_OK;
1165
Radek Krejci5449d472015-10-26 14:35:56 +01001166 /* another instance of the list */
1167 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001168 if (!new) {
1169 goto error;
1170 }
Radek Krejci5449d472015-10-26 14:35:56 +01001171 new->parent = list->parent;
1172 new->prev = list;
1173 list->next = new;
1174
1175 /* fix the "last" pointer */
Radek Krejci2d5525d2016-04-04 15:43:30 +02001176 first_sibling->prev = new;
Radek Krejci5449d472015-10-26 14:35:56 +01001177
1178 new->schema = list->schema;
Radek Krejci5449d472015-10-26 14:35:56 +01001179 list = new;
1180 }
1181 } while (data[len] == ',');
Michal Vaskod171d4e2016-06-06 13:33:19 +02001182 result = first_sibling;
Radek Krejci5449d472015-10-26 14:35:56 +01001183
1184 if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001185 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001186 goto error;
1187 }
1188 len++;
1189 len += skip_ws(&data[len]);
1190 }
1191
1192 /* various validation checks */
Michal Vasko10e586f2016-05-18 13:25:30 +02001193 if (!(options & LYD_OPT_TRUSTED) && lyv_data_context(result, options, unres)) {
1194 goto error;
1195 }
1196
Radek Krejci00a0e712016-10-26 10:24:46 +02001197 ly_err_clean(1);
Radek Krejci2d5525d2016-04-04 15:43:30 +02001198 if (!(options & LYD_OPT_TRUSTED) &&
1199 (lyv_data_content(result, options, unres) ||
Radek Krejci61767ca2016-09-19 14:21:55 +02001200 lyv_multicases(result, NULL, prev ? &first_sibling : NULL, 0, NULL))) {
Radek Krejci5449d472015-10-26 14:35:56 +01001201 if (ly_errno) {
1202 goto error;
1203 }
1204 }
1205
Radek Krejcica7efb72016-01-18 13:06:01 +01001206 /* validation successful */
Radek Krejci63b79c82016-08-10 10:09:33 +02001207 if (result->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
1208 /* postpone checking when there will be all list/leaflist instances */
1209 result->validity = LYD_VAL_UNIQUE;
1210 } else {
1211 result->validity = LYD_VAL_OK;
1212 }
Radek Krejcica7efb72016-01-18 13:06:01 +01001213
Radek Krejci88f29302015-10-30 15:42:33 +01001214 if (!(*parent)) {
1215 *parent = result;
1216 }
1217
Radek Krejcide9d92c2015-10-30 15:59:59 +01001218 free(str);
Radek Krejci5449d472015-10-26 14:35:56 +01001219 return len;
1220
1221error:
Radek Krejci0c0086a2016-03-24 15:20:28 +01001222 len = 0;
Radek Krejci0c0086a2016-03-24 15:20:28 +01001223 /* cleanup */
1224 for (i = unres->count - 1; i >= 0; i--) {
1225 /* remove unres items connected with the node being removed */
1226 if (unres->node[i] == result) {
1227 unres_data_del(unres, i);
1228 }
Radek Krejci88f29302015-10-30 15:42:33 +01001229 }
1230 while (*attrs) {
Radek Krejcic4831272015-11-01 19:26:34 +01001231 attrs_aux = *attrs;
Radek Krejci88f29302015-10-30 15:42:33 +01001232 *attrs = (*attrs)->next;
1233
Radek Krejcic4831272015-11-01 19:26:34 +01001234 lyd_free_attr(ctx, NULL, attrs_aux->attr, 1);
1235 free(attrs_aux);
Radek Krejci88f29302015-10-30 15:42:33 +01001236 }
1237
Radek Krejci5449d472015-10-26 14:35:56 +01001238 lyd_free(result);
Radek Krejci88f29302015-10-30 15:42:33 +01001239 free(str);
1240
Radek Krejci0c0086a2016-03-24 15:20:28 +01001241 return len;
Radek Krejci5449d472015-10-26 14:35:56 +01001242}
1243
1244struct lyd_node *
Michal Vasko945b96b2016-10-18 11:49:12 +02001245lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act,
1246 const struct lyd_node *data_tree)
Radek Krejci5449d472015-10-26 14:35:56 +01001247{
Michal Vaskoafa7a642016-10-18 15:11:38 +02001248 struct lyd_node *result = NULL, *next, *iter, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001249 struct unres_data *unres = NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001250 unsigned int len = 0, r;
Michal Vaskoafa7a642016-10-18 15:11:38 +02001251 int i, act_cont = 0;
Radek Krejcic4831272015-11-01 19:26:34 +01001252 struct attr_cont *attrs = NULL;
Radek Krejci63b79c82016-08-10 10:09:33 +02001253 struct ly_set *set;
Radek Krejci5449d472015-10-26 14:35:56 +01001254
Radek Krejci00a0e712016-10-26 10:24:46 +02001255 ly_err_clean(1);
Radek Krejci2342cf62016-01-29 16:48:23 +01001256
Michal Vaskoafa7a642016-10-18 15:11:38 +02001257 if (!ctx || !data) {
Radek Krejci5449d472015-10-26 14:35:56 +01001258 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1259 return NULL;
1260 }
1261
Radek Krejcic4831272015-11-01 19:26:34 +01001262 /* skip leading whitespaces */
1263 len += skip_ws(&data[len]);
1264
Michal Vasko24d982f2016-04-18 15:13:58 +02001265 /* no data (or whitespaces only) are fine */
1266 if (!data[len]) {
1267 lyd_validate(&result, options, ctx);
1268 return result;
1269 }
1270
Radek Krejcic4831272015-11-01 19:26:34 +01001271 /* expect top-level { */
1272 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001273 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level begin-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001274 return NULL;
1275 }
1276
1277 unres = calloc(1, sizeof *unres);
1278 if (!unres) {
1279 LOGMEM;
1280 return NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001281 }
1282
Michal Vaskoafa7a642016-10-18 15:11:38 +02001283 /* create RPC/action reply part that is not in the parsed data */
1284 if (rpc_act) {
1285 assert(options & LYD_OPT_RPCREPLY);
1286 if (rpc_act->schema->nodetype == LYS_RPC) {
1287 /* RPC request */
1288 reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0);
1289 } else {
1290 /* action request */
1291 reply_top = lyd_dup(rpc_act, 1);
1292 LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) {
1293 if (reply_parent->schema->nodetype == LYS_ACTION) {
1294 break;
1295 }
1296 LY_TREE_DFS_END(reply_top, iter, reply_parent);
1297 }
1298 if (!reply_parent) {
1299 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__);
1300 lyd_free_withsiblings(reply_top);
1301 goto error;
1302 }
1303 lyd_free_withsiblings(reply_parent->child);
1304 }
1305 }
1306
1307 iter = NULL;
1308 next = reply_parent;
Radek Krejcic4831272015-11-01 19:26:34 +01001309 do {
1310 len++;
1311 len += skip_ws(&data[len]);
1312
Michal Vaskoafa7a642016-10-18 15:11:38 +02001313 if (!act_cont) {
1314 if (!strncmp(&data[len], "\"yang:action\"", 13)) {
1315 len += 13;
1316 len += skip_ws(&data[len]);
1317 if (data[len] != ':') {
1318 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level begin-object)");
1319 lyd_free_withsiblings(reply_top);
1320 goto error;
1321 }
1322 ++len;
1323 len += skip_ws(&data[len]);
1324 if (data[len] != '{') {
1325 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level yang:action object)");
1326 lyd_free_withsiblings(reply_top);
1327 goto error;
1328 }
1329 ++len;
1330 len += skip_ws(&data[len]);
1331
1332 act_cont = 1;
1333 } else {
1334 act_cont = -1;
1335 }
1336 }
1337
1338 r = json_parse_data(ctx, &data[len], NULL, &next, result, iter, &attrs, options, unres, &act_notif);
Radek Krejcic4831272015-11-01 19:26:34 +01001339 if (!r) {
Michal Vaskoafa7a642016-10-18 15:11:38 +02001340 lyd_free_withsiblings(reply_top);
Michal Vasko24d982f2016-04-18 15:13:58 +02001341 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001342 }
1343 len += r;
1344
1345 if (!result) {
1346 result = next;
1347 }
1348 if (next) {
1349 iter = next;
1350 }
1351 next = NULL;
Michal Vaskoafa7a642016-10-18 15:11:38 +02001352 } while (data[len] == ',');
Radek Krejcic4831272015-11-01 19:26:34 +01001353
1354 if (data[len] != '}') {
1355 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001356 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001357 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001358 }
1359 len++;
1360 len += skip_ws(&data[len]);
1361
Michal Vaskoafa7a642016-10-18 15:11:38 +02001362 if (act_cont == 1) {
1363 if (data[len] != '}') {
1364 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)");
1365 goto error;
1366 }
1367 len++;
1368 len += skip_ws(&data[len]);
1369 }
1370
Radek Krejcic4831272015-11-01 19:26:34 +01001371 /* store attributes */
1372 if (store_attrs(ctx, attrs, result)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001373 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001374 }
Radek Krejci5449d472015-10-26 14:35:56 +01001375
Michal Vaskoafa7a642016-10-18 15:11:38 +02001376 if (reply_top) {
1377 result = reply_top;
1378 }
1379
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001380 if (!result) {
1381 LOGERR(LY_EVALID, "Model for the data to be linked with not found.");
Michal Vasko24d982f2016-04-18 15:13:58 +02001382 goto error;
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001383 }
1384
Michal Vaskoafa7a642016-10-18 15:11:38 +02001385 if ((options & LYD_OPT_RPCREPLY) && (rpc_act->schema->nodetype != LYS_RPC)) {
1386 /* action reply */
1387 act_notif = reply_parent;
1388 } else if ((options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) && !act_notif) {
1389 ly_vecode = LYVE_INELEM;
1390 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing %s node.", (options & LYD_OPT_RPC ? "action" : "notification"));
1391 goto error;
1392 }
1393
Radek Krejci63b79c82016-08-10 10:09:33 +02001394 /* check for uniquness of top-level lists/leaflists because
1395 * only the inner instances were tested in lyv_data_content() */
1396 set = ly_set_new();
1397 LY_TREE_FOR(result, iter) {
1398 if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) {
1399 continue;
1400 }
1401
1402 /* check each list/leaflist only once */
1403 i = set->number;
1404 if (ly_set_add(set, iter->schema, 0) != i) {
1405 /* already checked */
1406 continue;
1407 }
1408
1409 if (lyv_data_unique(iter, result)) {
1410 ly_set_free(set);
1411 goto error;
1412 }
1413 }
1414 ly_set_free(set);
1415
Radek Krejci46165822016-08-26 14:06:27 +02001416 /* add/validate default values, unres */
Michal Vaskoafa7a642016-10-18 15:11:38 +02001417 if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001418 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001419 }
1420
Radek Krejci46165822016-08-26 14:06:27 +02001421 /* check for missing top level mandatory nodes */
Michal Vaskoafa7a642016-10-18 15:11:38 +02001422 if (!(options & LYD_OPT_TRUSTED) && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001423 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001424 }
1425
Radek Krejci5449d472015-10-26 14:35:56 +01001426 free(unres->node);
1427 free(unres->type);
Radek Krejci5449d472015-10-26 14:35:56 +01001428 free(unres);
1429
1430 return result;
Michal Vasko24d982f2016-04-18 15:13:58 +02001431
1432error:
1433 lyd_free_withsiblings(result);
1434 free(unres->node);
1435 free(unres->type);
1436 free(unres);
1437
1438 return NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001439}