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