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