Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1 | /** |
| 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 Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * 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 Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | #include <ctype.h> |
| 18 | #include <limits.h> |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 20 | #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 Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 30 | #include "xml_internal.h" |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 31 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 32 | static int |
| 33 | lyjson_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 | |
| 46 | static unsigned int |
| 47 | skip_ws(const char *data) |
| 48 | { |
| 49 | unsigned int len = 0; |
| 50 | |
| 51 | /* skip leading whitespaces */ |
| 52 | while (data[len] && lyjson_isspace(data[len])) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 53 | len++; |
| 54 | } |
| 55 | |
| 56 | return len; |
| 57 | } |
| 58 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 59 | static char * |
| 60 | lyjson_parse_text(const char *data, unsigned int *len) |
| 61 | { |
| 62 | #define BUFSIZE 1024 |
| 63 | |
| 64 | char buf[BUFSIZE]; |
| 65 | char *result = NULL, *aux; |
Radek Krejci | 967e4bf | 2015-11-28 10:06:40 +0100 | [diff] [blame] | 66 | int o, size = 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 67 | 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 Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 75 | aux = ly_realloc(result, size + 1); |
| 76 | if (!aux) { |
| 77 | LOGMEM; |
| 78 | return NULL; |
| 79 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 80 | result = aux; |
| 81 | } else { |
| 82 | size = o; |
| 83 | result = malloc((size + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 84 | if (!result) { |
| 85 | LOGMEM; |
| 86 | return NULL; |
| 87 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 88 | } |
| 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 148 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character escape sequence"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 149 | goto error; |
| 150 | |
| 151 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 152 | r = pututf8(&buf[o], value); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 153 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 154 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character UTF8 character"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 155 | 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 161 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "control character (unescaped)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 162 | goto error; |
| 163 | } else { |
| 164 | /* unescaped character */ |
Radek Krejci | deee60e | 2016-09-23 15:21:14 +0200 | [diff] [blame] | 165 | 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | #undef BUFSIZE |
| 176 | |
| 177 | if (o) { |
| 178 | if (result) { |
| 179 | size = size + o; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 180 | aux = ly_realloc(result, size + 1); |
| 181 | if (!aux) { |
| 182 | LOGMEM; |
| 183 | return NULL; |
| 184 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 185 | result = aux; |
| 186 | } else { |
| 187 | size = o; |
| 188 | result = malloc((size + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 189 | if (!result) { |
| 190 | LOGMEM; |
| 191 | return NULL; |
| 192 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 193 | } |
| 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 | |
| 205 | error: |
| 206 | free(result); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | static unsigned int |
| 211 | lyjson_parse_number(const char *data) |
| 212 | { |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 213 | unsigned int len = 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 214 | |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 215 | if (data[len] == '-') { |
| 216 | ++len; |
| 217 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 218 | |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 219 | 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 238 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 239 | return 0; |
| 240 | } |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 241 | while (isdigit(data[len])) { |
| 242 | ++len; |
| 243 | } |
| 244 | } |
| 245 | |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 246 | 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 Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 256 | 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return len; |
| 262 | } |
| 263 | |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 264 | static char * |
| 265 | lyjson_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 Vasko | 9d1593d | 2016-09-30 12:33:34 +0200 | [diff] [blame] | 334 | for (ptr = num + strlen(num), number_ptr = number; (unsigned)(number_ptr - number) < num_len_no_e; ) { |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 335 | 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 363 | static unsigned int |
| 364 | lyjson_parse_boolean(const char *data) |
| 365 | { |
Radek Krejci | 6b47b50 | 2015-10-30 15:52:41 +0100 | [diff] [blame] | 366 | unsigned int len = 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 367 | |
| 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 375 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON literal value (expected true or false)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | return len; |
| 380 | } |
| 381 | |
| 382 | static unsigned int |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 383 | json_get_anydata(struct lyd_node_anydata *any, const char *data) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 384 | { |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame^] | 385 | unsigned int len = 0, start, stop, c = 0; |
| 386 | char *str; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 387 | |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame^] | 388 | /* 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 Vasko | a19f7d7 | 2016-05-18 13:24:08 +0200 | [diff] [blame] | 408 | return 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame^] | 411 | /* count opening '{' and closing '}' brackets to get the end of the object without its parsing */ |
| 412 | c = len = 1; |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 413 | len += skip_ws(&data[len]); |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame^] | 414 | start = len; |
| 415 | stop = start - 1; |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 416 | 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 Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame^] | 436 | if (stop >= start) { |
| 437 | any->value.str = lydict_insert(any->schema->module->ctx, &data[start], stop - start + 1); |
| 438 | } /* else no data */ |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 439 | |
Michal Vasko | a19f7d7 | 2016-05-18 13:24:08 +0200 | [diff] [blame] | 440 | return len; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static unsigned int |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 444 | json_get_value(struct lyd_node_leaf_list *leaf, struct lyd_node *first_sibling, const char *data, int options) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 445 | { |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 446 | struct lyd_node_leaf_list *new; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 447 | struct lys_type *stype; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 448 | struct ly_ctx *ctx; |
| 449 | unsigned int len = 0, r; |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 450 | int resolvable; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 451 | char *str; |
| 452 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 453 | assert(leaf && data); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 454 | ctx = leaf->schema->module->ctx; |
| 455 | |
Radek Krejci | 92ece00 | 2016-04-04 15:45:05 +0200 | [diff] [blame] | 456 | if (options & (LYD_OPT_EDIT | LYD_OPT_GET | LYD_OPT_GETCONFIG)) { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 457 | resolvable = 0; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 458 | } else { |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 459 | resolvable = 1; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 462 | stype = &((struct lys_node_leaf *)leaf->schema)->type; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 463 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 464 | if (leaf->schema->nodetype == LYS_LEAFLIST) { |
| 465 | /* expecting begin-array */ |
| 466 | if (data[len++] != '[') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 467 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expected begin-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | repeat: |
| 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 Vasko | 6baed1c | 2016-05-18 13:24:44 +0200 | [diff] [blame] | 480 | ++len; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 481 | str = lyjson_parse_text(&data[len], &r); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 482 | if (!str) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 483 | LOGPATH(LY_VLOG_LYD, leaf); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | leaf->value_str = lydict_insert_zc(ctx, str); |
| 487 | if (data[len + r] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 488 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 489 | "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 490 | 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 497 | LOGPATH(LY_VLOG_LYD, leaf); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 498 | return 0; |
| 499 | } |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 500 | /* 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 510 | len += r; |
| 511 | } else if (data[len] == 'f' || data[len] == 't') { |
| 512 | /* boolean */ |
| 513 | r = lyjson_parse_boolean(&data[len]); |
| 514 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 515 | LOGPATH(LY_VLOG_LYD, leaf); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 516 | 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 Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 522 | leaf->value_str = lydict_insert(ctx, "", 0); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 523 | len += 6; |
| 524 | } else { |
| 525 | /* error */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 526 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (unexpected value)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 530 | /* 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 Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 534 | ly_errno = LY_EVALID; |
| 535 | return 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 536 | } |
| 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 Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 544 | if (!new) { |
| 545 | LOGMEM; |
| 546 | return 0; |
| 547 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 548 | 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 Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 553 | first_sibling->prev = (struct lyd_node *)new; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 554 | |
| 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 566 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expecting value-separator or end-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | len += skip_ws(&data[len]); |
| 572 | return len; |
| 573 | } |
| 574 | |
| 575 | static unsigned int |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 576 | json_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 Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 583 | *attr = NULL; |
| 584 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 585 | if (data[len] != '{') { |
| 586 | if (!strncmp(&data[len], "null", 4)) { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 587 | len += 4; |
| 588 | len += skip_ws(&data[len]); |
| 589 | return len; |
| 590 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 591 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing begin-object)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 592 | goto error; |
| 593 | } |
| 594 | |
| 595 | repeat: |
| 596 | len++; |
| 597 | len += skip_ws(&data[len]); |
| 598 | |
| 599 | if (data[len] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 600 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the begining of string)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 601 | 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 608 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 609 | goto error; |
| 610 | } |
| 611 | if ((name = strchr(str, ':'))) { |
| 612 | *name = '\0'; |
| 613 | name++; |
| 614 | prefix = str; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 615 | module = (struct lys_module *)ly_ctx_get_module(parent_module->ctx, prefix, NULL); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 616 | if (!module) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 617 | LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 618 | 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 628 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing name-separator)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 629 | goto error; |
| 630 | } |
| 631 | len++; |
| 632 | len += skip_ws(&data[len]); |
| 633 | |
| 634 | if (data[len] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 635 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the beginning of string)"); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 636 | goto error; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 637 | } |
| 638 | len++; |
| 639 | value = lyjson_parse_text(&data[len], &r); |
| 640 | if (!r) { |
| 641 | goto error; |
| 642 | } else if (data[len + r] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 643 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 644 | free(value); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 645 | goto error; |
| 646 | } |
| 647 | len += r + 1; |
| 648 | len += skip_ws(&data[len]); |
| 649 | |
| 650 | attr_new = malloc(sizeof **attr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 651 | if (!attr_new) { |
| 652 | LOGMEM; |
| 653 | goto error; |
| 654 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 655 | 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 Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 667 | str = NULL; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 668 | |
| 669 | if (data[len] == ',') { |
| 670 | goto repeat; |
| 671 | } else if (data[len] != '}') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 672 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing end-object)"); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 673 | goto error; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 674 | } |
| 675 | len++; |
| 676 | len += skip_ws(&data[len]); |
| 677 | |
| 678 | return len; |
| 679 | |
| 680 | error: |
| 681 | free(str); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 682 | if (*attr) { |
| 683 | lyd_free_attr((*attr)->module->ctx, NULL, *attr, 1); |
| 684 | *attr = NULL; |
| 685 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | struct 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 Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 696 | static int |
| 697 | store_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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 723 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, diter, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 724 | "attribute (multiple attribute definitions belong to a single element)"); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 725 | free(iter); |
| 726 | goto error; |
| 727 | } |
| 728 | |
| 729 | diter->attr = iter->attr; |
| 730 | break; |
| 731 | } |
| 732 | |
| 733 | if (!diter) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 734 | LOGVAL(LYE_XML_MISS, LY_VLOG_NONE, NULL, "element for the specified attribute", iter->attr->name); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 735 | 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 | |
| 744 | error: |
| 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 Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 757 | static unsigned int |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 758 | json_parse_data(struct ly_ctx *ctx, const char *data, const struct lys_node *schema_parent, struct lyd_node **parent, |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 759 | struct lyd_node *first_sibling, struct lyd_node *prev, struct attr_cont **attrs, int options, |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 760 | struct unres_data *unres, struct lyd_node **act_notif) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 761 | { |
| 762 | unsigned int len = 0; |
| 763 | unsigned int r; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 764 | unsigned int flag_leaflist = 0; |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 765 | int i, pos; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 766 | char *name, *prefix = NULL, *str = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 767 | const struct lys_module *module = NULL; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 768 | struct lys_node *schema = NULL; |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 769 | struct lyd_node *result = NULL, *new, *list, *diter = NULL; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 770 | struct lyd_attr *attr; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 771 | struct attr_cont *attrs_aux; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 772 | |
| 773 | /* each YANG data node representation starts with string (node identifier) */ |
| 774 | if (data[len] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 775 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 776 | "JSON data (missing quotation-mark at the beginning of string)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 777 | return 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 778 | } |
| 779 | len++; |
| 780 | |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 781 | str = lyjson_parse_text(&data[len], &r); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 782 | if (!r) { |
| 783 | goto error; |
| 784 | } else if (data[len + r] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 785 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 786 | "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 787 | goto error; |
| 788 | } |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 789 | if ((name = strchr(str, ':'))) { |
| 790 | *name = '\0'; |
| 791 | name++; |
| 792 | prefix = str; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 793 | if (prefix[0] == '@') { |
| 794 | prefix++; |
| 795 | } |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 796 | } else { |
| 797 | name = str; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 798 | if (name[0] == '@') { |
| 799 | name++; |
| 800 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 801 | } |
| 802 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 803 | /* prepare data for parsing node content */ |
| 804 | len += r + 1; |
| 805 | len += skip_ws(&data[len]); |
| 806 | if (data[len] != ':') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 807 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing name-separator)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 808 | goto error; |
| 809 | } |
| 810 | len++; |
| 811 | len += skip_ws(&data[len]); |
| 812 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 813 | if (str[0] == '@' && !str[1]) { |
| 814 | /* process attribute of the parent object (container or list) */ |
Radek Krejci | 2a0efef | 2016-03-24 15:10:40 +0100 | [diff] [blame] | 815 | if (!(*parent)) { |
| 816 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute with no corresponding element to belongs to"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 817 | goto error; |
| 818 | } |
| 819 | |
| 820 | r = json_parse_attr((*parent)->schema->module, &attr, &data[len]); |
| 821 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 822 | LOGPATH(LY_VLOG_LYD, (*parent)); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 823 | 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 Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 832 | free(str); |
| 833 | return len; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 836 | /* 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 Krejci | 919a924 | 2016-07-27 08:17:13 +0200 | [diff] [blame] | 843 | while ((schema = (struct lys_node *)lys_getnext(schema, NULL, module, 0))) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 844 | if (!strcmp(schema->name, name)) { |
| 845 | break; |
| 846 | } |
| 847 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 848 | } else { |
Radek Krejci | 2a0efef | 2016-03-24 15:10:40 +0100 | [diff] [blame] | 849 | LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 850 | goto error; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 851 | } |
| 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 858 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 859 | goto error; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 860 | } |
| 861 | } |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 862 | |
| 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 Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 866 | if ((options & LYD_OPT_RPC) && (schema->nodetype == LYS_INPUT)) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 867 | break; |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 868 | } else if ((options & LYD_OPT_RPCREPLY) && (schema->nodetype == LYS_OUTPUT)) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 869 | break; |
| 870 | } |
| 871 | } |
| 872 | if (!schema) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 873 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name); |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 874 | goto error; |
| 875 | } |
| 876 | schema_parent = schema; |
| 877 | schema = NULL; |
| 878 | } |
| 879 | |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 880 | 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 894 | if (!schema || !lys_node_module(schema)->implemented) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 895 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, (*parent), name); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 896 | goto error; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 897 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 898 | |
| 899 | if (str[0] == '@') { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 900 | /* attribute for some sibling node */ |
| 901 | if (data[len] == '[') { |
| 902 | flag_leaflist = 1; |
| 903 | len++; |
| 904 | len += skip_ws(&data[len]); |
| 905 | } |
| 906 | |
| 907 | attr_repeat: |
| 908 | r = json_parse_attr(schema->module, &attr, &data[len]); |
| 909 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 910 | LOGPATH(LY_VLOG_LYD, (*parent)); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 911 | goto error; |
| 912 | } |
| 913 | len += r; |
| 914 | |
| 915 | if (attr) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 916 | attrs_aux = malloc(sizeof *attrs_aux); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 917 | if (!attrs_aux) { |
| 918 | LOGMEM; |
| 919 | goto error; |
| 920 | } |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 921 | 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 Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 926 | } else if (!flag_leaflist) { |
| 927 | /* error */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 928 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "attribute data"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 929 | 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 Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 939 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing end-array)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 940 | goto error; |
| 941 | } |
| 942 | len++; |
| 943 | len += skip_ws(&data[len]); |
| 944 | } |
| 945 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 946 | free(str); |
| 947 | return len; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 948 | } |
| 949 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 950 | switch (schema->nodetype) { |
| 951 | case LYS_CONTAINER: |
| 952 | case LYS_LIST: |
| 953 | case LYS_NOTIF: |
| 954 | case LYS_RPC: |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 955 | case LYS_ACTION: |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 956 | 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 Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 963 | case LYS_ANYDATA: |
| 964 | result = calloc(1, sizeof(struct lyd_node_anydata)); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 965 | break; |
| 966 | default: |
| 967 | LOGINT; |
Radek Krejci | 595060f | 2015-10-30 16:29:58 +0100 | [diff] [blame] | 968 | goto error; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 969 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 970 | if (!result) { |
| 971 | LOGMEM; |
| 972 | goto error; |
| 973 | } |
| 974 | |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 975 | result->prev = result; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 976 | result->schema = schema; |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 977 | 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 Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1017 | result->validity = LYD_VAL_NOT; |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1018 | if (resolve_applies_when(schema, 0, NULL)) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 1019 | result->when_status = LYD_WHEN; |
| 1020 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1021 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1022 | /* type specific processing */ |
| 1023 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 1024 | /* type detection and assigning the value */ |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 1025 | r = json_get_value((struct lyd_node_leaf_list *)result, first_sibling, &data[len], options); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1026 | if (!r) { |
| 1027 | goto error; |
| 1028 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1029 | while(result->next) { |
| 1030 | result = result->next; |
| 1031 | } |
| 1032 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1033 | len += r; |
| 1034 | len += skip_ws(&data[len]); |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1035 | } else if (schema->nodetype & LYS_ANYDATA) { |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 1036 | r = json_get_anydata((struct lyd_node_anydata *)result, &data[len]); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1037 | if (!r) { |
| 1038 | goto error; |
| 1039 | } |
| 1040 | len += r; |
| 1041 | len += skip_ws(&data[len]); |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1042 | } 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1060 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1061 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-object)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1062 | goto error; |
| 1063 | } |
| 1064 | len++; |
| 1065 | len += skip_ws(&data[len]); |
| 1066 | |
| 1067 | if (data[len] != '}') { |
| 1068 | /* non-empty container */ |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1069 | len--; |
| 1070 | diter = NULL; |
| 1071 | attrs_aux = NULL; |
| 1072 | do { |
| 1073 | len++; |
| 1074 | len += skip_ws(&data[len]); |
| 1075 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1076 | r = json_parse_data(ctx, &data[len], NULL, &result, result->child, diter, &attrs_aux, options, unres, act_notif); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1077 | 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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1089 | goto error; |
| 1090 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | if (data[len] != '}') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1094 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-object)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1095 | goto error; |
| 1096 | } |
| 1097 | len++; |
| 1098 | len += skip_ws(&data[len]); |
| 1099 | |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 1100 | /* if we have empty non-presence container, mark it as default */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 1101 | if (schema->nodetype == LYS_CONTAINER && !result->child && |
Radek Krejci | d3e7372 | 2016-05-23 12:24:55 +0200 | [diff] [blame] | 1102 | !result->attr && !((struct lys_node_container *)schema)->presence) { |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 1103 | result->dflt = 1; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1104 | } |
| 1105 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1106 | } else if (schema->nodetype == LYS_LIST) { |
| 1107 | if (data[len] != '[') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1108 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1109 | goto error; |
| 1110 | } |
| 1111 | |
| 1112 | list = result; |
| 1113 | do { |
| 1114 | len++; |
| 1115 | len += skip_ws(&data[len]); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 1116 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1117 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1118 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 1119 | "JSON data (missing list instance's begin-object)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1120 | goto error; |
| 1121 | } |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1122 | diter = NULL; |
| 1123 | attrs_aux = NULL; |
| 1124 | do { |
| 1125 | len++; |
| 1126 | len += skip_ws(&data[len]); |
| 1127 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1128 | r = json_parse_data(ctx, &data[len], NULL, &list, list->child, diter, &attrs_aux, options, unres, act_notif); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1129 | if (!r) { |
| 1130 | goto error; |
| 1131 | } |
| 1132 | len += r; |
| 1133 | |
Radek Krejci | 5293469 | 2015-11-01 20:08:15 +0100 | [diff] [blame] | 1134 | if (list->child) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1135 | diter = list->child->prev; |
| 1136 | } |
| 1137 | } while(data[len] == ','); |
| 1138 | |
| 1139 | /* store attributes */ |
Radek Krejci | 5293469 | 2015-11-01 20:08:15 +0100 | [diff] [blame] | 1140 | if (store_attrs(ctx, attrs_aux, list->child)) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1141 | goto error; |
| 1142 | } |
| 1143 | |
| 1144 | if (data[len] != '}') { |
| 1145 | /* expecting end-object */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1146 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 1147 | "JSON data (missing list instance's end-object)"); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1148 | goto error; |
| 1149 | } |
| 1150 | len++; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1151 | len += skip_ws(&data[len]); |
| 1152 | |
| 1153 | if (data[len] == ',') { |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1154 | /* various validation checks */ |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 1155 | ly_err_clean(1); |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 1156 | if (!(options & LYD_OPT_TRUSTED) && |
| 1157 | (lyv_data_content(list, options, unres) || |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 1158 | lyv_multicases(list, NULL, prev ? &first_sibling : NULL, 0, NULL))) { |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1159 | if (ly_errno) { |
| 1160 | goto error; |
| 1161 | } |
| 1162 | } |
| 1163 | /* validation successful */ |
| 1164 | list->validity = LYD_VAL_OK; |
| 1165 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1166 | /* another instance of the list */ |
| 1167 | new = calloc(1, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1168 | if (!new) { |
| 1169 | goto error; |
| 1170 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1171 | new->parent = list->parent; |
| 1172 | new->prev = list; |
| 1173 | list->next = new; |
| 1174 | |
| 1175 | /* fix the "last" pointer */ |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 1176 | first_sibling->prev = new; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1177 | |
| 1178 | new->schema = list->schema; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1179 | list = new; |
| 1180 | } |
| 1181 | } while (data[len] == ','); |
Michal Vasko | d171d4e | 2016-06-06 13:33:19 +0200 | [diff] [blame] | 1182 | result = first_sibling; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1183 | |
| 1184 | if (data[len] != ']') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1185 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1186 | goto error; |
| 1187 | } |
| 1188 | len++; |
| 1189 | len += skip_ws(&data[len]); |
| 1190 | } |
| 1191 | |
| 1192 | /* various validation checks */ |
Michal Vasko | 10e586f | 2016-05-18 13:25:30 +0200 | [diff] [blame] | 1193 | if (!(options & LYD_OPT_TRUSTED) && lyv_data_context(result, options, unres)) { |
| 1194 | goto error; |
| 1195 | } |
| 1196 | |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 1197 | ly_err_clean(1); |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 1198 | if (!(options & LYD_OPT_TRUSTED) && |
| 1199 | (lyv_data_content(result, options, unres) || |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 1200 | lyv_multicases(result, NULL, prev ? &first_sibling : NULL, 0, NULL))) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1201 | if (ly_errno) { |
| 1202 | goto error; |
| 1203 | } |
| 1204 | } |
| 1205 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1206 | /* validation successful */ |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1207 | 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 Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1213 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1214 | if (!(*parent)) { |
| 1215 | *parent = result; |
| 1216 | } |
| 1217 | |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 1218 | free(str); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1219 | return len; |
| 1220 | |
| 1221 | error: |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1222 | len = 0; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1223 | /* 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 Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1229 | } |
| 1230 | while (*attrs) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1231 | attrs_aux = *attrs; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1232 | *attrs = (*attrs)->next; |
| 1233 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1234 | lyd_free_attr(ctx, NULL, attrs_aux->attr, 1); |
| 1235 | free(attrs_aux); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1238 | lyd_free(result); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1239 | free(str); |
| 1240 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1241 | return len; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | struct lyd_node * |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 1245 | lyd_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 Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1247 | { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1248 | struct lyd_node *result = NULL, *next, *iter, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1249 | struct unres_data *unres = NULL; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1250 | unsigned int len = 0, r; |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1251 | int i, act_cont = 0; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1252 | struct attr_cont *attrs = NULL; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1253 | struct ly_set *set; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1254 | |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 1255 | ly_err_clean(1); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 1256 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1257 | if (!ctx || !data) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1258 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1259 | return NULL; |
| 1260 | } |
| 1261 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1262 | /* skip leading whitespaces */ |
| 1263 | len += skip_ws(&data[len]); |
| 1264 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1265 | /* no data (or whitespaces only) are fine */ |
| 1266 | if (!data[len]) { |
| 1267 | lyd_validate(&result, options, ctx); |
| 1268 | return result; |
| 1269 | } |
| 1270 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1271 | /* expect top-level { */ |
| 1272 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1273 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level begin-object)"); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1274 | return NULL; |
| 1275 | } |
| 1276 | |
| 1277 | unres = calloc(1, sizeof *unres); |
| 1278 | if (!unres) { |
| 1279 | LOGMEM; |
| 1280 | return NULL; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1283 | /* 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 Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1309 | do { |
| 1310 | len++; |
| 1311 | len += skip_ws(&data[len]); |
| 1312 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1313 | 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 Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1339 | if (!r) { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1340 | lyd_free_withsiblings(reply_top); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1341 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1342 | } |
| 1343 | len += r; |
| 1344 | |
| 1345 | if (!result) { |
| 1346 | result = next; |
| 1347 | } |
| 1348 | if (next) { |
| 1349 | iter = next; |
| 1350 | } |
| 1351 | next = NULL; |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1352 | } while (data[len] == ','); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1353 | |
| 1354 | if (data[len] != '}') { |
| 1355 | /* expecting end-object */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1356 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)"); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1357 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1358 | } |
| 1359 | len++; |
| 1360 | len += skip_ws(&data[len]); |
| 1361 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1362 | 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 Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1371 | /* store attributes */ |
| 1372 | if (store_attrs(ctx, attrs, result)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1373 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1374 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1375 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1376 | if (reply_top) { |
| 1377 | result = reply_top; |
| 1378 | } |
| 1379 | |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 1380 | if (!result) { |
| 1381 | LOGERR(LY_EVALID, "Model for the data to be linked with not found."); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1382 | goto error; |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 1383 | } |
| 1384 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1385 | 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 Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1394 | /* 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 Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1416 | /* add/validate default values, unres */ |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1417 | if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1418 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 1419 | } |
| 1420 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1421 | /* check for missing top level mandatory nodes */ |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1422 | if (!(options & LYD_OPT_TRUSTED) && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1423 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1426 | free(unres->node); |
| 1427 | free(unres->type); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1428 | free(unres); |
| 1429 | |
| 1430 | return result; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1431 | |
| 1432 | error: |
| 1433 | lyd_free_withsiblings(result); |
| 1434 | free(unres->node); |
| 1435 | free(unres->type); |
| 1436 | free(unres); |
| 1437 | |
| 1438 | return NULL; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1439 | } |