Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_json.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief JSON data parser for libyang |
| 5 | * |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 7 | * |
| 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 |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 16 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 18 | #include <stdint.h> |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include "common.h" |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 23 | #include "compat.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 24 | #include "context.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 25 | #include "dict.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 26 | #include "in_internal.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 27 | #include "json.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 28 | #include "log.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 29 | #include "parser_data.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 30 | #include "parser_internal.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 31 | #include "set.h" |
| 32 | #include "tree.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 33 | #include "tree_data.h" |
| 34 | #include "tree_data_internal.h" |
| 35 | #include "tree_schema.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 36 | #include "tree_schema_internal.h" |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 37 | #include "validation.h" |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 40 | * @brief Free the JSON data parser context. |
| 41 | * |
| 42 | * JSON implementation of lyd_ctx_free_clb(). |
| 43 | */ |
| 44 | static void |
| 45 | lyd_json_ctx_free(struct lyd_ctx *lydctx) |
| 46 | { |
| 47 | struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx; |
| 48 | |
| 49 | if (lydctx) { |
| 50 | lyd_ctx_free(lydctx); |
| 51 | lyjson_ctx_free(ctx->jsonctx); |
| 52 | free(ctx); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 57 | * @brief Submit the responsibility for releasing the dynamic values to @p dst. |
| 58 | * |
| 59 | * @param[in] jsonctx JSON context which contains the dynamic value. |
| 60 | * @param[in,out] dst Pointer to which the responsibility will be submited. |
| 61 | * If the pointer is already pointing to some allocated memory, |
| 62 | * it is released beforehand. |
| 63 | */ |
| 64 | static void |
| 65 | lyjson_ctx_submit_dynamic_value(struct lyjson_ctx *jsonctx, char **dst) |
| 66 | { |
| 67 | assert(jsonctx && dst); |
| 68 | |
| 69 | if (!jsonctx->dynamic) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if (dst) { |
| 74 | free(*dst); |
| 75 | } |
| 76 | *dst = NULL; |
| 77 | |
| 78 | /* Submit the dynamic value. */ |
| 79 | *dst = (char *)jsonctx->value; |
| 80 | |
| 81 | /* Responsibility for the release is now passed to @p dst. */ |
| 82 | jsonctx->dynamic = 0; |
| 83 | } |
| 84 | |
| 85 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 86 | * @brief Parse JSON member-name as [\@][prefix:][name] |
| 87 | * |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 88 | * \@ - metadata flag, maps to 1 in @p is_meta_p |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 89 | * prefix - name of the module of the data node |
| 90 | * name - name of the data node |
| 91 | * |
| 92 | * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller. |
| 93 | * |
| 94 | * @param[in] value String to parse |
| 95 | * @param[in] value_len Length of the @p str. |
| 96 | * @param[out] name_p Pointer to the beginning of the parsed name. |
| 97 | * @param[out] name_len_p Pointer to the length of the parsed name. |
| 98 | * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL. |
| 99 | * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0. |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 100 | * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 101 | */ |
| 102 | static void |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 103 | lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p, |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 104 | size_t *prefix_len_p, ly_bool *is_meta_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 105 | { |
| 106 | const char *name, *prefix = NULL; |
| 107 | size_t name_len, prefix_len = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 108 | ly_bool is_meta = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 109 | |
| 110 | name = memchr(value, ':', value_len); |
| 111 | if (name != NULL) { |
| 112 | prefix = value; |
| 113 | if (*prefix == '@') { |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 114 | is_meta = 1; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 115 | prefix++; |
| 116 | } |
| 117 | prefix_len = name - prefix; |
| 118 | name++; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 119 | name_len = value_len - (prefix_len + 1) - is_meta; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 120 | } else { |
| 121 | name = value; |
| 122 | if (name[0] == '@') { |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 123 | is_meta = 1; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 124 | name++; |
| 125 | } |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 126 | name_len = value_len - is_meta; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 129 | *name_p = name; |
| 130 | *name_len_p = name_len; |
| 131 | *prefix_p = prefix; |
| 132 | *prefix_len_p = prefix_len; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 133 | *is_meta_p = is_meta; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @brief Get correct prefix (module_name) inside the @p node. |
| 138 | * |
| 139 | * @param[in] node Data node to get inherited prefix. |
| 140 | * @param[in] local_prefix Local prefix to replace the inherited prefix. |
| 141 | * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken. |
| 142 | * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix |
| 143 | * and no context @p node to get inherited prefix. |
| 144 | * @param[out] prefix_len_p Pointer to the length of the resulting @p prefix_p string. Note that the result can be 0 in case |
| 145 | * of no local prefix and no context @p node to get inherited prefix. |
| 146 | * @return LY_ERR value. |
| 147 | */ |
| 148 | static LY_ERR |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 149 | lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 150 | size_t *prefix_len_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 151 | { |
| 152 | struct lyd_node_opaq *onode; |
| 153 | const char *module_name = NULL; |
| 154 | |
| 155 | assert(prefix_p && prefix_len_p); |
| 156 | |
| 157 | if (local_prefix_len) { |
| 158 | *prefix_p = local_prefix; |
| 159 | *prefix_len_p = local_prefix_len; |
| 160 | return LY_SUCCESS; |
| 161 | } |
| 162 | |
| 163 | *prefix_p = NULL; |
| 164 | while (node) { |
| 165 | if (node->schema) { |
| 166 | *prefix_p = node->schema->module->name; |
| 167 | break; |
| 168 | } |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 169 | onode = (struct lyd_node_opaq *)node; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 170 | if (onode->name.module_name) { |
| 171 | *prefix_p = onode->name.module_name; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 172 | break; |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 173 | } else if (onode->name.prefix) { |
| 174 | *prefix_p = onode->name.prefix; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 175 | break; |
| 176 | } |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 177 | node = lyd_parent(node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 178 | } |
| 179 | *prefix_len_p = ly_strlen(module_name); |
| 180 | |
| 181 | return LY_SUCCESS; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @brief Get schema node corresponding to the input parameters. |
| 186 | * |
| 187 | * @param[in] lydctx JSON data parser context. |
| 188 | * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only. |
| 189 | * @param[in] prefix Requested node's prefix (module name). |
| 190 | * @param[in] prefix_len Length of the @p prefix. |
| 191 | * @param[in] name Requested node's name. |
| 192 | * @param[in] name_len Length of the @p name. |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 193 | * @param[in] parent Parent of the node being processed, can be NULL in case of top-level. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 194 | * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters. |
| 195 | * @return LY_SUCCES on success, note that even in this case the returned value of @p snode_p can be NULL, so the data are expected to be parsed as opaq. |
| 196 | * @return LY_EVALID on failure, error message is logged |
| 197 | * @return LY_ENOT in case the input data are expected to be skipped |
| 198 | */ |
| 199 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 200 | lydjson_get_snode(const struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 201 | size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 202 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 203 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 204 | struct lys_module *mod = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 205 | uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 206 | |
| 207 | /* init return value */ |
| 208 | *snode_p = NULL; |
| 209 | |
Michal Vasko | 9e54340 | 2021-06-11 13:55:30 +0200 | [diff] [blame] | 210 | LOG_LOCSET(NULL, parent ? &parent->node : NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 211 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 212 | /* get the element module */ |
Michal Vasko | 2552ea3 | 2020-12-08 15:32:34 +0100 | [diff] [blame] | 213 | if (prefix_len) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 214 | mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len); |
| 215 | } else if (parent) { |
| 216 | if (parent->schema) { |
| 217 | mod = parent->schema->module; |
| 218 | } |
| 219 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 220 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 221 | (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 222 | ret = LY_EVALID; |
| 223 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 224 | } |
| 225 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 226 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 227 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 228 | ret = LY_EVALID; |
| 229 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 230 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 231 | if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 232 | ret = LY_ENOT; |
| 233 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 237 | /* get the schema node */ |
Michal Vasko | 5d54ad7 | 2021-07-22 15:36:01 +0200 | [diff] [blame] | 238 | if (mod && (!parent || parent->schema)) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 239 | if (!parent && lydctx->ext) { |
Radek Krejci | ba05eab | 2021-03-10 13:19:29 +0100 | [diff] [blame] | 240 | *snode_p = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 241 | } else { |
| 242 | *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts); |
| 243 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 244 | if (!*snode_p) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 245 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 246 | if (lydctx->ext) { |
| 247 | if (lydctx->ext->argument) { |
| 248 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 249 | (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 250 | } else { |
| 251 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 252 | (int)name_len, name, lydctx->ext->def->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 253 | } |
| 254 | } else { |
| 255 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 256 | (int)name_len, name, mod->name); |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 257 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 258 | ret = LY_EVALID; |
| 259 | goto cleanup; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 260 | } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 261 | /* skip element with children */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 262 | ret = LY_ENOT; |
| 263 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 264 | } |
| 265 | } else { |
| 266 | /* check that schema node is valid and can be used */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 267 | ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 271 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 272 | LOG_LOCBACK(0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 273 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
| 277 | * @brief Skip the currently open JSON object/array |
| 278 | * @param[in] jsonctx JSON context with the input data to skip. |
| 279 | * @return LY_ERR value. |
| 280 | */ |
| 281 | static LY_ERR |
| 282 | lydjson_data_skip(struct lyjson_ctx *jsonctx) |
| 283 | { |
| 284 | enum LYJSON_PARSER_STATUS status, current; |
| 285 | size_t sublevels = 1; |
| 286 | |
| 287 | status = lyjson_ctx_status(jsonctx, 0); |
| 288 | |
| 289 | /* skip after the content */ |
| 290 | do { |
Juraj Vijtiuk | 2e88368 | 2021-09-20 17:00:56 +0200 | [diff] [blame] | 291 | uint32_t prev_depth = jsonctx->depth; |
| 292 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 293 | LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t)); |
Juraj Vijtiuk | 2e88368 | 2021-09-20 17:00:56 +0200 | [diff] [blame] | 294 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 295 | if (current == status) { |
Juraj Vijtiuk | 2e88368 | 2021-09-20 17:00:56 +0200 | [diff] [blame] | 296 | /* lyjson_ctx_next() can return LYSJON_OBJECT in two cases, either when |
| 297 | * a new object is encountered, or when it finishes parsing a value from a |
| 298 | * previous key-value pair. In the latter case the sublevel shouldn't increase. |
| 299 | */ |
| 300 | if ((status == LYJSON_OBJECT) && (prev_depth == jsonctx->depth)) { |
| 301 | continue; |
| 302 | } |
| 303 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 304 | sublevels++; |
Juraj Vijtiuk | 2e88368 | 2021-09-20 17:00:56 +0200 | [diff] [blame] | 305 | } else if ((status == LYJSON_OBJECT) && (current == LYJSON_OBJECT_CLOSED)) { |
Juraj Vijtiuk | 46eb15e | 2021-09-06 15:38:36 +0200 | [diff] [blame] | 306 | sublevels--; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 307 | } |
Michal Vasko | c2da307 | 2022-01-03 11:13:25 +0100 | [diff] [blame] | 308 | } while ((current != status + 1) || sublevels); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 309 | |
| 310 | return LY_SUCCESS; |
| 311 | } |
| 312 | |
| 313 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 314 | * @brief Check that the input data are parseable as the @p list. |
| 315 | * |
| 316 | * Checks for all the list's keys. Function does not revert the context state. |
| 317 | * |
| 318 | * @param[in] jsonctx JSON parser context. |
| 319 | * @param[in] list List schema node corresponding to the input data object. |
| 320 | * @return LY_SUCCESS in case the data are ok for the @p list |
| 321 | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance. |
| 322 | */ |
| 323 | static LY_ERR |
| 324 | lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list) |
| 325 | { |
| 326 | LY_ERR ret = LY_SUCCESS; |
| 327 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0); |
| 328 | struct ly_set key_set = {0}; |
| 329 | const struct lysc_node *snode; |
| 330 | uint32_t i, status_count; |
| 331 | |
| 332 | assert(list && (list->nodetype == LYS_LIST)); |
| 333 | assert(status == LYJSON_OBJECT); |
| 334 | |
| 335 | /* get all keys into a set (keys do not have if-features or anything) */ |
| 336 | snode = NULL; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 337 | while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) { |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 338 | ret = ly_set_add(&key_set, (void *)snode, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 339 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (status != LYJSON_OBJECT_EMPTY) { |
| 343 | status_count = jsonctx->status.count; |
| 344 | |
| 345 | while (key_set.count && status != LYJSON_OBJECT_CLOSED) { |
| 346 | const char *name, *prefix; |
| 347 | size_t name_len, prefix_len; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 348 | ly_bool is_attr; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 349 | |
| 350 | /* match the key */ |
| 351 | snode = NULL; |
| 352 | lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
| 353 | |
| 354 | if (!is_attr && !prefix) { |
| 355 | for (i = 0; i < key_set.count; ++i) { |
| 356 | snode = (const struct lysc_node *)key_set.objs[i]; |
| 357 | if (!ly_strncmp(snode->name, name, name_len)) { |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | /* go into the item to a) process it as a key or b) start skipping it as another list child */ |
| 362 | ret = lyjson_ctx_next(jsonctx, &status); |
| 363 | LY_CHECK_GOTO(ret, cleanup); |
| 364 | |
| 365 | if (snode) { |
| 366 | /* we have the key, validate the value */ |
| 367 | if (status < LYJSON_NUMBER) { |
| 368 | /* not a terminal */ |
| 369 | ret = LY_ENOT; |
| 370 | goto cleanup; |
| 371 | } |
| 372 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 373 | ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 374 | LY_CHECK_GOTO(ret, cleanup); |
| 375 | |
| 376 | /* key with a valid value, remove from the set */ |
| 377 | ly_set_rm_index(&key_set, i, NULL); |
| 378 | } |
| 379 | } else { |
| 380 | /* start skipping the member we are not interested in */ |
| 381 | ret = lyjson_ctx_next(jsonctx, &status); |
| 382 | LY_CHECK_GOTO(ret, cleanup); |
| 383 | } |
| 384 | /* move to the next child */ |
| 385 | while (status_count < jsonctx->status.count) { |
| 386 | ret = lyjson_ctx_next(jsonctx, &status); |
| 387 | LY_CHECK_GOTO(ret, cleanup); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (key_set.count) { |
| 393 | /* some keys are missing/did not validate */ |
| 394 | ret = LY_ENOT; |
| 395 | } |
| 396 | |
| 397 | cleanup: |
| 398 | ly_set_erase(&key_set, NULL); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @brief Get the hint for the data type parsers according to the current JSON parser context. |
| 404 | * |
| 405 | * @param[in] lydctx JSON data parser context. The context is supposed to be on a value. |
| 406 | * @param[in,out] status Pointer to the current context status, |
| 407 | * in some circumstances the function manipulates with the context so the status is updated. |
| 408 | * @param[out] type_hint_p Pointer to the variable to store the result. |
| 409 | * @return LY_SUCCESS in case of success. |
| 410 | * @return LY_EINVAL in case of invalid context status not referring to a value. |
| 411 | */ |
| 412 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 413 | lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 414 | { |
| 415 | *type_hint_p = 0; |
| 416 | |
| 417 | if (*status_p == LYJSON_ARRAY) { |
| 418 | /* only [null] */ |
| 419 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); |
| 420 | LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL); |
| 421 | |
| 422 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL)); |
| 423 | LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL); |
| 424 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 425 | *type_hint_p = LYD_VALHINT_EMPTY; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 426 | } else if (*status_p == LYJSON_STRING) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 427 | *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 428 | } else if (*status_p == LYJSON_NUMBER) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 429 | *type_hint_p = LYD_VALHINT_DECNUM; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 430 | } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 431 | *type_hint_p = LYD_VALHINT_BOOLEAN; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 432 | } else if (*status_p == LYJSON_NULL) { |
| 433 | *type_hint_p = 0; |
| 434 | } else { |
| 435 | return LY_EINVAL; |
| 436 | } |
| 437 | |
| 438 | return LY_SUCCESS; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * @brief Check in advance if the input data are parsable according to the provided @p snode. |
| 443 | * |
| 444 | * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking |
| 445 | * is naturally done when the data are really parsed. |
| 446 | * |
| 447 | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
| 448 | * as before calling, despite it is necessary to process input data for checking. |
| 449 | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
| 450 | * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list. |
| 451 | * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled. |
| 452 | * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance |
| 453 | * @return LY_EINVAL in case of invalid leaf JSON encoding |
| 454 | * and they are expected to be parsed as opaq nodes. |
| 455 | */ |
| 456 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 457 | lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 458 | { |
| 459 | LY_ERR ret = LY_SUCCESS; |
| 460 | struct lyjson_ctx *jsonctx = lydctx->jsonctx; |
| 461 | enum LYJSON_PARSER_STATUS status; |
| 462 | |
| 463 | assert(snode); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 464 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 465 | if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) { |
| 466 | /* can always be parsed as a data node if we have the schema node */ |
| 467 | return LY_SUCCESS; |
| 468 | } |
| 469 | |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 470 | /* backup parser */ |
| 471 | lyjson_ctx_backup(jsonctx); |
| 472 | status = lyjson_ctx_status(jsonctx, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 473 | |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 474 | if (lydctx->parse_opts & LYD_PARSE_OPAQ) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 475 | /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed |
| 476 | * as an opaq node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 477 | switch (snode->nodetype) { |
| 478 | case LYS_LEAFLIST: |
| 479 | case LYS_LEAF: |
| 480 | /* value may not be valid in which case we parse it as an opaque node */ |
| 481 | ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); |
| 482 | if (ret) { |
| 483 | break; |
| 484 | } |
| 485 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 486 | if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 487 | ret = LY_ENOT; |
| 488 | } |
| 489 | break; |
| 490 | case LYS_LIST: |
| 491 | /* lists may not have all its keys */ |
| 492 | if (lydjson_check_list(jsonctx, snode)) { |
| 493 | /* invalid list, parse as opaque if it missing/has invalid some keys */ |
| 494 | ret = LY_ENOT; |
| 495 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 496 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 497 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 498 | } else if (snode->nodetype & LYD_NODE_TERM) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 499 | status = lyjson_ctx_status(jsonctx, 0); |
| 500 | ret = lydjson_value_type_hint(lydctx, &status, type_hint_p); |
| 501 | } |
| 502 | |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 503 | /* restore parser */ |
| 504 | lyjson_ctx_restore(jsonctx); |
| 505 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * @brief Join the forward-referencing metadata with their target data nodes. |
| 511 | * |
| 512 | * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists. |
| 513 | * |
| 514 | * @param[in] lydctx JSON data parser context. |
| 515 | * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node) |
| 516 | * as a starting point to search for the metadata's target data node |
| 517 | * @return LY_SUCCESS on success |
| 518 | * @return LY_EVALID in case there are some metadata with unresolved target data node instance |
| 519 | */ |
| 520 | static LY_ERR |
| 521 | lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p) |
| 522 | { |
| 523 | LY_ERR ret = LY_SUCCESS; |
aPiecek | 5057c2e | 2021-05-17 10:28:03 +0200 | [diff] [blame] | 524 | struct lyd_node *node, *attr, *next, *meta_iter; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 525 | uint64_t instance = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 526 | const char *prev = NULL; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 527 | uint32_t log_location_items = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 528 | |
| 529 | /* finish linking metadata */ |
| 530 | LY_LIST_FOR_SAFE(*first_p, next, attr) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 531 | struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 532 | uint64_t match = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 533 | ly_bool is_attr; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 534 | const char *name, *prefix; |
| 535 | size_t name_len, prefix_len; |
| 536 | const struct lysc_node *snode; |
| 537 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 538 | if (attr->schema || (meta_container->name.name[0] != '@')) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 539 | /* not an opaq metadata node */ |
| 540 | continue; |
| 541 | } |
| 542 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 543 | LOG_LOCSET(NULL, attr, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 544 | log_location_items++; |
| 545 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 546 | if (prev != meta_container->name.name) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 547 | /* metas' names are stored in dictionary, so checking pointers must works */ |
| 548 | lydict_remove(lydctx->jsonctx->ctx, prev); |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 549 | LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 550 | instance = 1; |
| 551 | } else { |
| 552 | instance++; |
| 553 | } |
| 554 | |
aPiecek | 5057c2e | 2021-05-17 10:28:03 +0200 | [diff] [blame] | 555 | /* find the corresponding data node */ |
| 556 | LY_LIST_FOR(*first_p, node) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 557 | if (!node->schema) { |
| 558 | /* opaq node - we are going to put into it just a generic attribute. */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 559 | if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 560 | continue; |
| 561 | } |
| 562 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 563 | if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 564 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.", |
| 565 | ((struct lyd_node_opaq *)node)->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 566 | ret = LY_EVALID; |
| 567 | goto cleanup; |
| 568 | } |
| 569 | |
| 570 | /* match */ |
| 571 | match++; |
| 572 | if (match != instance) { |
| 573 | continue; |
| 574 | } |
| 575 | |
| 576 | LY_LIST_FOR(meta_container->child, meta_iter) { |
| 577 | /* convert opaq node to a attribute of the opaq node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 578 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 579 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 580 | ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name), |
| 581 | meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 582 | ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 583 | NULL, meta->hints); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 584 | LY_CHECK_GOTO(ret, cleanup); |
| 585 | } |
| 586 | |
| 587 | /* done */ |
| 588 | break; |
| 589 | } else { |
| 590 | /* this is the second time we are resolving the schema node, so it must succeed, |
| 591 | * but remember that snode can be still NULL */ |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 592 | lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len, |
| 593 | &prefix, &prefix_len, &is_attr); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 594 | assert(is_attr); |
| 595 | ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode); |
| 596 | assert(ret == LY_SUCCESS); |
| 597 | |
| 598 | if (snode != node->schema) { |
| 599 | continue; |
| 600 | } |
| 601 | |
| 602 | /* match */ |
| 603 | match++; |
| 604 | if (match != instance) { |
| 605 | continue; |
| 606 | } |
| 607 | |
| 608 | LY_LIST_FOR(meta_container->child, meta_iter) { |
| 609 | /* convert opaq node to a metadata of the node */ |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 610 | struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 611 | struct lys_module *mod = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 612 | |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 613 | mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 614 | if (mod) { |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 615 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, |
Michal Vasko | ad92b67 | 2020-11-12 13:11:31 +0100 | [diff] [blame] | 616 | meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value), |
Michal Vasko | 02cf266 | 2021-07-22 15:36:29 +0200 | [diff] [blame] | 617 | NULL, LY_VALUE_JSON, NULL, meta->hints); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 618 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 619 | } else if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Michal Vasko | 4eab562 | 2021-07-22 15:36:45 +0200 | [diff] [blame] | 620 | if (meta->name.prefix) { |
| 621 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, |
| 622 | "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".", |
| 623 | meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "", |
| 624 | meta->name.name); |
| 625 | } else { |
| 626 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".", |
| 627 | meta->name.name); |
| 628 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 629 | ret = LY_EVALID; |
| 630 | goto cleanup; |
| 631 | } |
| 632 | } |
| 633 | /* add/correct flags */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 634 | lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &node->meta, lydctx->parse_opts); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 635 | |
| 636 | /* done */ |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | if (match != instance) { |
| 642 | /* there is no corresponding data node for the metadata */ |
| 643 | if (instance > 1) { |
Michal Vasko | 54ba891 | 2021-07-23 12:46:23 +0200 | [diff] [blame] | 644 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.", |
| 645 | instance, meta_container->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 646 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 647 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.", |
| 648 | meta_container->name.name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 649 | } |
| 650 | ret = LY_EVALID; |
| 651 | } else { |
| 652 | /* remove the opaq attr */ |
| 653 | if (attr == (*first_p)) { |
aPiecek | 5057c2e | 2021-05-17 10:28:03 +0200 | [diff] [blame] | 654 | *first_p = attr->next; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 655 | } |
| 656 | lyd_free_tree(attr); |
| 657 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 658 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 659 | LOG_LOCBACK(0, log_location_items, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 660 | log_location_items = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | cleanup: |
| 664 | lydict_remove(lydctx->jsonctx->ctx, prev); |
| 665 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 666 | LOG_LOCBACK(0, log_location_items, 0, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * @brief Parse a metadata member. |
| 672 | * |
| 673 | * @param[in] lydctx JSON data parser context. |
| 674 | * @param[in] snode Schema node of the metadata parent. |
| 675 | * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM) |
| 676 | * so the data node does not exists. In such a case the metadata is stored in the context for the later |
| 677 | * processing by lydjson_metadata_finish(). |
| 678 | * @return LY_SUCCESS on success |
| 679 | * @return Various LY_ERR values in case of failure. |
| 680 | */ |
| 681 | static LY_ERR |
| 682 | lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node) |
| 683 | { |
| 684 | LY_ERR ret = LY_SUCCESS; |
| 685 | enum LYJSON_PARSER_STATUS status; |
| 686 | const char *expected; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 687 | ly_bool in_parent = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 688 | const char *name, *prefix = NULL; |
aPiecek | 303fb25 | 2021-06-16 11:54:26 +0200 | [diff] [blame] | 689 | char *dynamic_prefname = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 690 | size_t name_len, prefix_len = 0; |
| 691 | struct lys_module *mod; |
| 692 | struct lyd_meta *meta = NULL; |
| 693 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 694 | ly_bool is_attr = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 695 | struct lyd_node *prev = node; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 696 | uint32_t instance = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 697 | uint16_t nodetype; |
| 698 | |
| 699 | assert(snode || node); |
| 700 | |
| 701 | nodetype = snode ? snode->nodetype : LYS_CONTAINER; |
| 702 | |
| 703 | /* move to the second item in the name/X pair */ |
| 704 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 705 | LY_CHECK_GOTO(ret, cleanup); |
| 706 | |
| 707 | /* check attribute encoding */ |
| 708 | switch (nodetype) { |
| 709 | case LYS_LEAFLIST: |
| 710 | expected = "@name/array of objects/nulls"; |
| 711 | |
| 712 | LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); |
| 713 | |
| 714 | next_entry: |
| 715 | instance++; |
| 716 | |
| 717 | /* move into array / next entry */ |
| 718 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 719 | LY_CHECK_GOTO(ret, cleanup); |
| 720 | |
| 721 | if (status == LYJSON_ARRAY_CLOSED) { |
| 722 | /* we are done, move after the array */ |
| 723 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 724 | goto cleanup; |
| 725 | } |
| 726 | LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error); |
| 727 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 728 | if (!node || (node->schema != prev->schema)) { |
Michal Vasko | 54ba891 | 2021-07-23 12:46:23 +0200 | [diff] [blame] | 729 | LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%u of %s:%s to be coupled with metadata.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 730 | instance, prev->schema->module->name, prev->schema->name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 731 | ret = LY_EVALID; |
| 732 | goto cleanup; |
| 733 | } |
| 734 | |
| 735 | if (status == LYJSON_NULL) { |
| 736 | /* continue with the next entry in the leaf-list array */ |
| 737 | prev = node; |
| 738 | node = node->next; |
| 739 | goto next_entry; |
| 740 | } |
| 741 | break; |
| 742 | case LYS_LEAF: |
| 743 | case LYS_ANYXML: |
| 744 | expected = "@name/object"; |
| 745 | |
aPiecek | 5b115fc | 2021-05-24 14:32:42 +0200 | [diff] [blame] | 746 | LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 747 | break; |
| 748 | case LYS_CONTAINER: |
| 749 | case LYS_LIST: |
| 750 | case LYS_ANYDATA: |
| 751 | case LYS_NOTIF: |
| 752 | case LYS_ACTION: |
| 753 | case LYS_RPC: |
| 754 | in_parent = 1; |
| 755 | expected = "@/object"; |
| 756 | LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error); |
| 757 | break; |
Radek Krejci | 8f5fad2 | 2020-09-15 16:50:54 +0200 | [diff] [blame] | 758 | default: |
| 759 | LOGINT_RET(ctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /* process all the members inside a single metadata object */ |
| 763 | assert(status == LYJSON_OBJECT); |
| 764 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 765 | LOG_LOCSET(snode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 766 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 767 | while (status != LYJSON_OBJECT_CLOSED) { |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 768 | lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr); |
| 769 | lyjson_ctx_submit_dynamic_value(lydctx->jsonctx, &dynamic_prefname); |
aPiecek | 303fb25 | 2021-06-16 11:54:26 +0200 | [diff] [blame] | 770 | |
Michal Vasko | 69d0ca1 | 2021-07-23 10:22:03 +0200 | [diff] [blame] | 771 | if (!name_len) { |
| 772 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name); |
| 773 | ret = LY_EVALID; |
| 774 | goto cleanup; |
| 775 | } else if (!prefix_len) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 776 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 777 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 778 | ret = LY_EVALID; |
| 779 | goto cleanup; |
| 780 | } else if (is_attr) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 781 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 782 | (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 783 | ret = LY_EVALID; |
| 784 | goto cleanup; |
| 785 | } |
| 786 | |
| 787 | /* get the element module */ |
| 788 | mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len); |
| 789 | if (!mod) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 790 | if (lydctx->parse_opts & LYD_PARSE_STRICT) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 791 | LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 792 | (int)prefix_len, prefix, (int)name_len, name); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 793 | ret = LY_EVALID; |
| 794 | goto cleanup; |
| 795 | } |
Michal Vasko | 8b83209 | 2022-01-17 14:08:30 +0100 | [diff] [blame] | 796 | if (node->schema) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 797 | /* skip element with children */ |
| 798 | ret = lydjson_data_skip(lydctx->jsonctx); |
| 799 | LY_CHECK_GOTO(ret, cleanup); |
| 800 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 801 | /* end of the item */ |
| 802 | continue; |
| 803 | } |
Michal Vasko | 8b83209 | 2022-01-17 14:08:30 +0100 | [diff] [blame] | 804 | assert(lydctx->parse_opts & LYD_PARSE_OPAQ); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* get the value */ |
| 808 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 809 | LY_CHECK_GOTO(ret, cleanup); |
| 810 | |
| 811 | if (node->schema) { |
| 812 | /* create metadata */ |
| 813 | meta = NULL; |
Michal Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 814 | ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 815 | lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 816 | LYD_HINT_DATA); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 817 | LY_CHECK_GOTO(ret, cleanup); |
| 818 | |
| 819 | /* add/correct flags */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 820 | lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 821 | } else { |
| 822 | /* create attribute */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 823 | const char *module_name; |
| 824 | size_t module_name_len; |
| 825 | |
| 826 | lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len); |
| 827 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 828 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 829 | ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, |
| 830 | module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 831 | LY_VALUE_JSON, NULL, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 832 | LY_CHECK_GOTO(ret, cleanup); |
| 833 | } |
| 834 | /* next member */ |
| 835 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 836 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | de3c692 | 2021-05-24 14:42:10 +0200 | [diff] [blame] | 837 | LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | if (nodetype == LYS_LEAFLIST) { |
| 841 | /* continue by processing another metadata object for the following |
| 842 | * leaf-list instance since they are allways instantiated in JSON array */ |
| 843 | prev = node; |
| 844 | node = node->next; |
| 845 | goto next_entry; |
| 846 | } |
| 847 | |
| 848 | /* move after the metadata */ |
| 849 | ret = lyjson_ctx_next(lydctx->jsonctx, NULL); |
| 850 | LY_CHECK_GOTO(ret, cleanup); |
| 851 | |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 852 | /* success */ |
| 853 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 854 | |
| 855 | representation_error: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 856 | LOGVAL(ctx, LYVE_SYNTAX_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 857 | "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.", |
Michal Vasko | f9bed93 | 2022-01-24 07:47:49 +0100 | [diff] [blame] | 858 | lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status), |
| 859 | in_parent ? "" : "name"); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 860 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 861 | ret = LY_EVALID; |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 862 | |
| 863 | cleanup: |
aPiecek | 303fb25 | 2021-06-16 11:54:26 +0200 | [diff] [blame] | 864 | free(dynamic_prefname); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 865 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 866 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /** |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 870 | * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing |
| 871 | * to the first child node. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 872 | * |
| 873 | * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p). |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 874 | * @param[in,out] first_p Pointer to the first sibling node in case of top-level. |
| 875 | * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL. |
| 876 | * @param[in] last If set, always insert at the end. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 877 | */ |
| 878 | static void |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 879 | lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 880 | { |
| 881 | if (*node_p) { |
| 882 | /* insert, keep first pointer correct */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 883 | lyd_insert_node(parent ? &parent->node : NULL, first_p, *node_p, last); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 884 | if (first_p) { |
| 885 | if (parent) { |
| 886 | *first_p = parent->child; |
| 887 | } else { |
| 888 | while ((*first_p)->prev->next) { |
| 889 | *first_p = (*first_p)->prev; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | *node_p = NULL; |
| 894 | } |
| 895 | } |
| 896 | |
aPiecek | 4bf4721 | 2021-05-27 10:55:47 +0200 | [diff] [blame] | 897 | /** |
| 898 | * @brief Wrapper for ::lyd_create_opaq(). |
| 899 | * |
| 900 | * @param[in] lydctx JSON data parser context. |
| 901 | * @param[in] name Name of the opaq node to create. |
| 902 | * @param[in] name_len Length of the @p name string. |
| 903 | * @param[in] prefix Prefix of the opaq node to create. |
| 904 | * @param[in] prefix_len Length of the @p prefx string. |
| 905 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 906 | * but must be set if @p first is not. |
| 907 | * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone |
| 908 | * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. |
| 909 | * @param[out] node_p Pointer to the created opaq node. |
| 910 | * @return LY_ERR value. |
| 911 | */ |
| 912 | static LY_ERR |
| 913 | lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, |
| 914 | struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p) |
| 915 | { |
| 916 | LY_ERR ret = LY_SUCCESS; |
| 917 | const char *value = NULL, *module_name; |
| 918 | size_t value_len = 0, module_name_len = 0; |
| 919 | ly_bool dynamic = 0; |
| 920 | uint32_t type_hint = 0; |
| 921 | |
| 922 | if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) { |
| 923 | /* prepare for creating opaq node with a value */ |
| 924 | value = lydctx->jsonctx->value; |
| 925 | value_len = lydctx->jsonctx->value_len; |
| 926 | dynamic = lydctx->jsonctx->dynamic; |
| 927 | lydctx->jsonctx->dynamic = 0; |
| 928 | |
| 929 | LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint)); |
| 930 | } |
| 931 | |
| 932 | /* create node */ |
Michal Vasko | d8e774e | 2021-06-15 14:58:07 +0200 | [diff] [blame] | 933 | lydjson_get_node_prefix(parent ? &parent->node : NULL, prefix, prefix_len, &module_name, &module_name_len); |
aPiecek | 4bf4721 | 2021-05-27 10:55:47 +0200 | [diff] [blame] | 934 | ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value, |
| 935 | value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p); |
| 936 | if (dynamic) { |
| 937 | free((char *)value); |
| 938 | } |
| 939 | |
| 940 | return ret; |
| 941 | } |
| 942 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 943 | static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 944 | struct ly_set *parsed); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 945 | |
| 946 | /** |
| 947 | * @brief Parse opaq node from the input. |
| 948 | * |
| 949 | * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array. |
| 950 | * |
| 951 | * @param[in] lydctx JSON data parser context. |
| 952 | * @param[in] name Name of the opaq node to create. |
| 953 | * @param[in] name_len Length of the @p name string. |
| 954 | * @param[in] prefix Prefix of the opaq node to create. |
| 955 | * @param[in] prefix_len Length of the @p prefx string. |
| 956 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 957 | * but must be set if @p first is not. |
| 958 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 959 | * since the function manipulates with the context and process the input, the status can be updated. |
| 960 | * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone |
| 961 | * context status of the array content. Otherwise, it is supposed to be the same as @p status_p. |
| 962 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 963 | * @param[out] node_p Pointer to the created opaq node. |
| 964 | * @return LY_ERR value. |
| 965 | */ |
| 966 | static LY_ERR |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 967 | lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 968 | struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, |
| 969 | enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 970 | { |
| 971 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 972 | |
aPiecek | 4bf4721 | 2021-05-27 10:55:47 +0200 | [diff] [blame] | 973 | ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p); |
Radek Krejci | d46e46a | 2020-09-15 14:22:42 +0200 | [diff] [blame] | 974 | LY_CHECK_RET(ret); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 975 | |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 976 | if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) { |
| 977 | /* special array null value */ |
| 978 | ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY; |
| 979 | |
| 980 | /* must be the only item */ |
| 981 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p)); |
| 982 | if (*status_inner_p != LYJSON_ARRAY_CLOSED) { |
| 983 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member."); |
| 984 | return LY_EVALID; |
| 985 | } |
| 986 | |
| 987 | goto finish; |
| 988 | } |
| 989 | |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 990 | while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 991 | /* process another instance of the same node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 992 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 993 | if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) { |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 994 | /* array with objects, list */ |
| 995 | ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST; |
| 996 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 997 | /* but first process children of the object in the array */ |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 998 | while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 999 | LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1000 | *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1001 | } |
Michal Vasko | 1a85d33 | 2021-08-27 10:35:28 +0200 | [diff] [blame] | 1002 | } else { |
| 1003 | /* array with values, leaf-list */ |
| 1004 | ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p)); |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1008 | if (*status_inner_p == LYJSON_ARRAY_CLOSED) { |
| 1009 | goto finish; |
| 1010 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1011 | |
| 1012 | /* continue with the next instance */ |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1013 | assert(node_p); |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1014 | lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0); |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1015 | ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p); |
| 1016 | LY_CHECK_RET(ret); |
| 1017 | } |
| 1018 | |
| 1019 | if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) { |
| 1020 | /* process children */ |
| 1021 | while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) { |
| 1022 | LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL)); |
| 1023 | *status_p = lyjson_ctx_status(lydctx->jsonctx, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
aPiecek | dbb8baf | 2021-05-27 11:01:51 +0200 | [diff] [blame] | 1027 | finish: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1028 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1029 | LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p))); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1030 | |
| 1031 | /* move after the item */ |
| 1032 | return lyjson_ctx_next(lydctx->jsonctx, status_p); |
| 1033 | } |
| 1034 | |
| 1035 | /** |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1036 | * @brief Move to the second item in the name/X pair and parse opaq node from the input. |
| 1037 | * |
| 1038 | * This function is basically the wrapper of the ::lydjson_parse_opaq(). |
| 1039 | * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter |
| 1040 | * for ::lydjson_parse_opaq(). |
| 1041 | * |
| 1042 | * @param[in] lydctx JSON data parser context. |
| 1043 | * @param[in] name Name of the opaq node to create. |
| 1044 | * @param[in] name_len Length of the @p name string. |
| 1045 | * @param[in] prefix Prefix of the opaq node to create. |
| 1046 | * @param[in] prefix_len Length of the @p prefx string. |
| 1047 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 1048 | * but must be set if @p first is not. |
| 1049 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 1050 | * since the function manipulates with the context and process the input, the status can be updated. |
| 1051 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 1052 | * @param[out] node_p Pointer to the created opaq node. |
| 1053 | * @return LY_ERR value. |
| 1054 | */ |
| 1055 | static LY_ERR |
| 1056 | lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, |
| 1057 | const char *prefix, size_t prefix_len, struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p, |
| 1058 | struct lyd_node **first_p, struct lyd_node **node_p) |
| 1059 | { |
| 1060 | enum LYJSON_PARSER_STATUS status_inner = 0; |
| 1061 | |
| 1062 | /* move to the second item in the name/X pair */ |
| 1063 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p)); |
| 1064 | |
| 1065 | if (*status_p == LYJSON_ARRAY) { |
| 1066 | /* move into the array */ |
| 1067 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner)); |
| 1068 | } else { |
| 1069 | /* just a flag to pass correct parameters into lydjson_parse_opaq() */ |
| 1070 | status_inner = LYJSON_ERROR; |
| 1071 | } |
| 1072 | |
| 1073 | if (status_inner == LYJSON_ERROR) { |
| 1074 | status_inner = *status_p; |
| 1075 | } |
| 1076 | |
| 1077 | /* parse opaq node from the input */ |
| 1078 | LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner, first_p, node_p)); |
| 1079 | |
| 1080 | return LY_SUCCESS; |
| 1081 | } |
| 1082 | |
| 1083 | /** |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1084 | * @brief Process the attribute container (starting by @) |
| 1085 | * |
| 1086 | * @param[in] lydctx JSON data parser context. |
| 1087 | * @param[in] attr_node The data node referenced by the attribute container, if already known. |
| 1088 | * @param[in] snode The schema node of the data node referenced by the attribute container, if known. |
| 1089 | * @param[in] name Name of the opaq node to create. |
| 1090 | * @param[in] name_len Length of the @p name string. |
| 1091 | * @param[in] prefix Prefix of the opaq node to create. |
| 1092 | * @param[in] prefix_len Length of the @p prefx string. |
| 1093 | * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level, |
| 1094 | * but must be set if @p first is not. |
| 1095 | * @param[in,out] status_p Pointer to the current status of the parser context, |
| 1096 | * since the function manipulates with the context and process the input, the status can be updated. |
| 1097 | * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not. |
| 1098 | * @param[out] node_p Pointer to the created opaq node. |
| 1099 | * @return LY_ERR value. |
| 1100 | */ |
| 1101 | static LY_ERR |
| 1102 | lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode, |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1103 | const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent, |
| 1104 | enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1105 | { |
| 1106 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1107 | |
| 1108 | /* parse as an attribute to a node */ |
| 1109 | if (!attr_node && snode) { |
| 1110 | /* try to find the instance */ |
| 1111 | for (struct lyd_node *iter = *first_p; iter; iter = iter->next) { |
| 1112 | if (iter->schema == snode) { |
| 1113 | attr_node = iter; |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | if (!attr_node) { |
| 1119 | /* parse just as an opaq node with the name beginning with @, |
| 1120 | * later we have to check that it belongs to a standard node |
| 1121 | * and it is supposed to be converted to a metadata */ |
| 1122 | uint32_t prev_opts; |
| 1123 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1124 | /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1125 | prev_opts = lydctx->parse_opts; |
| 1126 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 1127 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1128 | |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1129 | ret = lydjson_ctx_next_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1, |
| 1130 | NULL, 0, (struct lyd_node_inner *)parent, status_p, first_p, node_p); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1131 | |
| 1132 | /* restore the parser options */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1133 | lydctx->parse_opts = prev_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1134 | } else { |
| 1135 | ret = lydjson_metadata(lydctx, snode, attr_node); |
| 1136 | } |
| 1137 | |
| 1138 | return ret; |
| 1139 | } |
| 1140 | |
| 1141 | /** |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1142 | * @brief Parse a single instance of a node. |
| 1143 | * |
| 1144 | * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state |
| 1145 | * as before calling, despite it is necessary to process input data for checking. |
| 1146 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 1147 | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
| 1148 | * @param[in] snode Schema node corresponding to the member currently being processed in the context. |
| 1149 | * @param[in] name Parsed JSON node name. |
| 1150 | * @param[in] name_len Lenght of @p name. |
| 1151 | * @param[in] prefix Parsed JSON node prefix. |
| 1152 | * @param[in] prefix_len Length of @p prefix. |
| 1153 | * @param[in,out] status JSON parser status, is updated. |
| 1154 | * @param[out] node Parsed data (or opaque) node. |
| 1155 | * @return LY_SUCCESS if a node was successfully parsed, |
| 1156 | * @return LY_EINVAL in case of invalid JSON encoding, |
| 1157 | * @return LY_ERR on other errors. |
| 1158 | */ |
| 1159 | static LY_ERR |
| 1160 | lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1161 | const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len, |
| 1162 | enum LYJSON_PARSER_STATUS *status, struct lyd_node **node) |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1163 | { |
| 1164 | LY_ERR ret; |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1165 | uint32_t type_hints = 0, prev_parse_opts, prev_int_opts; |
| 1166 | char *val; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1167 | struct lyd_node *tree = NULL; |
| 1168 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1169 | ret = lydjson_data_check_opaq(lydctx, snode, &type_hints); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1170 | if (ret == LY_SUCCESS) { |
| 1171 | assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY)); |
| 1172 | if (snode->nodetype & LYD_NODE_TERM) { |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1173 | LY_CHECK_RET((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && |
| 1174 | (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_EINVAL); |
| 1175 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1176 | /* create terminal node */ |
| 1177 | ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1178 | lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1179 | type_hints, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1180 | LY_CHECK_RET(ret); |
| 1181 | |
| 1182 | /* move JSON parser */ |
Michal Vasko | 8543070 | 2021-07-21 14:29:56 +0200 | [diff] [blame] | 1183 | if (*status == LYJSON_ARRAY) { |
| 1184 | /* only [null], 2 more moves are needed */ |
| 1185 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
| 1186 | assert(*status == LYJSON_NULL); |
| 1187 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
| 1188 | assert(*status == LYJSON_ARRAY_CLOSED); |
| 1189 | } |
| 1190 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1191 | } else if (snode->nodetype == LYS_ANYXML) { |
| 1192 | LY_CHECK_RET((*status != LYJSON_STRING) && (*status != LYJSON_NULL), LY_EINVAL); |
| 1193 | |
| 1194 | /* create anyxml node */ |
| 1195 | if (*status == LYJSON_NULL) { |
| 1196 | ret = lyd_create_any(snode, NULL, LYD_ANYDATA_STRING, 1, node); |
| 1197 | LY_CHECK_RET(ret); |
| 1198 | } else if (lydctx->jsonctx->dynamic) { |
| 1199 | ret = lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node); |
| 1200 | LY_CHECK_RET(ret); |
| 1201 | lydctx->jsonctx->dynamic = 0; |
| 1202 | } else { |
| 1203 | val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len); |
| 1204 | LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM); |
| 1205 | ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node); |
| 1206 | if (ret) { |
| 1207 | free(val); |
| 1208 | return ret; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | /* move JSON parser */ |
| 1213 | LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status)); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1214 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 1215 | /* create inner node */ |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1216 | LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_EINVAL); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1217 | |
| 1218 | ret = lyd_create_inner(snode, node); |
| 1219 | LY_CHECK_RET(ret); |
| 1220 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1221 | LOG_LOCSET(snode, *node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1222 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1223 | /* process children */ |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1224 | while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1225 | ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1226 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1227 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1228 | } |
| 1229 | |
| 1230 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1231 | ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node)); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1232 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1233 | |
| 1234 | if (snode->nodetype == LYS_LIST) { |
| 1235 | /* check all keys exist */ |
| 1236 | ret = lyd_parse_check_keys(*node); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1237 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1240 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1241 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1242 | ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1243 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1244 | |
| 1245 | /* add any missing default children */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 1246 | ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts, |
| 1247 | &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1248 | LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1251 | LOG_LOCBACK(1, 1, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1252 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1253 | /* move JSON parser */ |
| 1254 | ret = lyjson_ctx_next(lydctx->jsonctx, status); |
| 1255 | LY_CHECK_RET(ret); |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1256 | } else if (snode->nodetype == LYS_ANYDATA) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1257 | /* create any node */ |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1258 | LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_EINVAL); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1259 | |
| 1260 | /* parse any data tree with correct options */ |
| 1261 | /* first backup the current options and then make the parser to process data as opaq nodes */ |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1262 | prev_parse_opts = lydctx->parse_opts; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1263 | lydctx->parse_opts &= ~LYD_PARSE_STRICT; |
| 1264 | lydctx->parse_opts |= LYD_PARSE_OPAQ; |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1265 | prev_int_opts = lydctx->int_opts; |
| 1266 | lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1267 | |
| 1268 | /* process the anydata content */ |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1269 | while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1270 | ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1271 | LY_CHECK_RET(ret); |
| 1272 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1273 | } |
| 1274 | |
| 1275 | /* restore parser options */ |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1276 | lydctx->parse_opts = prev_parse_opts; |
| 1277 | lydctx->int_opts = prev_int_opts; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1278 | |
| 1279 | /* finish linking metadata */ |
| 1280 | ret = lydjson_metadata_finish(lydctx, &tree); |
| 1281 | LY_CHECK_RET(ret); |
| 1282 | |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 1283 | ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1284 | LY_CHECK_RET(ret); |
| 1285 | } |
Michal Vasko | caf608d | 2021-07-23 13:51:23 +0200 | [diff] [blame] | 1286 | |
| 1287 | /* add/correct flags */ |
| 1288 | lyd_parse_set_data_flags(*node, &lydctx->node_when, &lydctx->node_exts, &(*node)->meta, lydctx->parse_opts); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1289 | } else if (ret == LY_ENOT) { |
| 1290 | /* parse it again as an opaq node */ |
| 1291 | ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1292 | status, status, first_p, node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1293 | LY_CHECK_RET(ret); |
| 1294 | |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1295 | if (snode->nodetype == LYS_LIST) { |
| 1296 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST; |
| 1297 | } else if (snode->nodetype == LYS_LEAFLIST) { |
| 1298 | ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 1306 | * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1307 | * |
| 1308 | * @param[in] lydctx JSON data parser context. |
| 1309 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 1310 | * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1311 | * @param[in,out] parsed Optional set to add all the parsed siblings into. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1312 | * @return LY_ERR value. |
| 1313 | */ |
| 1314 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1315 | lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1316 | { |
| 1317 | LY_ERR ret = LY_SUCCESS; |
| 1318 | enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0); |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 1319 | const char *name, *prefix = NULL, *expected = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1320 | size_t name_len, prefix_len = 0; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1321 | ly_bool is_meta = 0; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1322 | const struct lysc_node *snode = NULL; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1323 | struct lyd_node *node = NULL, *attr_node = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1324 | const struct ly_ctx *ctx = lydctx->jsonctx->ctx; |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 1325 | char *value = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1326 | |
| 1327 | assert(parent || first_p); |
| 1328 | assert(status == LYJSON_OBJECT); |
| 1329 | |
| 1330 | /* process the node name */ |
aPiecek | d2c3937 | 2021-06-16 14:03:12 +0200 | [diff] [blame] | 1331 | lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta); |
| 1332 | lyjson_ctx_submit_dynamic_value(lydctx->jsonctx, &value); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1333 | |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1334 | if (!is_meta || name_len || prefix_len) { |
| 1335 | /* get the schema node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1336 | ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, (struct lyd_node_inner *)parent, &snode); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1337 | if (ret == LY_ENOT) { |
| 1338 | /* skip element with children */ |
| 1339 | ret = lydjson_data_skip(lydctx->jsonctx); |
| 1340 | LY_CHECK_GOTO(ret, cleanup); |
| 1341 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
| 1342 | /* nothing for now, continue with another call of lydjson_subtree_r() */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1343 | goto cleanup; |
| 1344 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1345 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1346 | |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1347 | if (!snode) { |
| 1348 | /* we will not be parsing it as metadata */ |
| 1349 | is_meta = 0; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if (is_meta) { |
| 1354 | /* parse as metadata */ |
| 1355 | if (!name_len && !prefix_len) { |
| 1356 | /* parent's metadata without a name - use the schema from the parent */ |
| 1357 | if (!parent) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1358 | LOGVAL(ctx, LYVE_SYNTAX_JSON, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1359 | "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries."); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1360 | ret = LY_EVALID; |
| 1361 | goto cleanup; |
| 1362 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1363 | attr_node = parent; |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1364 | snode = attr_node->schema; |
| 1365 | } |
| 1366 | ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1367 | first_p, &node); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1368 | LY_CHECK_GOTO(ret, cleanup); |
| 1369 | } else if (!snode) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1370 | /* parse as an opaq node */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1371 | assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1372 | |
aPiecek | b7b29e6 | 2021-05-11 10:02:43 +0200 | [diff] [blame] | 1373 | /* opaq node cannot have an empty string as the name. */ |
| 1374 | if (name_len == 0) { |
| 1375 | LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string."); |
| 1376 | ret = LY_EVALID; |
| 1377 | goto cleanup; |
| 1378 | } |
| 1379 | |
aPiecek | 6cee5d0 | 2021-05-12 10:32:00 +0200 | [diff] [blame] | 1380 | /* move to the second item in the name/X pair and parse opaq */ |
| 1381 | ret = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, |
| 1382 | (struct lyd_node_inner *)parent, &status, first_p, &node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1383 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | cabe070 | 2020-08-12 10:14:36 +0200 | [diff] [blame] | 1384 | } else { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1385 | /* parse as a standard lyd_node but it can still turn out to be an opaque node */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1386 | |
| 1387 | /* move to the second item in the name/X pair */ |
| 1388 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 1389 | LY_CHECK_GOTO(ret, cleanup); |
| 1390 | |
| 1391 | /* first check the expected representation according to the nodetype and then continue with the content */ |
| 1392 | switch (snode->nodetype) { |
| 1393 | case LYS_LEAFLIST: |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1394 | case LYS_LIST: |
| 1395 | if (snode->nodetype == LYS_LEAFLIST) { |
| 1396 | expected = "name/array of values"; |
| 1397 | } else { |
| 1398 | expected = "name/array of objects"; |
| 1399 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1400 | |
| 1401 | LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error); |
| 1402 | |
| 1403 | /* move into array */ |
| 1404 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 1405 | LY_CHECK_GOTO(ret, cleanup); |
| 1406 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1407 | /* process all the values/objects */ |
| 1408 | do { |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1409 | lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node, |
| 1410 | lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1411 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1412 | ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len, |
| 1413 | prefix, prefix_len, &status, &node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1414 | if (ret == LY_EINVAL) { |
| 1415 | goto representation_error; |
| 1416 | } else if (ret) { |
| 1417 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1418 | } |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1419 | } while (status != LYJSON_ARRAY_CLOSED); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1420 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1421 | /* move after the array */ |
| 1422 | ret = lyjson_ctx_next(lydctx->jsonctx, &status); |
| 1423 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1424 | |
| 1425 | break; |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1426 | case LYS_LEAF: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1427 | case LYS_CONTAINER: |
| 1428 | case LYS_NOTIF: |
| 1429 | case LYS_ACTION: |
| 1430 | case LYS_RPC: |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1431 | case LYS_ANYDATA: |
| 1432 | case LYS_ANYXML: |
Michal Vasko | ca0b23b | 2022-01-06 11:40:10 +0100 | [diff] [blame] | 1433 | if (snode->nodetype & (LYS_LEAF | LYS_ANYXML)) { |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1434 | if (status == LYJSON_ARRAY) { |
| 1435 | expected = "name/[null]"; |
| 1436 | } else { |
| 1437 | expected = "name/value"; |
| 1438 | } |
| 1439 | } else { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1440 | expected = "name/object"; |
| 1441 | } |
| 1442 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1443 | /* process the value/object */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1444 | ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len, |
| 1445 | prefix, prefix_len, &status, &node); |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1446 | if (ret == LY_EINVAL) { |
| 1447 | goto representation_error; |
| 1448 | } else if (ret) { |
| 1449 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1452 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1453 | /* rememeber the RPC/action/notification */ |
| 1454 | lydctx->op_node = node; |
| 1455 | } |
| 1456 | |
| 1457 | break; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1458 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
Michal Vasko | 32ac994 | 2020-08-12 14:35:12 +0200 | [diff] [blame] | 1461 | /* finally connect the parsed node */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 1462 | lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node, |
| 1463 | lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1464 | |
| 1465 | /* rememeber a successfully parsed node */ |
| 1466 | if (parsed) { |
| 1467 | ly_set_add(parsed, node, 1, NULL); |
| 1468 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1469 | |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1470 | /* success */ |
| 1471 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1472 | |
| 1473 | representation_error: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1474 | LOG_LOCSET(NULL, parent, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1475 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1476 | lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status)); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1477 | LOG_LOCBACK(0, parent ? 1 : 0, 0, 0); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1478 | ret = LY_EVALID; |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1479 | |
| 1480 | cleanup: |
aPiecek | 49e2a3a | 2021-05-13 10:36:46 +0200 | [diff] [blame] | 1481 | free(value); |
Radek Krejci | 5813c36 | 2021-01-05 10:51:57 +0100 | [diff] [blame] | 1482 | lyd_free_tree(node); |
| 1483 | return ret; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * @brief Common start of JSON parser processing different types of the input data. |
| 1488 | * |
| 1489 | * @param[in] ctx libyang context |
| 1490 | * @param[in] in Input structure. |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1491 | * @param[in] parse_opts Options for parser, see @ref dataparseroptions. |
| 1492 | * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1493 | * @param[out] lydctx_p Data parser context to finish validation. |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1494 | * @param[out] status Storage for the current context's status |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1495 | * @return LY_ERR value. |
| 1496 | */ |
| 1497 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1498 | lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1499 | struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1500 | { |
| 1501 | LY_ERR ret = LY_SUCCESS; |
| 1502 | struct lyd_json_ctx *lydctx; |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1503 | size_t i; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1504 | |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1505 | assert(lydctx_p); |
| 1506 | assert(status); |
| 1507 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1508 | /* init context */ |
| 1509 | lydctx = calloc(1, sizeof *lydctx); |
| 1510 | LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1511 | lydctx->parse_opts = parse_opts; |
| 1512 | lydctx->val_opts = val_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1513 | lydctx->free = lyd_json_ctx_free; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1514 | |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1515 | /* starting top-level */ |
| 1516 | for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1517 | if (in->current[i] == '\n') { |
| 1518 | /* new line */ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 1519 | LY_IN_NEW_LINE(in); |
Michal Vasko | 61d7636 | 2020-10-07 10:47:30 +0200 | [diff] [blame] | 1520 | } |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1521 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1522 | |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1523 | LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret); |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1524 | *status = lyjson_ctx_status(lydctx->jsonctx, 0); |
Radek Krejci | bb27df3 | 2020-11-13 15:39:16 +0100 | [diff] [blame] | 1525 | if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) { |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1526 | *lydctx_p = lydctx; |
| 1527 | return LY_SUCCESS; |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1528 | } else { |
| 1529 | /* expecting top-level object */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1530 | LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1531 | lyjson_token2str(*status)); |
| 1532 | *lydctx_p = NULL; |
Michal Vasko | 9350901 | 2020-11-13 10:26:09 +0100 | [diff] [blame] | 1533 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1534 | return LY_EVALID; |
Radek Krejci | 284f31f | 2020-09-18 15:40:29 +0200 | [diff] [blame] | 1535 | } |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | LY_ERR |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1539 | lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1540 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
| 1541 | struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1542 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1543 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1544 | struct lyd_json_ctx *lydctx = NULL; |
| 1545 | enum LYJSON_PARSER_STATUS status; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1546 | uint32_t int_opts; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1547 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1548 | rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status); |
| 1549 | LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1550 | |
Radek Krejci | cd5f622 | 2020-11-12 08:54:13 +0100 | [diff] [blame] | 1551 | assert(status == LYJSON_OBJECT); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1552 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1553 | switch (data_type) { |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1554 | case LYD_TYPE_DATA_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1555 | int_opts = LYD_INTOPT_WITH_SIBLINGS; |
| 1556 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1557 | case LYD_TYPE_RPC_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1558 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS; |
| 1559 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1560 | case LYD_TYPE_NOTIF_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1561 | int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS; |
| 1562 | break; |
Michal Vasko | 1e4c68e | 2021-02-18 15:03:01 +0100 | [diff] [blame] | 1563 | case LYD_TYPE_REPLY_YANG: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1564 | int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS; |
| 1565 | break; |
| 1566 | default: |
| 1567 | LOGINT(ctx); |
| 1568 | rc = LY_EINT; |
| 1569 | goto cleanup; |
| 1570 | } |
| 1571 | lydctx->int_opts = int_opts; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1572 | lydctx->ext = ext; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1573 | |
| 1574 | /* find the operation node if it exists already */ |
| 1575 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup); |
| 1576 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1577 | /* read subtree(s) */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1578 | while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) { |
| 1579 | rc = lydjson_subtree_r(lydctx, parent, first_p, parsed); |
| 1580 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1581 | |
| 1582 | status = lyjson_ctx_status(lydctx->jsonctx, 0); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1583 | |
| 1584 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && |
| 1590 | (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) { |
| 1591 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1592 | rc = LY_EVALID; |
| 1593 | goto cleanup; |
| 1594 | } |
| 1595 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) { |
| 1596 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1597 | rc = LY_EVALID; |
| 1598 | goto cleanup; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /* finish linking metadata */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1602 | rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p); |
| 1603 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1604 | |
| 1605 | cleanup: |
| 1606 | /* there should be no unresolved types stored */ |
Michal Vasko | 40b8059 | 2021-12-09 09:25:33 +0100 | [diff] [blame] | 1607 | assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count && |
Michal Vasko | 49c39d8 | 2020-11-06 17:20:27 +0100 | [diff] [blame] | 1608 | !lydctx->node_when.count)); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1609 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1610 | if (rc) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1611 | lyd_json_ctx_free((struct lyd_ctx *)lydctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1612 | } else { |
| 1613 | *lydctx_p = (struct lyd_ctx *)lydctx; |
| 1614 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1615 | return rc; |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1616 | } |