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