diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 04:23:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 04:23:18 +0000 |
commit | b90161ccd3b318f3314a23cb10c387651ad35831 (patch) | |
tree | a47dc087160299ce02d728cbf031d84af6281537 /src/parser_json.c | |
parent | Adding upstream version 2.1.30. (diff) | |
download | libyang2-b90161ccd3b318f3314a23cb10c387651ad35831.tar.xz libyang2-b90161ccd3b318f3314a23cb10c387651ad35831.zip |
Adding upstream version 2.1.148.upstream/2.1.148upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/parser_json.c | 1056 |
1 files changed, 677 insertions, 379 deletions
diff --git a/src/parser_json.c b/src/parser_json.c index 6219c6e..3655d4c 100644 --- a/src/parser_json.c +++ b/src/parser_json.c @@ -4,7 +4,7 @@ * @author Michal Vasko <mvasko@cesnet.cz> * @brief JSON data parser for libyang * - * Copyright (c) 2020 - 2022 CESNET, z.s.p.o. + * Copyright (c) 2020 - 2023 CESNET, z.s.p.o. * * This source code is licensed under BSD 3-Clause License (the "License"). * You may not use this file except in compliance with the License. @@ -161,29 +161,32 @@ lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t return LY_SUCCESS; } - *prefix_p = NULL; while (node) { if (node->schema) { - *prefix_p = node->schema->module->name; + module_name = node->schema->module->name; break; } onode = (struct lyd_node_opaq *)node; if (onode->name.module_name) { - *prefix_p = onode->name.module_name; + module_name = onode->name.module_name; break; } else if (onode->name.prefix) { - *prefix_p = onode->name.prefix; + module_name = onode->name.prefix; break; } node = lyd_parent(node); } - *prefix_len_p = ly_strlen(module_name); + *prefix_p = module_name; + *prefix_len_p = ly_strlen(module_name); return LY_SUCCESS; } /** - * @brief Skip the current JSON object/array. + * @brief Skip the current JSON item (based on status). + * + * The JSON context is moved to the last "status" of the JSON item so to completely + * finish the skip, one more JSON context move is required. * * @param[in] jsonctx JSON context with the input data to skip. * @return LY_ERR value. @@ -192,34 +195,39 @@ static LY_ERR lydjson_data_skip(struct lyjson_ctx *jsonctx) { enum LYJSON_PARSER_STATUS status, current; - uint32_t orig_depth; + uint32_t depth; - status = lyjson_ctx_status(jsonctx, 0); - assert((status == LYJSON_OBJECT) || (status == LYJSON_ARRAY)); - orig_depth = jsonctx->depth; + status = lyjson_ctx_status(jsonctx); + depth = lyjson_ctx_depth(jsonctx); - /* next */ - LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); - - if ((status == LYJSON_OBJECT) && (current != LYJSON_OBJECT) && (current != LYJSON_ARRAY)) { - /* no nested objects */ - LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL)); - return LY_SUCCESS; - } + switch (status) { + case LYJSON_OBJECT: + ++depth; - /* skip after the content */ - while ((jsonctx->depth > orig_depth) || (current != status + 1)) { - if (current == LYJSON_ARRAY) { - /* skip the array separately */ - LY_CHECK_RET(lydjson_data_skip(jsonctx)); - current = lyjson_ctx_status(jsonctx, 0); - } else { + /* skip until object closes */ + do { LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); - } + } while ((current != LYJSON_OBJECT_CLOSED) || (depth != lyjson_ctx_depth(jsonctx))); + break; + case LYJSON_ARRAY: + ++depth; - if (current == LYJSON_END) { - break; + /* skip until array closes */ + do { + LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); + } while ((current != LYJSON_ARRAY_CLOSED) || (depth != lyjson_ctx_depth(jsonctx))); + break; + case LYJSON_OBJECT_NAME: + /* just get to the value */ + LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); + if ((current == LYJSON_OBJECT) || (current == LYJSON_ARRAY)) { + LY_CHECK_RET(lydjson_data_skip(jsonctx)); } + break; + default: + /* no other status really expected, just go to next */ + LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); + break; } return LY_SUCCESS; @@ -280,14 +288,6 @@ lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *pref ret = LY_EVALID; goto cleanup; } - if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { - /* skip element with children */ - ret = lydjson_data_skip(lydctx->jsonctx); - LY_CHECK_GOTO(ret, cleanup); - - ret = LY_ENOT; - goto cleanup; - } } /* get the schema node */ @@ -295,7 +295,7 @@ lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *pref if (!parent && lydctx->ext) { *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts); } else { - *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts); + *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts); } if (!*snode) { /* check for extension data */ @@ -326,13 +326,6 @@ lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *pref } ret = LY_EVALID; goto cleanup; - } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { - /* skip element with children */ - ret = lydjson_data_skip(lydctx->jsonctx); - LY_CHECK_GOTO(ret, cleanup); - - ret = LY_ENOT; - goto cleanup; } } else { /* check that schema node is valid and can be used */ @@ -345,6 +338,55 @@ cleanup: } /** + * @brief Get the hint for the data type parsers according to the current JSON parser context. + * + * @param[in] jsonctx JSON parser context. The context is supposed to be on a value. + * @param[in,out] status Pointer to the current context status, + * in some circumstances the function manipulates with the context so the status is updated. + * @param[out] type_hint_p Pointer to the variable to store the result. + * @return LY_SUCCESS in case of success. + * @return LY_EINVAL in case of invalid context status not referring to a value. + */ +static LY_ERR +lydjson_value_type_hint(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p) +{ + *type_hint_p = 0; + + if (*status_p == LYJSON_ARRAY) { + /* only [null] */ + LY_CHECK_RET(lyjson_ctx_next(jsonctx, status_p)); + if (*status_p != LYJSON_NULL) { + LOGVAL(jsonctx->ctx, LYVE_SYNTAX_JSON, + "Expected JSON name/value or special name/[null], but input data contains name/[%s].", + lyjson_token2str(*status_p)); + return LY_EINVAL; + } + + LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL)); + if (lyjson_ctx_status(jsonctx) != LYJSON_ARRAY_CLOSED) { + LOGVAL(jsonctx->ctx, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.", + lyjson_token2str(*status_p)); + return LY_EINVAL; + } + + *type_hint_p = LYD_VALHINT_EMPTY; + } else if (*status_p == LYJSON_STRING) { + *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64; + } else if (*status_p == LYJSON_NUMBER) { + *type_hint_p = LYD_VALHINT_DECNUM; + } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) { + *type_hint_p = LYD_VALHINT_BOOLEAN; + } else if (*status_p == LYJSON_NULL) { + *type_hint_p = 0; + } else { + LOGVAL(jsonctx->ctx, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p)); + return LY_EINVAL; + } + + return LY_SUCCESS; +} + +/** * @brief Check that the input data are parseable as the @p list. * * Checks for all the list's keys. Function does not revert the context state. @@ -357,128 +399,85 @@ cleanup: static LY_ERR lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list) { - LY_ERR ret = LY_SUCCESS; - enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0); + LY_ERR rc = LY_SUCCESS; + enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx); struct ly_set key_set = {0}; const struct lysc_node *snode; - uint32_t i, status_count; + uint32_t i, hints; assert(list && (list->nodetype == LYS_LIST)); /* get all keys into a set (keys do not have if-features or anything) */ snode = NULL; while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { - ret = ly_set_add(&key_set, (void *)snode, 1, NULL); - LY_CHECK_GOTO(ret, cleanup); + rc = ly_set_add(&key_set, (void *)snode, 1, NULL); + LY_CHECK_GOTO(rc, cleanup); + } + if (!key_set.count) { + /* no keys */ + goto cleanup; } if (status == LYJSON_OBJECT) { - status_count = jsonctx->status.count; - - while (key_set.count && (status != LYJSON_OBJECT_CLOSED)) { + do { const char *name, *prefix; size_t name_len, prefix_len; ly_bool is_attr; /* match the key */ + LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); + if (status != LYJSON_OBJECT_NAME) { + break; + } snode = NULL; lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); if (!is_attr && !prefix) { for (i = 0; i < key_set.count; ++i) { - snode = (const struct lysc_node *)key_set.objs[i]; - if (!ly_strncmp(snode->name, name, name_len)) { + if (!ly_strncmp(key_set.snodes[i]->name, name, name_len)) { + snode = key_set.snodes[i]; break; } } - /* go into the item to a) process it as a key or b) start skipping it as another list child */ - ret = lyjson_ctx_next(jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); + + /* get the value */ + LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); if (snode) { /* we have the key, validate the value */ - if (status < LYJSON_NUMBER) { + if ((status < LYJSON_NUMBER) || (status > LYJSON_NULL)) { /* not a terminal */ - ret = LY_ENOT; + rc = LY_ENOT; goto cleanup; } - ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL); - LY_CHECK_GOTO(ret, cleanup); + rc = lydjson_value_type_hint(jsonctx, &status, &hints); + LY_CHECK_GOTO(rc, cleanup); + rc = ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL, hints); + LY_CHECK_GOTO(rc, cleanup); /* key with a valid value, remove from the set */ ly_set_rm_index(&key_set, i, NULL); } + + /* next object */ + LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); } else { - /* start skipping the member we are not interested in */ - ret = lyjson_ctx_next(jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); - } - /* move to the next child */ - while (status_count < jsonctx->status.count) { - ret = lyjson_ctx_next(jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); + /* skip the uninteresting object */ + LY_CHECK_GOTO(rc = lydjson_data_skip(jsonctx), cleanup); + LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup); } - } + } while (key_set.count && (status == LYJSON_OBJECT_NEXT)); } if (key_set.count) { /* some keys are missing/did not validate */ - ret = LY_ENOT; + rc = LY_ENOT; } cleanup: ly_set_erase(&key_set, NULL); - return ret; -} - -/** - * @brief Get the hint for the data type parsers according to the current JSON parser context. - * - * @param[in] lydctx JSON data parser context. The context is supposed to be on a value. - * @param[in,out] status Pointer to the current context status, - * in some circumstances the function manipulates with the context so the status is updated. - * @param[out] type_hint_p Pointer to the variable to store the result. - * @return LY_SUCCESS in case of success. - * @return LY_EINVAL in case of invalid context status not referring to a value. - */ -static LY_ERR -lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p) -{ - *type_hint_p = 0; - - if (*status_p == LYJSON_ARRAY) { - /* only [null] */ - LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); - if (*status_p != LYJSON_NULL) { - LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, - "Expected JSON name/value or special name/[null], but input data contains name/[%s].", - lyjson_token2str(*status_p)); - return LY_EINVAL; - } - - LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL)); - if (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED) { - LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.", - lyjson_token2str(*status_p)); - return LY_EINVAL; - } - - *type_hint_p = LYD_VALHINT_EMPTY; - } else if (*status_p == LYJSON_STRING) { - *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64; - } else if (*status_p == LYJSON_NUMBER) { - *type_hint_p = LYD_VALHINT_DECNUM; - } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) { - *type_hint_p = LYD_VALHINT_BOOLEAN; - } else if (*status_p == LYJSON_NULL) { - *type_hint_p = 0; - } else { - LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p)); - return LY_EINVAL; - } - - return LY_SUCCESS; + return rc; } /** @@ -512,7 +511,7 @@ lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *sno /* backup parser */ lyjson_ctx_backup(jsonctx); - status = lyjson_ctx_status(jsonctx, 0); + status = lyjson_ctx_status(jsonctx); if (lydctx->parse_opts & LYD_PARSE_OPAQ) { /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed @@ -521,12 +520,10 @@ lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *sno case LYS_LEAFLIST: case LYS_LEAF: /* value may not be valid in which case we parse it as an opaque node */ - ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); - if (ret) { + if ((ret = lydjson_value_type_hint(jsonctx, &status, type_hint_p))) { break; } - - if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) { + if (ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL, *type_hint_p)) { ret = LY_ENOT; } break; @@ -539,8 +536,7 @@ lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *sno break; } } else if (snode->nodetype & LYD_NODE_TERM) { - status = lyjson_ctx_status(jsonctx, 0); - ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); + ret = lydjson_value_type_hint(jsonctx, &status, type_hint_p); } /* restore parser */ @@ -725,7 +721,7 @@ cleanup: static LY_ERR lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node) { - LY_ERR ret = LY_SUCCESS; + LY_ERR rc = LY_SUCCESS, r; enum LYJSON_PARSER_STATUS status; const char *expected; ly_bool in_parent = 0; @@ -745,33 +741,29 @@ lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, str LOG_LOCSET(snode, NULL, NULL, NULL); /* move to the second item in the name/X pair */ - ret = lyjson_ctx_next(lydctx->jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); + LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); /* check attribute encoding */ switch (nodetype) { case LYS_LEAFLIST: expected = "@name/array of objects/nulls"; - LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); next_entry: - instance++; - - /* move into array/next entry */ - ret = lyjson_ctx_next(lydctx->jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); - if (status == LYJSON_ARRAY_CLOSED) { /* no more metadata */ goto cleanup; } + + /* move into the array/next item */ + LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); + instance++; LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_NULL), representation_error); if (!node || (node->schema != prev->schema)) { LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%u of %s:%s to be coupled with metadata.", instance, prev->schema->module->name, prev->schema->name); - ret = LY_EVALID; + rc = LY_EVALID; goto cleanup; } @@ -779,6 +771,8 @@ next_entry: /* continue with the next entry in the leaf-list array */ prev = node; node = node->next; + + LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); goto next_entry; } break; @@ -800,31 +794,32 @@ next_entry: break; default: LOGINT(ctx); - ret = LY_EINT; + rc = LY_EINT; goto cleanup; } /* process all the members inside a single metadata object */ assert(status == LYJSON_OBJECT); - while (status != LYJSON_OBJECT_CLOSED) { - LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); + do { + LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); + LY_CHECK_GOTO(status != LYJSON_OBJECT_NAME, representation_error); lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname); if (!name_len) { LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name); - ret = LY_EVALID; + rc = LY_EVALID; goto cleanup; } else if (!prefix_len) { LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".", (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); - ret = LY_EVALID; + rc = LY_EVALID; goto cleanup; } else if (is_attr) { LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"", (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); - ret = LY_EVALID; + rc = LY_EVALID; goto cleanup; } @@ -834,14 +829,13 @@ next_entry: if (lydctx->parse_opts & LYD_PARSE_STRICT) { LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.", (int)prefix_len, prefix, (int)name_len, name); - ret = LY_EVALID; + rc = LY_EVALID; goto cleanup; } if (node->schema) { /* skip element with children */ - ret = lydjson_data_skip(lydctx->jsonctx); - LY_CHECK_GOTO(ret, cleanup); - status = lyjson_ctx_status(lydctx->jsonctx, 0); + LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup); + status = lyjson_ctx_status(lydctx->jsonctx); /* end of the item */ continue; } @@ -849,22 +843,20 @@ next_entry: } /* get the value */ - ret = lyjson_ctx_next(lydctx->jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); + LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); /* get value hints */ - ret = lydjson_value_type_hint(lydctx, &status, &val_hints); - LY_CHECK_GOTO(ret, cleanup); + LY_CHECK_GOTO(rc = lydjson_value_type_hint(lydctx->jsonctx, &status, &val_hints), cleanup); if (node->schema) { /* create metadata */ - ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, name, name_len, lydctx->jsonctx->value, + rc = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, name, name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, val_hints, node->schema); - LY_CHECK_GOTO(ret, cleanup); + LY_CHECK_GOTO(rc, cleanup); /* add/correct flags */ - ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL); - LY_CHECK_GOTO(ret, cleanup); + rc = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL); + LY_CHECK_GOTO(rc, cleanup); } else { /* create attribute */ const char *module_name; @@ -873,22 +865,24 @@ next_entry: lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len); /* attr2 is always changed to the created attribute */ - ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, + rc = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, val_hints); - LY_CHECK_GOTO(ret, cleanup); + LY_CHECK_GOTO(rc, cleanup); } + /* next member */ - ret = lyjson_ctx_next(lydctx->jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); - LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error); - } + LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); + } while (status == LYJSON_OBJECT_NEXT); + LY_CHECK_GOTO(status != LYJSON_OBJECT_CLOSED, representation_error); if (nodetype == LYS_LEAFLIST) { /* continue by processing another metadata object for the following * leaf-list instance since they are always instantiated in JSON array */ prev = node; node = node->next; + + LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); goto next_entry; } @@ -900,12 +894,18 @@ representation_error: "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.", lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status), in_parent ? "" : "name"); - ret = LY_EVALID; + rc = LY_EVALID; cleanup: + if ((rc == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { + /* try to skip the invalid data */ + if ((r = lydjson_data_skip(lydctx->jsonctx))) { + rc = r; + } + } free(dynamic_prefname); LOG_LOCBACK(1, 0, 0, 0); - return ret; + return rc; } /** @@ -922,24 +922,26 @@ static void lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last, struct lysc_ext_instance *ext) { - if (*node_p) { - /* insert, keep first pointer correct */ - if (ext) { - lyplg_ext_insert(parent, *node_p); + if (!*node_p) { + return; + } + + /* insert, keep first pointer correct */ + if (ext) { + lyplg_ext_insert(parent, *node_p); + } else { + lyd_insert_node(parent, first_p, *node_p, last); + } + if (first_p) { + if (parent) { + *first_p = lyd_child(parent); } else { - lyd_insert_node(parent, first_p, *node_p, last); - } - if (first_p) { - if (parent) { - *first_p = lyd_child(parent); - } else { - while ((*first_p)->prev->next) { - *first_p = (*first_p)->prev; - } + while ((*first_p)->prev->next) { + *first_p = (*first_p)->prev; } } - *node_p = NULL; } + *node_p = NULL; } /** @@ -950,8 +952,7 @@ lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, st * @param[in] name_len Length of the @p name string. * @param[in] prefix Prefix of the opaq node to create. * @param[in] prefix_len Length of the @p prefx string. - * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, - * but must be set if @p first is not. + * @param[in] parent Data parent of the opaq node, to inherit module name from. * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. * @param[out] node_p Pointer to the created opaq node. @@ -967,18 +968,25 @@ lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_l ly_bool dynamic = 0; uint32_t type_hint = 0; - if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) { + if (*status_inner_p != LYJSON_OBJECT) { /* prepare for creating opaq node with a value */ value = lydctx->jsonctx->value; value_len = lydctx->jsonctx->value_len; dynamic = lydctx->jsonctx->dynamic; lydctx->jsonctx->dynamic = 0; - LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint)); + LY_CHECK_RET(lydjson_value_type_hint(lydctx->jsonctx, status_inner_p, &type_hint)); } - /* create node */ + /* get the module name */ lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len); + if (!module_name && !parent && lydctx->any_schema) { + /* in an anyxml/anydata tree, parsing first node, use the previous any schema node */ + module_name = lydctx->any_schema->module->name; + module_name_len = strlen(module_name); + } + + /* create node */ ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value, value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p); if (dynamic) { @@ -1016,61 +1024,80 @@ lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_le struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p) { - LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p)); + LY_ERR ret = LY_SUCCESS; + + LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p), cleanup); + + assert(*node_p); + LOG_LOCSET(NULL, *node_p, NULL, NULL); if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) { /* special array null value */ ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY; /* must be the only item */ - LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p)); + LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup); if (*status_inner_p != LYJSON_ARRAY_CLOSED) { LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member."); - return LY_EVALID; + ret = LY_EVALID; + goto cleanup; } goto finish; } - while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) { + while (*status_p == LYJSON_ARRAY) { /* process another instance of the same node */ - - if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) { + if (*status_inner_p == LYJSON_OBJECT) { /* array with objects, list */ ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST; /* but first process children of the object in the array */ - while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) { - LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); - *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0); - } + do { + LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL), cleanup); + *status_inner_p = lyjson_ctx_status(lydctx->jsonctx); + } while (*status_inner_p == LYJSON_OBJECT_NEXT); } else { /* array with values, leaf-list */ ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST; } - LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p)); + LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup); if (*status_inner_p == LYJSON_ARRAY_CLOSED) { goto finish; } + assert(*status_inner_p == LYJSON_ARRAY_NEXT); /* continue with the next instance */ - assert(node_p); + LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup); + assert(*node_p); lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, NULL); - LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p)); + + LOG_LOCBACK(0, 1, 0, 0); + + LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p), cleanup); + + assert(*node_p); + LOG_LOCSET(NULL, *node_p, NULL, NULL); } - if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) { + if (*status_p == LYJSON_OBJECT) { /* process children */ - while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) { - LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); - *status_p = lyjson_ctx_status(lydctx->jsonctx, 0); - } + do { + LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL), cleanup); + *status_p = lyjson_ctx_status(lydctx->jsonctx); + } while (*status_p == LYJSON_OBJECT_NEXT); } finish: /* finish linking metadata */ - return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p)); + ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p)); + +cleanup: + if (*node_p) { + LOG_LOCBACK(0, 1, 0, 0); + } + return ret; } /** @@ -1094,8 +1121,8 @@ finish: * @return LY_ERR value. */ static LY_ERR -lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, - const char *prefix, size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, +lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, + size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p) { enum LYJSON_PARSER_STATUS status_inner = 0; @@ -1232,79 +1259,94 @@ static LY_ERR lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) { - LY_ERR rc = LY_SUCCESS; - uint32_t prev_parse_opts, prev_int_opts; + LY_ERR r, rc = LY_SUCCESS; + uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts; struct ly_in in_start; char *val = NULL; - struct lyd_node *tree = NULL; + const char *end; + struct lyd_node *child = NULL; + ly_bool log_node = 0; assert(snode->nodetype & LYD_NODE_ANY); + *node = NULL; + /* status check according to allowed JSON types */ if (snode->nodetype == LYS_ANYXML) { - LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY) && - (*status != LYJSON_ARRAY_EMPTY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && - (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT); + LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && + (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && + (*status != LYJSON_NULL), LY_ENOT); } else { - LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT); + LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT); } /* create any node */ switch (*status) { case LYJSON_OBJECT: + /* create node */ + r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + + assert(*node); + LOG_LOCSET(NULL, *node, NULL, NULL); + log_node = 1; + /* parse any data tree with correct options, first backup the current options and then make the parser * process data as opaq nodes */ - prev_parse_opts = lydctx->parse_opts; lydctx->parse_opts &= ~LYD_PARSE_STRICT; lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0); - prev_int_opts = lydctx->int_opts; lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; + lydctx->any_schema = snode; /* process the anydata content */ - while (*status != LYJSON_OBJECT_CLOSED) { - LY_CHECK_RET(lydjson_subtree_r(lydctx, NULL, &tree, NULL)); - *status = lyjson_ctx_status(lydctx->jsonctx, 0); - } + do { + r = lydjson_subtree_r(lydctx, NULL, &child, NULL); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); - /* restore parser options */ - lydctx->parse_opts = prev_parse_opts; - lydctx->int_opts = prev_int_opts; + *status = lyjson_ctx_status(lydctx->jsonctx); + } while (*status == LYJSON_OBJECT_NEXT); /* finish linking metadata */ - LY_CHECK_RET(lydjson_metadata_finish(lydctx, &tree)); + r = lydjson_metadata_finish(lydctx, &child); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); - LY_CHECK_RET(lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node)); - break; - case LYJSON_ARRAY_EMPTY: - /* store the empty array */ - if (asprintf(&val, "[]") == -1) { - LOGMEM(lydctx->jsonctx->ctx); - return LY_EMEM; - } - LY_CHECK_GOTO(rc = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node), val_err); + /* assign the data tree */ + ((struct lyd_node_any *)*node)->value.tree = child; + child = NULL; break; case LYJSON_ARRAY: /* skip until the array end */ in_start = *lydctx->jsonctx->in; - LY_CHECK_RET(lydjson_data_skip(lydctx->jsonctx)); + LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup); + + /* return back by all the WS */ + end = lydctx->jsonctx->in->current; + while (is_jsonws(end[-1])) { + --end; + } /* make a copy of the whole array and store it */ - if (asprintf(&val, "[%.*s", (int)(lydctx->jsonctx->in->current - in_start.current), in_start.current) == -1) { + if (asprintf(&val, "[%.*s", (int)(end - in_start.current), in_start.current) == -1) { LOGMEM(lydctx->jsonctx->ctx); - return LY_EMEM; + rc = LY_EMEM; + goto cleanup; } - LY_CHECK_GOTO(rc = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node), val_err); + r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + val = NULL; break; case LYJSON_STRING: /* string value */ if (lydctx->jsonctx->dynamic) { - LY_CHECK_RET(lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node)); + LY_CHECK_GOTO(rc = lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node), cleanup); lydctx->jsonctx->dynamic = 0; } else { val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); - LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM); + LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup); - LY_CHECK_GOTO(rc = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node), val_err); + r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + val = NULL; } break; case LYJSON_NUMBER: @@ -1313,26 +1355,32 @@ lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, st /* JSON value */ assert(!lydctx->jsonctx->dynamic); val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); - LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM); + LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup); - LY_CHECK_GOTO(rc = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node), val_err); + r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + val = NULL; break; case LYJSON_NULL: /* no value */ - LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node)); - break; - case LYJSON_OBJECT_EMPTY: - /* empty object */ - LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node)); + r = lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); break; default: - LOGINT_RET(lydctx->jsonctx->ctx); + LOGINT(lydctx->jsonctx->ctx); + rc = LY_EINT; + goto cleanup; } - return LY_SUCCESS; - -val_err: +cleanup: + if (log_node) { + LOG_LOCBACK(0, 1, 0, 0); + } + lydctx->parse_opts = prev_parse_opts; + lydctx->int_opts = prev_int_opts; + lydctx->any_schema = NULL; free(val); + lyd_free_tree(child); return rc; } @@ -1352,10 +1400,10 @@ static LY_ERR lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) { - LY_ERR ret = LY_SUCCESS; + LY_ERR r, rc = LY_SUCCESS; uint32_t prev_parse_opts = lydctx->parse_opts; - LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT); + LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT); /* create inner node */ LY_CHECK_RET(lyd_create_inner(snode, node)); @@ -1369,38 +1417,43 @@ lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node } /* process children */ - while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) { - ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); - LY_CHECK_GOTO(ret, cleanup); - *status = lyjson_ctx_status(lydctx->jsonctx, 0); - } + do { + r = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + + *status = lyjson_ctx_status(lydctx->jsonctx); + } while (*status == LYJSON_OBJECT_NEXT); /* finish linking metadata */ - ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node)); - LY_CHECK_GOTO(ret, cleanup); + r = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node)); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); if (snode->nodetype == LYS_LIST) { /* check all keys exist */ - ret = lyd_parse_check_keys(*node); - LY_CHECK_GOTO(ret, cleanup); + r = lyd_parse_check_keys(*node); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); } - if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { - /* new node validation, autodelete CANNOT occur, all nodes are new */ - ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL); - LY_CHECK_GOTO(ret, cleanup); + if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) { + /* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */ + r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); /* add any missing default children */ - ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, - &lydctx->node_types, &lydctx->ext_node, - (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); - LY_CHECK_GOTO(ret, cleanup); + r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types, + &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); } cleanup: lydctx->parse_opts = prev_parse_opts; LOG_LOCBACK(0, 1, 0, 0); - return ret; + if (!(*node)->hash) { + /* list without keys is unusable */ + lyd_free_tree(*node); + *node = NULL; + } + return rc; } /** @@ -1427,52 +1480,61 @@ lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, str const struct lysc_node *snode, struct lysc_ext_instance *ext, const char *name, size_t name_len, const char *prefix, size_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) { - LY_ERR ret = LY_SUCCESS; + LY_ERR r, rc = LY_SUCCESS; uint32_t type_hints = 0; LOG_LOCSET(snode, NULL, NULL, NULL); - ret = lydjson_data_check_opaq(lydctx, snode, &type_hints); - if (ret == LY_SUCCESS) { + r = lydjson_data_check_opaq(lydctx, snode, &type_hints); + if (r == LY_SUCCESS) { assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY)); if (snode->nodetype & LYD_NODE_TERM) { if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) { - ret = LY_ENOT; + rc = LY_ENOT; goto cleanup; } /* create terminal node */ - LY_CHECK_GOTO(ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value, - lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, type_hints, node), cleanup); + r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value, + lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, type_hints, node); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); /* move JSON parser */ if (*status == LYJSON_ARRAY) { /* only [null], 2 more moves are needed */ - LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status), cleanup); + r = lyjson_ctx_next(lydctx->jsonctx, status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); assert(*status == LYJSON_NULL); - LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status), cleanup); + + r = lyjson_ctx_next(lydctx->jsonctx, status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); assert(*status == LYJSON_ARRAY_CLOSED); } } else if (snode->nodetype & LYD_NODE_INNER) { /* create inner node */ - LY_CHECK_GOTO(ret = lydjson_parse_instance_inner(lydctx, snode, ext, status, node), cleanup); + r = lydjson_parse_instance_inner(lydctx, snode, ext, status, node); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); } else { /* create any node */ - LY_CHECK_GOTO(ret = lydjson_parse_any(lydctx, snode, ext, status, node), cleanup); + r = lydjson_parse_any(lydctx, snode, ext, status, node); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); } + LY_CHECK_GOTO(!*node, cleanup); /* add/correct flags */ - LY_CHECK_GOTO(ret = lyd_parse_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext), cleanup); + r = lyd_parse_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { /* store for ext instance node validation, if needed */ - LY_CHECK_GOTO(ret = lyd_validate_node_ext(*node, &lydctx->ext_node), cleanup); + r = lyd_validate_node_ext(*node, &lydctx->ext_node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); } - } else if (ret == LY_ENOT) { + } else if (r == LY_ENOT) { /* parse it again as an opaq node */ - LY_CHECK_GOTO(ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, - first_p, node), cleanup); + r = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); if (snode->nodetype == LYS_LIST) { ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST; @@ -1481,12 +1543,13 @@ lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, str } } else { /* error */ + rc = r; goto cleanup; } cleanup: LOG_LOCBACK(1, 0, 0, 0); - return ret; + return rc; } /** @@ -1501,36 +1564,80 @@ cleanup: static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) { - LY_ERR ret = LY_SUCCESS, r; - enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0); + LY_ERR r, rc = LY_SUCCESS; + enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx); const char *name, *prefix = NULL, *expected = NULL; size_t name_len, prefix_len = 0; ly_bool is_meta = 0, parse_subtree; const struct lysc_node *snode = NULL; - struct lysc_ext_instance *ext; + struct lysc_ext_instance *ext = NULL; struct lyd_node *node = NULL, *attr_node = NULL; const struct ly_ctx *ctx = lydctx->jsonctx->ctx; char *value = NULL; assert(parent || first_p); - assert(status == LYJSON_OBJECT); + assert((status == LYJSON_OBJECT) || (status == LYJSON_OBJECT_NEXT)); parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0; /* all descendants should be parsed */ lydctx->parse_opts &= ~LYD_PARSE_SUBTREE; + r = lyjson_ctx_next(lydctx->jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + if (status == LYJSON_OBJECT_CLOSED) { + /* empty object, fine... */ + goto cleanup; + } + /* process the node name */ + assert(status == LYJSON_OBJECT_NAME); lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta); lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value); - if (!is_meta || name_len || prefix_len) { + if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && !is_meta && name_len && !prefix_len && + !ly_strncmp("eventTime", name, name_len)) { + /* parse eventTime */ + r = lyjson_ctx_next(lydctx->jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + + if (status != LYJSON_STRING) { + LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_STRING), + lyjson_token2str(status)); + rc = LY_EVALID; + goto cleanup; + } + + /* create node */ + r = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, prefix, prefix_len, + lydctx->jsonctx->value, lydctx->jsonctx->value_len, NULL, LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, &node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + + /* validate the value */ + r = lyd_parser_notif_eventtime_validate(node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + + goto node_parsed; + } else if (!is_meta || name_len || prefix_len) { /* get the schema node */ r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext); if (r == LY_ENOT) { /* data parsed */ goto cleanup; + } else if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) { + rc = r; + + /* skip the invalid data */ + if ((r = lydjson_data_skip(lydctx->jsonctx))) { + rc = r; + } + r = lyjson_ctx_next(lydctx->jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + goto cleanup; + } else if (r) { + /* error */ + rc = r; + goto cleanup; } - LY_CHECK_ERR_GOTO(r, ret = r, cleanup); if (!snode) { /* we will not be parsing it as metadata */ @@ -1540,39 +1647,44 @@ lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct l if (is_meta) { /* parse as metadata */ - if (!name_len && !prefix_len) { + if (!name_len && !prefix_len && !parent) { + LOGVAL(ctx, LYVE_SYNTAX_JSON, + "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries."); + r = LY_EVALID; + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + } else if (!name_len && !prefix_len) { /* parent's metadata without a name - use the schema from the parent */ - if (!parent) { - LOGVAL(ctx, LYVE_SYNTAX_JSON, - "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries."); - ret = LY_EVALID; - goto cleanup; - } attr_node = parent; snode = attr_node->schema; } - ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status, + r = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status, first_p, &node); - LY_CHECK_GOTO(ret, cleanup); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); } else if (!snode) { - /* parse as an opaq node */ - assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts)); + if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { + /* skip element with children */ + r = lydjson_data_skip(lydctx->jsonctx); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + } else { + /* parse as an opaq node */ - /* opaq node cannot have an empty string as the name. */ - if (name_len == 0) { - LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string."); - ret = LY_EVALID; - goto cleanup; - } + /* opaq node cannot have an empty string as the name. */ + if (name_len == 0) { + LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "JSON object member name cannot be a zero-length string."); + r = LY_EVALID; + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + } - /* move to the second item in the name/X pair and parse opaq */ - ret = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node); - LY_CHECK_GOTO(ret, cleanup); + /* move to the second item in the name/X pair and parse opaq */ + r = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + } } else { /* parse as a standard lyd_node but it can still turn out to be an opaque node */ /* move to the second item in the name/X pair */ - LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); + r = lyjson_ctx_next(lydctx->jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); /* set expected representation */ switch (snode->nodetype) { @@ -1609,30 +1721,31 @@ lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct l switch (snode->nodetype) { case LYS_LEAFLIST: case LYS_LIST: - if (status == LYJSON_ARRAY_EMPTY) { - /* no instances, skip */ - break; - } LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); - /* move into array */ - ret = lyjson_ctx_next(lydctx->jsonctx, &status); - LY_CHECK_GOTO(ret, cleanup); - /* process all the values/objects */ do { - ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, + /* move into array/next value */ + r = lyjson_ctx_next(lydctx->jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + if (status == LYJSON_ARRAY_CLOSED) { + /* empty array, fine... */ + break; + } + + r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, &status, &node); - if (ret == LY_ENOT) { + if (r == LY_ENOT) { goto representation_error; - } else if (ret) { - goto cleanup; } + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext); /* move after the item(s) */ - LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); - } while (status != LYJSON_ARRAY_CLOSED); + r = lyjson_ctx_next(lydctx->jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + } while (status == LYJSON_ARRAY_NEXT); break; case LYS_LEAF: @@ -1643,13 +1756,12 @@ lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct l case LYS_ANYDATA: case LYS_ANYXML: /* process the value/object */ - ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, + r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len, &status, &node); - if (ret == LY_ENOT) { + if (r == LY_ENOT) { goto representation_error; - } else if (ret) { - goto cleanup; } + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { /* rememeber the RPC/action/notification */ @@ -1659,31 +1771,39 @@ lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct l } } - /* finally connect the parsed node */ - lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext); - +node_parsed: /* rememeber a successfully parsed node */ if (parsed && node) { ly_set_add(parsed, node, 1, NULL); } + /* finally connect the parsed node, is zeroed */ + lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext); + if (!parse_subtree) { /* move after the item(s) */ - LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup); + r = lyjson_ctx_next(lydctx->jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); } /* success */ goto cleanup; representation_error: - LOGVAL(ctx, LYVE_SYNTAX_JSON, "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.", - lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status)); - ret = LY_EVALID; + LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as name/%s.", + expected, lys_nodetype2str(snode->nodetype), snode->name, lyjson_token2str(status)); + rc = LY_EVALID; + if (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) { + /* try to skip the invalid data */ + if ((r = lydjson_data_skip(lydctx->jsonctx))) { + rc = r; + } + } cleanup: free(value); lyd_free_tree(node); - return ret; + return rc; } /** @@ -1694,20 +1814,17 @@ cleanup: * @param[in] parse_opts Options for parser, see @ref dataparseroptions. * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions. * @param[out] lydctx_p Data parser context to finish validation. - * @param[out] status Storage for the current context's status * @return LY_ERR value. */ static LY_ERR lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, - struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status) + struct lyd_json_ctx **lydctx_p) { LY_ERR ret = LY_SUCCESS; struct lyd_json_ctx *lydctx; - size_t i; - ly_bool subtree; + enum LYJSON_PARSER_STATUS status; assert(lydctx_p); - assert(status); /* init context */ lydctx = calloc(1, sizeof *lydctx); @@ -1716,28 +1833,20 @@ lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_o lydctx->val_opts = val_opts; lydctx->free = lyd_json_ctx_free; - /* starting top-level */ - for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) { - if (in->current[i] == '\n') { - /* new line */ - LY_IN_NEW_LINE(in); - } - } - - subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0; - LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret); - *status = lyjson_ctx_status(lydctx->jsonctx, 0); + LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret); + status = lyjson_ctx_status(lydctx->jsonctx); - if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) { - *lydctx_p = lydctx; - return LY_SUCCESS; - } else { + /* parse_opts & LYD_PARSE_SUBTREE not implemented */ + if (status != LYJSON_OBJECT) { /* expecting top-level object */ - LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status)); + LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(status)); *lydctx_p = NULL; lyd_json_ctx_free((struct lyd_ctx *)lydctx); return LY_EVALID; } + + *lydctx_p = lydctx; + return LY_SUCCESS; } LY_ERR @@ -1745,14 +1854,12 @@ lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, st struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts, struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p) { - LY_ERR rc = LY_SUCCESS; + LY_ERR r, rc = LY_SUCCESS; struct lyd_json_ctx *lydctx = NULL; enum LYJSON_PARSER_STATUS status; - rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status); - LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup); - - assert(status == LYJSON_OBJECT); + rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx); + LY_CHECK_GOTO(rc, cleanup); lydctx->int_opts = int_opts; lydctx->ext = ext; @@ -1761,37 +1868,36 @@ lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, st LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); /* read subtree(s) */ - while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) { - rc = lydjson_subtree_r(lydctx, parent, first_p, parsed); - LY_CHECK_GOTO(rc, cleanup); + do { + r = lydjson_subtree_r(lydctx, parent, first_p, parsed); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); - status = lyjson_ctx_status(lydctx->jsonctx, 0); + status = lyjson_ctx_status(lydctx->jsonctx); if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { break; } - } + } while (status == LYJSON_OBJECT_NEXT); - if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && - (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) { + if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) { LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); - rc = LY_EVALID; - goto cleanup; + r = LY_EVALID; + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); } if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); - rc = LY_EVALID; - goto cleanup; + r = LY_EVALID; + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); } /* finish linking metadata */ - rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); - LY_CHECK_GOTO(rc, cleanup); + r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); if (parse_opts & LYD_PARSE_SUBTREE) { /* check for a sibling object */ assert(subtree_sibling); - if (lydctx->jsonctx->in->current[0] == ',') { + if (status == LYJSON_OBJECT_NEXT) { *subtree_sibling = 1; /* move to the next object */ @@ -1806,6 +1912,198 @@ cleanup: assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && !lydctx->node_when.count)); + if (rc && (!lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) { + lyd_json_ctx_free((struct lyd_ctx *)lydctx); + } else { + *lydctx_p = (struct lyd_ctx *)lydctx; + + /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */ + lyjson_ctx_free(lydctx->jsonctx); + lydctx->jsonctx = NULL; + } + return rc; +} + +/** + * @brief Parse a specific JSON object into an opaque node. + * + * @param[in] jsonctx JSON parser context. + * @param[in] name Name of the object. + * @param[in] module Module name of the object, NULL if none expected. + * @param[out] evnp Parsed envelope (opaque node). + * @return LY_SUCCESS on success. + * @return LY_ENOT if the specified object did not match. + * @return LY_ERR value on error. + */ +static LY_ERR +lydjson_envelope(struct lyjson_ctx *jsonctx, const char *name, const char *module, struct lyd_node **envp) +{ + LY_ERR rc = LY_SUCCESS, r; + enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx); + const char *nam, *prefix; + size_t nam_len, prefix_len; + ly_bool is_meta; + + assert(status == LYJSON_OBJECT); + + *envp = NULL; + + r = lyjson_ctx_next(jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + if (status == LYJSON_OBJECT_CLOSED) { + LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Empty JSON object."); + rc = LY_EVALID; + goto cleanup; + } + + /* process the name */ + assert(status == LYJSON_OBJECT_NAME); + lydjson_parse_name(jsonctx->value, jsonctx->value_len, &nam, &nam_len, &prefix, &prefix_len, &is_meta); + if (is_meta) { + LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected metadata."); + rc = LY_EVALID; + goto cleanup; + } else if (module && ly_strncmp(module, prefix, prefix_len)) { + LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected module \"%.*s\" instead of \"%s\".", (int)prefix_len, prefix, module); + rc = LY_EVALID; + goto cleanup; + } else if (ly_strncmp(name, nam, nam_len)) { + LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected object \"%.*s\" instead of \"%s\".", (int)nam_len, nam, name); + rc = LY_EVALID; + goto cleanup; + } + + r = lyjson_ctx_next(jsonctx, &status); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + + /* create node */ + rc = lyd_create_opaq(jsonctx->ctx, name, strlen(name), prefix, prefix_len, prefix, prefix_len, NULL, 0, NULL, + LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, envp); + LY_CHECK_GOTO(rc, cleanup); + +cleanup: + if (rc) { + lyd_free_tree(*envp); + *envp = NULL; + } + return rc; +} + +LY_ERR +lyd_parse_json_restconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, + struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, + struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p) +{ + LY_ERR rc = LY_SUCCESS, r; + struct lyd_json_ctx *lydctx = NULL; + struct lyd_node *node; + uint32_t i, int_opts = 0, close_elem = 0; + + assert(ctx && in && lydctx_p); + assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); + assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); + + assert((data_type == LYD_TYPE_RPC_RESTCONF) || (data_type == LYD_TYPE_NOTIF_RESTCONF) || + (data_type == LYD_TYPE_REPLY_RESTCONF)); + assert(!(parse_opts & LYD_PARSE_SUBTREE)); + + /* init context */ + rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx); + LY_CHECK_GOTO(rc, cleanup); + lydctx->ext = ext; + + switch (data_type) { + case LYD_TYPE_RPC_RESTCONF: + assert(parent); + + /* parse "input" */ + rc = lydjson_envelope(lydctx->jsonctx, "input", lyd_node_module(parent)->name, envp); + LY_CHECK_GOTO(rc, cleanup); + + int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION; + close_elem = 1; + break; + case LYD_TYPE_NOTIF_RESTCONF: + assert(!parent); + + /* parse "notification" */ + rc = lydjson_envelope(lydctx->jsonctx, "notification", "ietf-restconf", envp); + LY_CHECK_GOTO(rc, cleanup); + + /* RESTCONF notification and eventTime */ + int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME; + close_elem = 1; + break; + case LYD_TYPE_REPLY_RESTCONF: + assert(parent); + + /* parse "output" */ + rc = lydjson_envelope(lydctx->jsonctx, "output", lyd_node_module(parent)->name, envp); + LY_CHECK_GOTO(rc, cleanup); + + int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY; + close_elem = 1; + break; + default: + LOGINT(ctx); + rc = LY_EINT; + goto cleanup; + } + + lydctx->int_opts = int_opts; + + /* find the operation node if it exists already */ + LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); + + /* read subtree(s) */ + do { + r = lydjson_subtree_r(lydctx, parent, first_p, parsed); + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + } while (lyjson_ctx_status(lydctx->jsonctx) == LYJSON_OBJECT_NEXT); + + /* close all opened elements */ + for (i = 0; i < close_elem; ++i) { + if (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_OBJECT_CLOSED) { + LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_OBJECT_CLOSED), + lyjson_token2str(lyjson_ctx_status(lydctx->jsonctx))); + rc = LY_EVALID; + goto cleanup; + } + + r = lyjson_ctx_next(lydctx->jsonctx, NULL); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + } + + if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { + LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); + r = LY_EVALID; + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + } + if (int_opts & LYD_INTOPT_EVENTTIME) { + /* parse as a child of the envelope */ + node = (*first_p)->prev; + if (node->schema) { + LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node."); + r = LY_EVALID; + LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup); + } else { + /* can be the only opaque node and an operation had to be parsed */ + assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next); + lyd_unlink(node); + assert(*envp); + lyd_insert_child(*envp, node); + } + } + + /* finish linking metadata */ + r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); + LY_CHECK_ERR_GOTO(r, rc = r, cleanup); + +cleanup: + /* there should be no unres stored if validation should be skipped */ + assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && + !lydctx->node_when.count)); + if (rc) { lyd_json_ctx_free((struct lyd_ctx *)lydctx); } else { |