blob: 69ba657d653b0a75c40600ef1209ad41c1d59ca0 [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 Krejci4ae82942016-09-19 16:41:06 +0200385 unsigned int len = 0, start, stop, c;
Radek Krejci5449d472015-10-26 14:35:56 +0100386
Radek Krejci4ae82942016-09-19 16:41:06 +0200387 /* anydata (as well as meaningful anyxml) is supposed to be encoded as object */
388 if (data[len] != '{') {
389 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any, "Anydata/anyxml content (not an object)");
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200390 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100391 }
392
Radek Krejci4ae82942016-09-19 16:41:06 +0200393 /* count opening '{' and closing '}' to get the end of the object without its parsing */
394 c = 1;
395 len++;
396 len += skip_ws(&data[len]);
397 start = stop = len;
398 while (data[len] && c) {
399 switch (data[len]) {
400 case '{':
401 c++;
402 break;
403 case '}':
404 c--;
405 break;
406 default:
407 if (!isspace(data[len])) {
408 stop = len;
409 }
410 }
411 len++;
412 }
413 if (c) {
414 LOGVAL(LYE_EOF, LY_VLOG_LYD, any);
415 return 0;
416 }
417 any->value_type = LYD_ANYDATA_JSON;
418 any->value.str = lydict_insert(any->schema->module->ctx, &data[start], stop - start + 1);
419
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200420 len += skip_ws(&data[len]);
421 return len;
Radek Krejci5449d472015-10-26 14:35:56 +0100422}
423
424static unsigned int
Radek Krejcibd930122016-08-10 13:28:26 +0200425json_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 +0100426{
Radek Krejcibd930122016-08-10 13:28:26 +0200427 struct lyd_node_leaf_list *new;
Radek Krejci37b756f2016-01-18 10:15:03 +0100428 struct lys_type *stype;
Radek Krejci5449d472015-10-26 14:35:56 +0100429 struct ly_ctx *ctx;
430 unsigned int len = 0, r;
Radek Krejci37b756f2016-01-18 10:15:03 +0100431 int resolve;
Radek Krejci5449d472015-10-26 14:35:56 +0100432 char *str;
433
Radek Krejci0b7704f2016-03-18 12:16:14 +0100434 assert(leaf && data);
Radek Krejci5449d472015-10-26 14:35:56 +0100435 ctx = leaf->schema->module->ctx;
436
Radek Krejci92ece002016-04-04 15:45:05 +0200437 if (options & (LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) {
Radek Krejci23238922015-10-27 17:13:34 +0100438 resolve = 0;
439 } else {
440 resolve = 1;
441 }
442
Radek Krejci5449d472015-10-26 14:35:56 +0100443 stype = &((struct lys_node_leaf *)leaf->schema)->type;
Radek Krejci23238922015-10-27 17:13:34 +0100444
Radek Krejci5449d472015-10-26 14:35:56 +0100445 if (leaf->schema->nodetype == LYS_LEAFLIST) {
446 /* expecting begin-array */
447 if (data[len++] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100448 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expected begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100449 return 0;
450 }
451
452repeat:
453 len += skip_ws(&data[len]);
454 }
455
456 /* will be changed in case of union */
457 leaf->value_type = stype->base;
458
459 if (data[len] == '"') {
460 /* string representations */
Michal Vasko6baed1c2016-05-18 13:24:44 +0200461 ++len;
Radek Krejci5449d472015-10-26 14:35:56 +0100462 str = lyjson_parse_text(&data[len], &r);
Radek Krejci23238922015-10-27 17:13:34 +0100463 if (!str) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100464 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100465 return 0;
466 }
467 leaf->value_str = lydict_insert_zc(ctx, str);
468 if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100469 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf,
Radek Krejciadb57612016-02-16 13:34:34 +0100470 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100471 return 0;
472 }
473 len += r + 1;
474 } else if (data[len] == '-' || isdigit(data[len])) {
475 /* numeric type */
476 r = lyjson_parse_number(&data[len]);
477 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100478 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100479 return 0;
480 }
Michal Vasko1e676ca2016-06-24 15:23:54 +0200481 /* if it's a number with 'e' or 'E', get rid of it first */
482 if ((str = strnchr(&data[len], 'e', r)) || (str = strnchr(&data[len], 'E', r))) {
483 str = lyjson_convert_enumber(&data[len], r, str);
484 if (!str) {
485 return 0;
486 }
487 leaf->value_str = lydict_insert_zc(ctx, str);
488 } else {
489 leaf->value_str = lydict_insert(ctx, &data[len], r);
490 }
Radek Krejci5449d472015-10-26 14:35:56 +0100491 len += r;
492 } else if (data[len] == 'f' || data[len] == 't') {
493 /* boolean */
494 r = lyjson_parse_boolean(&data[len]);
495 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100496 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100497 return 0;
498 }
499 leaf->value_str = lydict_insert(ctx, &data[len], r);
500 len += r;
501 } else if (!strncmp(&data[len], "[null]", 6)) {
502 /* empty */
Michal Vasko44913842016-04-13 14:20:41 +0200503 leaf->value_str = lydict_insert(ctx, "", 0);
Radek Krejci5449d472015-10-26 14:35:56 +0100504 len += 6;
505 } else {
506 /* error */
Radek Krejci48464ed2016-03-17 15:44:09 +0100507 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (unexpected value)");
Radek Krejci5449d472015-10-26 14:35:56 +0100508 return 0;
509 }
510
Radek Krejci0b7704f2016-03-18 12:16:14 +0100511 if (lyp_parse_value(leaf, NULL, resolve)) {
Radek Krejci23238922015-10-27 17:13:34 +0100512 ly_errno = LY_EVALID;
513 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100514 }
515
516 if (leaf->schema->nodetype == LYS_LEAFLIST) {
517 /* repeat until end-array */
518 len += skip_ws(&data[len]);
519 if (data[len] == ',') {
520 /* another instance of the leaf-list */
521 new = calloc(1, sizeof(struct lyd_node_leaf_list));
Michal Vasko253035f2015-12-17 16:58:13 +0100522 if (!new) {
523 LOGMEM;
524 return 0;
525 }
Radek Krejci5449d472015-10-26 14:35:56 +0100526 new->parent = leaf->parent;
527 new->prev = (struct lyd_node *)leaf;
528 leaf->next = (struct lyd_node *)new;
529
530 /* fix the "last" pointer */
Radek Krejcibd930122016-08-10 13:28:26 +0200531 first_sibling->prev = (struct lyd_node *)new;
Radek Krejci5449d472015-10-26 14:35:56 +0100532
533 new->schema = leaf->schema;
534
535 /* repeat value parsing */
536 leaf = new;
537 len++;
538 goto repeat;
539 } else if (data[len] == ']') {
540 len++;
541 len += skip_ws(&data[len]);
542 } else {
543 /* something unexpected */
Radek Krejci48464ed2016-03-17 15:44:09 +0100544 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expecting value-separator or end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100545 return 0;
546 }
547 }
548
549 len += skip_ws(&data[len]);
550 return len;
551}
552
553static unsigned int
Radek Krejci88f29302015-10-30 15:42:33 +0100554json_parse_attr(struct lys_module *parent_module, struct lyd_attr **attr, const char *data)
555{
556 unsigned int len = 0, r;
557 char *str = NULL, *name, *prefix, *value;
558 struct lys_module *module = parent_module;
559 struct lyd_attr *attr_new, *attr_last = NULL;
560
Radek Krejcide9d92c2015-10-30 15:59:59 +0100561 *attr = NULL;
562
Radek Krejci88f29302015-10-30 15:42:33 +0100563 if (data[len] != '{') {
564 if (!strncmp(&data[len], "null", 4)) {
Radek Krejci88f29302015-10-30 15:42:33 +0100565 len += 4;
566 len += skip_ws(&data[len]);
567 return len;
568 }
Radek Krejci48464ed2016-03-17 15:44:09 +0100569 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing begin-object)");
Radek Krejci88f29302015-10-30 15:42:33 +0100570 goto error;
571 }
572
573repeat:
574 len++;
575 len += skip_ws(&data[len]);
576
577 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100578 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 +0100579 return 0;
580 }
581 len++;
582 str = lyjson_parse_text(&data[len], &r);
583 if (!r) {
584 goto error;
585 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100586 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 +0100587 goto error;
588 }
589 if ((name = strchr(str, ':'))) {
590 *name = '\0';
591 name++;
592 prefix = str;
Michal Vasko1e62a092015-12-01 12:27:20 +0100593 module = (struct lys_module *)ly_ctx_get_module(parent_module->ctx, prefix, NULL);
Radek Krejci88f29302015-10-30 15:42:33 +0100594 if (!module) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100595 LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name);
Radek Krejci88f29302015-10-30 15:42:33 +0100596 goto error;
597 }
598 } else {
599 name = str;
600 }
601
602 /* prepare data for parsing node content */
603 len += r + 1;
604 len += skip_ws(&data[len]);
605 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100606 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing name-separator)");
Radek Krejci88f29302015-10-30 15:42:33 +0100607 goto error;
608 }
609 len++;
610 len += skip_ws(&data[len]);
611
612 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100613 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 +0100614 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100615 }
616 len++;
617 value = lyjson_parse_text(&data[len], &r);
618 if (!r) {
619 goto error;
620 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100621 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 +0100622 free(value);
Radek Krejci88f29302015-10-30 15:42:33 +0100623 goto error;
624 }
625 len += r + 1;
626 len += skip_ws(&data[len]);
627
628 attr_new = malloc(sizeof **attr);
Michal Vasko253035f2015-12-17 16:58:13 +0100629 if (!attr_new) {
630 LOGMEM;
631 goto error;
632 }
Radek Krejci88f29302015-10-30 15:42:33 +0100633 attr_new->module = module;
634 attr_new->next = NULL;
635 attr_new->name = lydict_insert(module->ctx, name, 0);
636 attr_new->value = lydict_insert_zc(module->ctx, value);
637 if (!attr_last) {
638 *attr = attr_last = attr_new;
639 } else {
640 attr_last->next = attr_new;
641 attr_last = attr_new;
642 }
643
644 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100645 str = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100646
647 if (data[len] == ',') {
648 goto repeat;
649 } else if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100650 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing end-object)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100651 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100652 }
653 len++;
654 len += skip_ws(&data[len]);
655
656 return len;
657
658error:
659 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100660 if (*attr) {
661 lyd_free_attr((*attr)->module->ctx, NULL, *attr, 1);
662 *attr = NULL;
663 }
Radek Krejci88f29302015-10-30 15:42:33 +0100664 return 0;
665}
666
667struct attr_cont {
668 struct attr_cont *next;
669 struct lyd_attr *attr;
670 struct lys_node *schema;
671 unsigned int index; /** non-zero only in case of leaf-list */
672};
673
Radek Krejcic4831272015-11-01 19:26:34 +0100674static int
675store_attrs(struct ly_ctx *ctx, struct attr_cont *attrs, struct lyd_node *first)
676{
677 struct lyd_node *diter;
678 struct attr_cont *iter;
679 unsigned int flag_leaflist = 0;
680
681 while (attrs) {
682 iter = attrs;
683 attrs = attrs->next;
684
685 if (iter->index) {
686 flag_leaflist = 1;
687 }
688
689 LY_TREE_FOR(first, diter) {
690 if (iter->schema != diter->schema) {
691 continue;
692 }
693
694 if (flag_leaflist && flag_leaflist != iter->index) {
695 flag_leaflist++;
696 continue;
697 }
698
699 /* we have match */
700 if (diter->attr) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100701 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, diter,
Radek Krejciadb57612016-02-16 13:34:34 +0100702 "attribute (multiple attribute definitions belong to a single element)");
Radek Krejcic4831272015-11-01 19:26:34 +0100703 free(iter);
704 goto error;
705 }
706
707 diter->attr = iter->attr;
708 break;
709 }
710
711 if (!diter) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100712 LOGVAL(LYE_XML_MISS, LY_VLOG_NONE, NULL, "element for the specified attribute", iter->attr->name);
Radek Krejcic4831272015-11-01 19:26:34 +0100713 lyd_free_attr(iter->schema->module->ctx, NULL, iter->attr, 1);
714 free(iter);
715 goto error;
716 }
717 free(iter);
718 }
719
720 return 0;
721
722error:
723
724 while (attrs) {
725 iter = attrs;
726 attrs = attrs->next;
727
728 lyd_free_attr(ctx, NULL, iter->attr, 1);
729 free(iter);
730 }
731
732 return -1;
733}
734
Radek Krejci88f29302015-10-30 15:42:33 +0100735static unsigned int
Michal Vasko36ef6932015-12-01 14:30:17 +0100736json_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 +0200737 struct lyd_node *first_sibling, struct lyd_node *prev, struct attr_cont **attrs, int options,
738 struct unres_data *unres)
Radek Krejci5449d472015-10-26 14:35:56 +0100739{
740 unsigned int len = 0;
741 unsigned int r;
Radek Krejcic4831272015-11-01 19:26:34 +0100742 unsigned int flag_leaflist = 0;
Radek Krejci61767ca2016-09-19 14:21:55 +0200743 int i, pos;
Radek Krejci88f29302015-10-30 15:42:33 +0100744 char *name, *prefix = NULL, *str = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100745 const struct lys_module *module = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +0100746 struct lys_node *schema = NULL;
Radek Krejcibd930122016-08-10 13:28:26 +0200747 struct lyd_node *result = NULL, *new, *list, *diter = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100748 struct lyd_attr *attr;
Radek Krejcic4831272015-11-01 19:26:34 +0100749 struct attr_cont *attrs_aux;
Radek Krejci5449d472015-10-26 14:35:56 +0100750
751 /* each YANG data node representation starts with string (node identifier) */
752 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100753 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100754 "JSON data (missing quotation-mark at the beginning of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100755 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100756 }
757 len++;
758
Radek Krejci23238922015-10-27 17:13:34 +0100759 str = lyjson_parse_text(&data[len], &r);
Radek Krejci5449d472015-10-26 14:35:56 +0100760 if (!r) {
761 goto error;
762 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100763 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100764 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100765 goto error;
766 }
Radek Krejci23238922015-10-27 17:13:34 +0100767 if ((name = strchr(str, ':'))) {
768 *name = '\0';
769 name++;
770 prefix = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100771 if (prefix[0] == '@') {
772 prefix++;
773 }
Radek Krejci23238922015-10-27 17:13:34 +0100774 } else {
775 name = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100776 if (name[0] == '@') {
777 name++;
778 }
Radek Krejci5449d472015-10-26 14:35:56 +0100779 }
780
Radek Krejci5449d472015-10-26 14:35:56 +0100781 /* prepare data for parsing node content */
782 len += r + 1;
783 len += skip_ws(&data[len]);
784 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100785 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing name-separator)");
Radek Krejci5449d472015-10-26 14:35:56 +0100786 goto error;
787 }
788 len++;
789 len += skip_ws(&data[len]);
790
Radek Krejci88f29302015-10-30 15:42:33 +0100791 if (str[0] == '@' && !str[1]) {
792 /* process attribute of the parent object (container or list) */
Radek Krejci2a0efef2016-03-24 15:10:40 +0100793 if (!(*parent)) {
794 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute with no corresponding element to belongs to");
Radek Krejci88f29302015-10-30 15:42:33 +0100795 goto error;
796 }
797
798 r = json_parse_attr((*parent)->schema->module, &attr, &data[len]);
799 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100800 LOGPATH(LY_VLOG_LYD, (*parent));
Radek Krejci88f29302015-10-30 15:42:33 +0100801 goto error;
802 }
803 len += r;
804
805 if ((*parent)->attr) {
806 lyd_free_attr(ctx, NULL, attr, 1);
807 } else {
808 (*parent)->attr = attr;
809 }
Radek Krejcic4831272015-11-01 19:26:34 +0100810 free(str);
811 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100812 }
813
Radek Krejci5449d472015-10-26 14:35:56 +0100814 /* find schema node */
815 if (!(*parent)) {
816 /* starting in root */
817 /* get the proper schema */
818 module = ly_ctx_get_module(ctx, prefix, NULL);
819 if (module) {
820 /* get the proper schema node */
Radek Krejci919a9242016-07-27 08:17:13 +0200821 while ((schema = (struct lys_node *)lys_getnext(schema, NULL, module, 0))) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100822 /* skip nodes in module's data which are not expected here according to options' data type */
823 if (options & LYD_OPT_RPC) {
824 if (schema->nodetype != LYS_RPC) {
825 continue;
826 }
827 } else if (options & LYD_OPT_NOTIF) {
828 if (schema->nodetype != LYS_NOTIF) {
829 continue;
830 }
831 } else if (!(options & LYD_OPT_RPCREPLY)) {
832 /* rest of the data types except RPCREPLY which cannot be here */
Radek Krejci919a9242016-07-27 08:17:13 +0200833 if (schema->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100834 continue;
835 }
836 }
Radek Krejci919a9242016-07-27 08:17:13 +0200837
Radek Krejci5449d472015-10-26 14:35:56 +0100838 if (!strcmp(schema->name, name)) {
839 break;
840 }
841 }
Radek Krejci88f29302015-10-30 15:42:33 +0100842 } else {
Radek Krejci2a0efef2016-03-24 15:10:40 +0100843 LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name);
Radek Krejci88f29302015-10-30 15:42:33 +0100844 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100845 }
846 } else {
847 /* parsing some internal node, we start with parent's schema pointer */
848 if (prefix) {
849 /* get the proper schema */
850 module = ly_ctx_get_module(ctx, prefix, NULL);
851 if (!module) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100852 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci88f29302015-10-30 15:42:33 +0100853 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100854 }
855 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100856
857 /* go through RPC's input/output following the options' data type */
858 if ((*parent)->schema->nodetype == LYS_RPC) {
859 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, module, LYS_GETNEXT_WITHINOUT))) {
860 if ((options & LYD_OPT_RPC) && schema->nodetype == LYS_INPUT) {
861 break;
862 } else if ((options & LYD_OPT_RPCREPLY) && schema->nodetype == LYS_OUTPUT) {
863 break;
864 }
865 }
866 if (!schema) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100867 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100868 goto error;
869 }
870 schema_parent = schema;
871 schema = NULL;
872 }
873
Michal Vasko36ef6932015-12-01 14:30:17 +0100874 if (schema_parent) {
875 while ((schema = (struct lys_node *)lys_getnext(schema, schema_parent, module, 0))) {
876 if (!strcmp(schema->name, name)) {
877 break;
878 }
879 }
880 } else {
881 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, module, 0))) {
882 if (!strcmp(schema->name, name)) {
883 break;
884 }
Radek Krejci5449d472015-10-26 14:35:56 +0100885 }
886 }
887 }
Radek Krejci27fe55e2016-09-13 17:13:35 +0200888 if (!schema || !lys_node_module(schema)->implemented) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100889 LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name);
Radek Krejci88f29302015-10-30 15:42:33 +0100890 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100891 }
Radek Krejci88f29302015-10-30 15:42:33 +0100892
893 if (str[0] == '@') {
Radek Krejci88f29302015-10-30 15:42:33 +0100894 /* attribute for some sibling node */
895 if (data[len] == '[') {
896 flag_leaflist = 1;
897 len++;
898 len += skip_ws(&data[len]);
899 }
900
901attr_repeat:
902 r = json_parse_attr(schema->module, &attr, &data[len]);
903 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100904 LOGPATH(LY_VLOG_LYD, (*parent));
Radek Krejci88f29302015-10-30 15:42:33 +0100905 goto error;
906 }
907 len += r;
908
909 if (attr) {
Radek Krejcic4831272015-11-01 19:26:34 +0100910 attrs_aux = malloc(sizeof *attrs_aux);
Michal Vasko253035f2015-12-17 16:58:13 +0100911 if (!attrs_aux) {
912 LOGMEM;
913 goto error;
914 }
Radek Krejcic4831272015-11-01 19:26:34 +0100915 attrs_aux->attr = attr;
916 attrs_aux->index = flag_leaflist;
917 attrs_aux->schema = schema;
918 attrs_aux->next = *attrs;
919 *attrs = attrs_aux;
Radek Krejci88f29302015-10-30 15:42:33 +0100920 } else if (!flag_leaflist) {
921 /* error */
Radek Krejci48464ed2016-03-17 15:44:09 +0100922 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "attribute data");
Radek Krejci88f29302015-10-30 15:42:33 +0100923 goto error;
924 }
925
926 if (flag_leaflist) {
927 if (data[len] == ',') {
928 len++;
929 len += skip_ws(&data[len]);
930 flag_leaflist++;
931 goto attr_repeat;
932 } else if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100933 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing end-array)");
Radek Krejci88f29302015-10-30 15:42:33 +0100934 goto error;
935 }
936 len++;
937 len += skip_ws(&data[len]);
938 }
939
Radek Krejcic4831272015-11-01 19:26:34 +0100940 free(str);
941 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100942 }
943
Radek Krejci5449d472015-10-26 14:35:56 +0100944 switch (schema->nodetype) {
945 case LYS_CONTAINER:
946 case LYS_LIST:
947 case LYS_NOTIF:
948 case LYS_RPC:
949 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 }
Radek Krejcica7efb72016-01-18 13:06:01 +01001010 result->validity = LYD_VAL_NOT;
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 */
Radek Krejcibd930122016-08-10 13:28:26 +02001018 r = json_get_value((struct lyd_node_leaf_list *)result, first_sibling, &data[len], options);
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 Vasko0d634d02016-05-18 13:26:11 +02001035 } else if (schema->nodetype & (LYS_CONTAINER | LYS_RPC | LYS_NOTIF)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001036 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001037 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001038 goto error;
1039 }
1040 len++;
1041 len += skip_ws(&data[len]);
1042
1043 if (data[len] != '}') {
1044 /* non-empty container */
Radek Krejcic4831272015-11-01 19:26:34 +01001045 len--;
1046 diter = NULL;
1047 attrs_aux = NULL;
1048 do {
1049 len++;
1050 len += skip_ws(&data[len]);
1051
Radek Krejcibd930122016-08-10 13:28:26 +02001052 r = json_parse_data(ctx, &data[len], NULL, &result, result->child, diter, &attrs_aux, options, unres);
Radek Krejcic4831272015-11-01 19:26:34 +01001053 if (!r) {
1054 goto error;
1055 }
1056 len += r;
1057
1058 if (result->child) {
1059 diter = result->child->prev;
1060 }
1061 } while(data[len] == ',');
1062
1063 /* store attributes */
1064 if (store_attrs(ctx, attrs_aux, result->child)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001065 goto error;
1066 }
Radek Krejci5449d472015-10-26 14:35:56 +01001067 }
1068
1069 if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001070 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001071 goto error;
1072 }
1073 len++;
1074 len += skip_ws(&data[len]);
1075
Michal Vaskoc4280842016-04-19 16:10:42 +02001076 /* if we have empty non-presence container, we could remove it immediately if there were no attributes of it, who knows */
Radek Krejci2537fd32016-09-07 16:22:41 +02001077 if (schema->nodetype == LYS_CONTAINER && !result->child &&
Radek Krejcid3e73722016-05-23 12:24:55 +02001078 !result->attr && !((struct lys_node_container *)schema)->presence) {
Michal Vaskoc4280842016-04-19 16:10:42 +02001079 if (unres_data_add(unres, result, UNRES_EMPTYCONT)) {
1080 goto error;
1081 }
Radek Krejci0c0086a2016-03-24 15:20:28 +01001082 }
1083
Radek Krejci5449d472015-10-26 14:35:56 +01001084 } else if (schema->nodetype == LYS_LIST) {
1085 if (data[len] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001086 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001087 goto error;
1088 }
1089
1090 list = result;
1091 do {
1092 len++;
1093 len += skip_ws(&data[len]);
Radek Krejci23238922015-10-27 17:13:34 +01001094
Radek Krejcic4831272015-11-01 19:26:34 +01001095 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001096 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001097 "JSON data (missing list instance's begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001098 goto error;
1099 }
Radek Krejcic4831272015-11-01 19:26:34 +01001100 diter = NULL;
1101 attrs_aux = NULL;
1102 do {
1103 len++;
1104 len += skip_ws(&data[len]);
1105
Radek Krejcibd930122016-08-10 13:28:26 +02001106 r = json_parse_data(ctx, &data[len], NULL, &list, list->child, diter, &attrs_aux, options, unres);
Radek Krejcic4831272015-11-01 19:26:34 +01001107 if (!r) {
1108 goto error;
1109 }
1110 len += r;
1111
Radek Krejci52934692015-11-01 20:08:15 +01001112 if (list->child) {
Radek Krejcic4831272015-11-01 19:26:34 +01001113 diter = list->child->prev;
1114 }
1115 } while(data[len] == ',');
1116
1117 /* store attributes */
Radek Krejci52934692015-11-01 20:08:15 +01001118 if (store_attrs(ctx, attrs_aux, list->child)) {
Radek Krejcic4831272015-11-01 19:26:34 +01001119 goto error;
1120 }
1121
1122 if (data[len] != '}') {
1123 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001124 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001125 "JSON data (missing list instance's end-object)");
Radek Krejcic4831272015-11-01 19:26:34 +01001126 goto error;
1127 }
1128 len++;
Radek Krejci5449d472015-10-26 14:35:56 +01001129 len += skip_ws(&data[len]);
1130
1131 if (data[len] == ',') {
Radek Krejci93fab982016-02-03 15:58:19 +01001132 /* various validation checks */
1133 ly_errno = 0;
Radek Krejci2d5525d2016-04-04 15:43:30 +02001134 if (!(options & LYD_OPT_TRUSTED) &&
1135 (lyv_data_content(list, options, unres) ||
Radek Krejci61767ca2016-09-19 14:21:55 +02001136 lyv_multicases(list, NULL, prev ? &first_sibling : NULL, 0, NULL))) {
Radek Krejci93fab982016-02-03 15:58:19 +01001137 if (ly_errno) {
1138 goto error;
1139 }
1140 }
1141 /* validation successful */
1142 list->validity = LYD_VAL_OK;
1143
Radek Krejci5449d472015-10-26 14:35:56 +01001144 /* another instance of the list */
1145 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001146 if (!new) {
1147 goto error;
1148 }
Radek Krejci5449d472015-10-26 14:35:56 +01001149 new->parent = list->parent;
1150 new->prev = list;
1151 list->next = new;
1152
1153 /* fix the "last" pointer */
Radek Krejci2d5525d2016-04-04 15:43:30 +02001154 first_sibling->prev = new;
Radek Krejci5449d472015-10-26 14:35:56 +01001155
1156 new->schema = list->schema;
Radek Krejci5449d472015-10-26 14:35:56 +01001157 list = new;
1158 }
1159 } while (data[len] == ',');
Michal Vaskod171d4e2016-06-06 13:33:19 +02001160 result = first_sibling;
Radek Krejci5449d472015-10-26 14:35:56 +01001161
1162 if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001163 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001164 goto error;
1165 }
1166 len++;
1167 len += skip_ws(&data[len]);
1168 }
1169
1170 /* various validation checks */
Michal Vasko10e586f2016-05-18 13:25:30 +02001171 if (!(options & LYD_OPT_TRUSTED) && lyv_data_context(result, options, unres)) {
1172 goto error;
1173 }
1174
Radek Krejci5449d472015-10-26 14:35:56 +01001175 ly_errno = 0;
Radek Krejci2d5525d2016-04-04 15:43:30 +02001176 if (!(options & LYD_OPT_TRUSTED) &&
1177 (lyv_data_content(result, options, unres) ||
Radek Krejci61767ca2016-09-19 14:21:55 +02001178 lyv_multicases(result, NULL, prev ? &first_sibling : NULL, 0, NULL))) {
Radek Krejci5449d472015-10-26 14:35:56 +01001179 if (ly_errno) {
1180 goto error;
1181 }
1182 }
1183
Radek Krejcica7efb72016-01-18 13:06:01 +01001184 /* validation successful */
Radek Krejci63b79c82016-08-10 10:09:33 +02001185 if (result->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
1186 /* postpone checking when there will be all list/leaflist instances */
1187 result->validity = LYD_VAL_UNIQUE;
1188 } else {
1189 result->validity = LYD_VAL_OK;
1190 }
Radek Krejcica7efb72016-01-18 13:06:01 +01001191
Radek Krejci88f29302015-10-30 15:42:33 +01001192 if (!(*parent)) {
1193 *parent = result;
1194 }
1195
Radek Krejcide9d92c2015-10-30 15:59:59 +01001196 free(str);
Radek Krejci5449d472015-10-26 14:35:56 +01001197 return len;
1198
1199error:
Radek Krejci0c0086a2016-03-24 15:20:28 +01001200 len = 0;
Radek Krejci0c0086a2016-03-24 15:20:28 +01001201 /* cleanup */
1202 for (i = unres->count - 1; i >= 0; i--) {
1203 /* remove unres items connected with the node being removed */
1204 if (unres->node[i] == result) {
1205 unres_data_del(unres, i);
1206 }
Radek Krejci88f29302015-10-30 15:42:33 +01001207 }
1208 while (*attrs) {
Radek Krejcic4831272015-11-01 19:26:34 +01001209 attrs_aux = *attrs;
Radek Krejci88f29302015-10-30 15:42:33 +01001210 *attrs = (*attrs)->next;
1211
Radek Krejcic4831272015-11-01 19:26:34 +01001212 lyd_free_attr(ctx, NULL, attrs_aux->attr, 1);
1213 free(attrs_aux);
Radek Krejci88f29302015-10-30 15:42:33 +01001214 }
1215
Radek Krejci5449d472015-10-26 14:35:56 +01001216 lyd_free(result);
Radek Krejci88f29302015-10-30 15:42:33 +01001217 free(str);
1218
Radek Krejci0c0086a2016-03-24 15:20:28 +01001219 return len;
Radek Krejci5449d472015-10-26 14:35:56 +01001220}
1221
1222struct lyd_node *
Michal Vasko6b44d712016-09-12 16:25:46 +02001223lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lys_node *parent, struct lyd_node *data_tree)
Radek Krejci5449d472015-10-26 14:35:56 +01001224{
Radek Krejcic4831272015-11-01 19:26:34 +01001225 struct lyd_node *result = NULL, *next = NULL, *iter = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001226 struct unres_data *unres = NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001227 unsigned int len = 0, r;
Radek Krejci63b79c82016-08-10 10:09:33 +02001228 int i;
Radek Krejcic4831272015-11-01 19:26:34 +01001229 struct attr_cont *attrs = NULL;
Radek Krejci63b79c82016-08-10 10:09:33 +02001230 struct ly_set *set;
Radek Krejci5449d472015-10-26 14:35:56 +01001231
Radek Krejci2342cf62016-01-29 16:48:23 +01001232 ly_errno = LY_SUCCESS;
1233
Michal Vasko0a073da2016-05-10 15:48:40 +02001234 if (!ctx || !data || (options & LYD_OPT_RPCREPLY)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001235 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1236 return NULL;
1237 }
1238
Radek Krejcic4831272015-11-01 19:26:34 +01001239 /* skip leading whitespaces */
1240 len += skip_ws(&data[len]);
1241
Michal Vasko24d982f2016-04-18 15:13:58 +02001242 /* no data (or whitespaces only) are fine */
1243 if (!data[len]) {
1244 lyd_validate(&result, options, ctx);
1245 return result;
1246 }
1247
Radek Krejcic4831272015-11-01 19:26:34 +01001248 /* expect top-level { */
1249 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001250 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level begin-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001251 return NULL;
1252 }
1253
1254 unres = calloc(1, sizeof *unres);
1255 if (!unres) {
1256 LOGMEM;
1257 return NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001258 }
1259
1260 do {
1261 len++;
1262 len += skip_ws(&data[len]);
1263
Radek Krejcibd930122016-08-10 13:28:26 +02001264 r = json_parse_data(ctx, &data[len], parent, &next, result, iter, &attrs, options, unres);
Radek Krejcic4831272015-11-01 19:26:34 +01001265 if (!r) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001266 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001267 }
1268 len += r;
1269
1270 if (!result) {
1271 result = next;
1272 }
1273 if (next) {
1274 iter = next;
1275 }
1276 next = NULL;
1277 } while(data[len] == ',');
1278
1279 if (data[len] != '}') {
1280 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001281 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001282 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001283 }
1284 len++;
1285 len += skip_ws(&data[len]);
1286
1287 /* store attributes */
1288 if (store_attrs(ctx, attrs, result)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001289 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001290 }
Radek Krejci5449d472015-10-26 14:35:56 +01001291
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001292 if (!result) {
1293 LOGERR(LY_EVALID, "Model for the data to be linked with not found.");
Michal Vasko24d982f2016-04-18 15:13:58 +02001294 goto error;
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001295 }
1296
Radek Krejci63b79c82016-08-10 10:09:33 +02001297 /* check for uniquness of top-level lists/leaflists because
1298 * only the inner instances were tested in lyv_data_content() */
1299 set = ly_set_new();
1300 LY_TREE_FOR(result, iter) {
1301 if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) {
1302 continue;
1303 }
1304
1305 /* check each list/leaflist only once */
1306 i = set->number;
1307 if (ly_set_add(set, iter->schema, 0) != i) {
1308 /* already checked */
1309 continue;
1310 }
1311
1312 if (lyv_data_unique(iter, result)) {
1313 ly_set_free(set);
1314 goto error;
1315 }
1316 }
1317 ly_set_free(set);
1318
Radek Krejci46165822016-08-26 14:06:27 +02001319 /* add/validate default values, unres */
Michal Vasko6b44d712016-09-12 16:25:46 +02001320 if (lyd_defaults_add_unres(&result, options, ctx, data_tree, NULL, unres)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001321 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001322 }
1323
Radek Krejci46165822016-08-26 14:06:27 +02001324 /* check for missing top level mandatory nodes */
1325 if (!(options & LYD_OPT_TRUSTED) && lyd_check_mandatory_tree(result, ctx, options)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001326 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001327 }
1328
Radek Krejci5449d472015-10-26 14:35:56 +01001329 free(unres->node);
1330 free(unres->type);
Radek Krejci5449d472015-10-26 14:35:56 +01001331 free(unres);
1332
1333 return result;
Michal Vasko24d982f2016-04-18 15:13:58 +02001334
1335error:
1336 lyd_free_withsiblings(result);
1337 free(unres->node);
1338 free(unres->type);
1339 free(unres);
1340
1341 return NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001342}