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