blob: abeb12044bdc20fb52d2c1c12a136efbed0040d7 [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko61ad1ff2022-02-10 15:48:39 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01005 * @brief JSON data parser for libyang
6 *
Michal Vasko09e04632023-03-22 14:34:10 +01007 * Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01008 *
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 Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010017
Radek Krejci1798aae2020-07-14 13:26:06 +020018#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010020#include <stdlib.h>
21#include <string.h>
22
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vaskobbdadda2022-01-06 11:40:10 +010024#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010025#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020028#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010030#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020031#include "parser_internal.h"
Michal Vasko61ad1ff2022-02-10 15:48:39 +010032#include "plugins_exts.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "set.h"
34#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020035#include "tree_data.h"
36#include "tree_data_internal.h"
37#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010038#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020039#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010040
41/**
Radek Krejci1798aae2020-07-14 13:26:06 +020042 * @brief Free the JSON data parser context.
43 *
44 * JSON implementation of lyd_ctx_free_clb().
45 */
46static void
47lyd_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 Vasko61ad1ff2022-02-10 15:48:39 +010059 * @brief Pass the responsibility for releasing the dynamic values to @p dst.
aPiecekd2c39372021-06-16 14:03:12 +020060 *
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 Vasko61ad1ff2022-02-10 15:48:39 +010063 * If the pointer is already pointing to some allocated memory, it is released beforehand.
aPiecekd2c39372021-06-16 14:03:12 +020064 */
65static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010066lyjson_ctx_give_dynamic_value(struct lyjson_ctx *jsonctx, char **dst)
aPiecekd2c39372021-06-16 14:03:12 +020067{
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 Vasko61ad1ff2022-02-10 15:48:39 +010079 /* give the dynamic value */
aPiecekd2c39372021-06-16 14:03:12 +020080 *dst = (char *)jsonctx->value;
81
Michal Vasko61ad1ff2022-02-10 15:48:39 +010082 /* responsibility for the release is now passed to dst */
aPiecekd2c39372021-06-16 14:03:12 +020083 jsonctx->dynamic = 0;
84}
85
86/**
Radek Krejci1798aae2020-07-14 13:26:06 +020087 * @brief Parse JSON member-name as [\@][prefix:][name]
88 *
Michal Vaskocabe0702020-08-12 10:14:36 +020089 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020090 * 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 Vaskocabe0702020-08-12 10:14:36 +0200101 * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise.
Radek Krejci1798aae2020-07-14 13:26:06 +0200102 */
103static void
Michal Vaskocabe0702020-08-12 10:14:36 +0200104lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p,
Radek Krejci857189e2020-09-01 13:26:36 +0200105 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200106{
107 const char *name, *prefix = NULL;
108 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200109 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200110
111 name = memchr(value, ':', value_len);
112 if (name != NULL) {
113 prefix = value;
114 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200115 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 prefix++;
117 }
118 prefix_len = name - prefix;
119 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200120 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200121 } else {
122 name = value;
123 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200124 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200125 name++;
126 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200127 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100128 }
129
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 *name_p = name;
131 *name_len_p = name_len;
132 *prefix_p = prefix;
133 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200134 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200135}
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 */
149static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200150lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200151 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200152{
153 struct lyd_node_opaq *onode;
154 const char *module_name = NULL;
155
156 assert(prefix_p && prefix_len_p);
157
158 if (local_prefix_len) {
159 *prefix_p = local_prefix;
160 *prefix_len_p = local_prefix_len;
161 return LY_SUCCESS;
162 }
163
164 *prefix_p = NULL;
165 while (node) {
166 if (node->schema) {
167 *prefix_p = node->schema->module->name;
168 break;
169 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200170 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100171 if (onode->name.module_name) {
172 *prefix_p = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200173 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100174 } else if (onode->name.prefix) {
175 *prefix_p = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200176 break;
177 }
Michal Vasko9e685082021-01-29 14:49:09 +0100178 node = lyd_parent(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200179 }
180 *prefix_len_p = ly_strlen(module_name);
181
182 return LY_SUCCESS;
183}
184
185/**
Michal Vasko87c5d6b2023-02-14 15:27:41 +0100186 * @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 Vaskoa4af6252022-05-20 10:38:54 +0200190 *
Radek Krejci1798aae2020-07-14 13:26:06 +0200191 * @param[in] jsonctx JSON context with the input data to skip.
192 * @return LY_ERR value.
193 */
194static LY_ERR
195lydjson_data_skip(struct lyjson_ctx *jsonctx)
196{
197 enum LYJSON_PARSER_STATUS status, current;
Michal Vasko09e04632023-03-22 14:34:10 +0100198 uint32_t depth;
Radek Krejci1798aae2020-07-14 13:26:06 +0200199
Michal Vasko09e04632023-03-22 14:34:10 +0100200 status = lyjson_ctx_status(jsonctx);
201 depth = lyjson_ctx_depth(jsonctx);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200202
Michal Vasko09e04632023-03-22 14:34:10 +0100203 switch (status) {
204 case LYJSON_OBJECT:
205 ++depth;
Michal Vaskoa4af6252022-05-20 10:38:54 +0200206
Michal Vasko09e04632023-03-22 14:34:10 +0100207 /* skip until object closes */
208 do {
Michal Vasko4f04d762022-09-05 08:38:26 +0200209 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
Michal Vasko09e04632023-03-22 14:34:10 +0100210 } while ((current != LYJSON_OBJECT_CLOSED) || (depth != lyjson_ctx_depth(jsonctx)));
211 break;
212 case LYJSON_ARRAY:
213 ++depth;
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200214
Michal Vasko09e04632023-03-22 14:34:10 +0100215 /* skip until array closes */
216 do {
217 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
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, &current));
223 if ((current == LYJSON_OBJECT) || (current == LYJSON_ARRAY)) {
224 LY_CHECK_RET(lydjson_data_skip(jsonctx));
Radek Krejci1798aae2020-07-14 13:26:06 +0200225 }
Michal Vasko09e04632023-03-22 14:34:10 +0100226 break;
227 default:
228 /* no other status expected */
229 LOGINT(jsonctx->ctx);
230 return LY_EINT;
Michal Vaskoa4af6252022-05-20 10:38:54 +0200231 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200232
233 return LY_SUCCESS;
234}
235
236/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100237 * @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 Vasko8cc3f662022-03-29 11:25:51 +0200246 * @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 Vasko61ad1ff2022-02-10 15:48:39 +0100248 * @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 */
252static LY_ERR
253lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200254 size_t name_len, struct lyd_node *parent, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100255{
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 Vasko8cc3f662022-03-29 11:25:51 +0200260 *snode = NULL;
261 *ext = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100262
Michal Vasko8cc3f662022-03-29 11:25:51 +0200263 /* get the element module, prefer parent context because of extensions */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100264 if (prefix_len) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200265 mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100266 } else if (parent) {
267 if (parent->schema) {
268 mod = parent->schema->module;
269 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100270 } else if (!(lydctx->int_opts & LYD_INTOPT_ANY)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100271 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 Vasko8cc3f662022-03-29 11:25:51 +0200278 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 Vasko61ad1ff2022-02-10 15:48:39 +0100281 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 Vasko61ad1ff2022-02-10 15:48:39 +0100291 }
292
293 /* get the schema node */
294 if (mod && (!parent || parent->schema)) {
295 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200296 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200298 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200300 if (!*snode) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100301 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200302 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 Vasko61ad1ff2022-02-10 15:48:39 +0100305 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 Vasko61ad1ff2022-02-10 15:48:39 +0100329 }
330 } else {
331 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200332 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100333 }
334 }
335
336cleanup:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100337 return ret;
338}
339
340/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200341 * @brief Check that the input data are parseable as the @p list.
342 *
343 * Checks for all the list's keys. Function does not revert the context state.
344 *
345 * @param[in] jsonctx JSON parser context.
346 * @param[in] list List schema node corresponding to the input data object.
347 * @return LY_SUCCESS in case the data are ok for the @p list
348 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
349 */
350static LY_ERR
351lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
352{
Michal Vasko09e04632023-03-22 14:34:10 +0100353 LY_ERR rc = LY_SUCCESS;
354 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200355 struct ly_set key_set = {0};
356 const struct lysc_node *snode;
Michal Vasko09e04632023-03-22 14:34:10 +0100357 uint32_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200358
359 assert(list && (list->nodetype == LYS_LIST));
Radek Krejci1798aae2020-07-14 13:26:06 +0200360
361 /* get all keys into a set (keys do not have if-features or anything) */
362 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100363 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Michal Vasko09e04632023-03-22 14:34:10 +0100364 rc = ly_set_add(&key_set, (void *)snode, 1, NULL);
365 LY_CHECK_GOTO(rc, cleanup);
366 }
367 if (!key_set.count) {
368 /* no keys */
369 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200370 }
371
Michal Vasko86fdf342022-12-01 11:08:29 +0100372 if (status == LYJSON_OBJECT) {
Michal Vasko09e04632023-03-22 14:34:10 +0100373 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 const char *name, *prefix;
375 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200376 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200377
378 /* match the key */
Michal Vasko09e04632023-03-22 14:34:10 +0100379 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
380 assert(status == LYJSON_OBJECT_NAME);
Radek Krejci1798aae2020-07-14 13:26:06 +0200381 snode = NULL;
382 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
383
384 if (!is_attr && !prefix) {
385 for (i = 0; i < key_set.count; ++i) {
386 snode = (const struct lysc_node *)key_set.objs[i];
387 if (!ly_strncmp(snode->name, name, name_len)) {
388 break;
389 }
390 }
Michal Vasko09e04632023-03-22 14:34:10 +0100391
392 /* get the value */
393 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200394
395 if (snode) {
396 /* we have the key, validate the value */
Michal Vaskob677d8e2023-03-23 15:37:07 +0100397 if ((status < LYJSON_NUMBER) || (status > LYJSON_NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200398 /* not a terminal */
Michal Vasko09e04632023-03-22 14:34:10 +0100399 rc = LY_ENOT;
Radek Krejci1798aae2020-07-14 13:26:06 +0200400 goto cleanup;
401 }
402
Michal Vasko09e04632023-03-22 14:34:10 +0100403 rc = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
404 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200405
406 /* key with a valid value, remove from the set */
407 ly_set_rm_index(&key_set, i, NULL);
408 }
Michal Vasko09e04632023-03-22 14:34:10 +0100409
410 /* next object */
411 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200412 } else {
Michal Vasko09e04632023-03-22 14:34:10 +0100413 /* skip the uninteresting object */
414 LY_CHECK_GOTO(rc = lydjson_data_skip(jsonctx), cleanup);
415 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200416 }
Michal Vasko09e04632023-03-22 14:34:10 +0100417 } while (key_set.count && (status == LYJSON_OBJECT_NEXT));
Radek Krejci1798aae2020-07-14 13:26:06 +0200418 }
419
420 if (key_set.count) {
421 /* some keys are missing/did not validate */
Michal Vasko09e04632023-03-22 14:34:10 +0100422 rc = LY_ENOT;
Radek Krejci1798aae2020-07-14 13:26:06 +0200423 }
424
425cleanup:
426 ly_set_erase(&key_set, NULL);
Michal Vasko09e04632023-03-22 14:34:10 +0100427 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +0200428}
429
430/**
431 * @brief Get the hint for the data type parsers according to the current JSON parser context.
432 *
433 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
434 * @param[in,out] status Pointer to the current context status,
435 * in some circumstances the function manipulates with the context so the status is updated.
436 * @param[out] type_hint_p Pointer to the variable to store the result.
437 * @return LY_SUCCESS in case of success.
438 * @return LY_EINVAL in case of invalid context status not referring to a value.
439 */
440static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200441lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200442{
443 *type_hint_p = 0;
444
445 if (*status_p == LYJSON_ARRAY) {
446 /* only [null] */
447 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200448 if (*status_p != LYJSON_NULL) {
Michal Vaskod1336a32022-10-25 15:00:20 +0200449 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON,
450 "Expected JSON name/value or special name/[null], but input data contains name/[%s].",
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200451 lyjson_token2str(*status_p));
452 return LY_EINVAL;
453 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200454
455 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
Michal Vasko09e04632023-03-22 14:34:10 +0100456 if (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_ARRAY_CLOSED) {
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200457 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.",
458 lyjson_token2str(*status_p));
459 return LY_EINVAL;
460 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200461
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200462 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200464 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200466 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200467 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200468 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200469 } else if (*status_p == LYJSON_NULL) {
470 *type_hint_p = 0;
471 } else {
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200472 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p));
Radek Krejci1798aae2020-07-14 13:26:06 +0200473 return LY_EINVAL;
474 }
475
476 return LY_SUCCESS;
477}
478
479/**
480 * @brief Check in advance if the input data are parsable according to the provided @p snode.
481 *
482 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
483 * is naturally done when the data are really parsed.
484 *
485 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
486 * as before calling, despite it is necessary to process input data for checking.
487 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
488 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
489 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
490 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
491 * @return LY_EINVAL in case of invalid leaf JSON encoding
492 * and they are expected to be parsed as opaq nodes.
493 */
494static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200495lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200496{
497 LY_ERR ret = LY_SUCCESS;
498 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
499 enum LYJSON_PARSER_STATUS status;
500
501 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200502
Michal Vasko32ac9942020-08-12 14:35:12 +0200503 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
504 /* can always be parsed as a data node if we have the schema node */
505 return LY_SUCCESS;
506 }
507
Michal Vasko85430702021-07-21 14:29:56 +0200508 /* backup parser */
509 lyjson_ctx_backup(jsonctx);
Michal Vasko09e04632023-03-22 14:34:10 +0100510 status = lyjson_ctx_status(jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200511
Michal Vasko85430702021-07-21 14:29:56 +0200512 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200513 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
514 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200515 switch (snode->nodetype) {
516 case LYS_LEAFLIST:
517 case LYS_LEAF:
518 /* value may not be valid in which case we parse it as an opaque node */
519 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
520 if (ret) {
521 break;
522 }
523
Radek Krejci8df109d2021-04-23 12:19:08 +0200524 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 ret = LY_ENOT;
526 }
527 break;
528 case LYS_LIST:
529 /* lists may not have all its keys */
530 if (lydjson_check_list(jsonctx, snode)) {
Michal Vasko86fdf342022-12-01 11:08:29 +0100531 /* invalid list, parse as opaque if it misses/has invalid some keys */
Radek Krejci1798aae2020-07-14 13:26:06 +0200532 ret = LY_ENOT;
533 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200534 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200535 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200536 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200537 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
538 }
539
Michal Vasko85430702021-07-21 14:29:56 +0200540 /* restore parser */
541 lyjson_ctx_restore(jsonctx);
542
Radek Krejci1798aae2020-07-14 13:26:06 +0200543 return ret;
544}
545
546/**
547 * @brief Join the forward-referencing metadata with their target data nodes.
548 *
549 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
550 *
551 * @param[in] lydctx JSON data parser context.
552 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
553 * as a starting point to search for the metadata's target data node
554 * @return LY_SUCCESS on success
555 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
556 */
557static LY_ERR
558lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
559{
560 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200561 struct lyd_node *node, *attr, *next, *meta_iter;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200562 struct lysc_ext_instance *ext;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200563 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200564 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100565 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200566
567 /* finish linking metadata */
568 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200569 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200570 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200571 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200572 const char *name, *prefix;
573 size_t name_len, prefix_len;
574 const struct lysc_node *snode;
575
Michal Vaskoad92b672020-11-12 13:11:31 +0100576 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200577 /* not an opaq metadata node */
578 continue;
579 }
580
Radek Krejciddace2c2021-01-08 11:30:56 +0100581 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100582 log_location_items++;
583
Michal Vaskoad92b672020-11-12 13:11:31 +0100584 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200585 /* metas' names are stored in dictionary, so checking pointers must works */
586 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100587 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200588 instance = 1;
589 } else {
590 instance++;
591 }
592
aPiecek5057c2e2021-05-17 10:28:03 +0200593 /* find the corresponding data node */
594 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200595 if (!node->schema) {
596 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100597 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200598 continue;
599 }
600
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200601 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100602 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
603 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200604 ret = LY_EVALID;
605 goto cleanup;
606 }
607
608 /* match */
609 match++;
610 if (match != instance) {
611 continue;
612 }
613
614 LY_LIST_FOR(meta_container->child, meta_iter) {
615 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200616 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100617
Michal Vaskoad92b672020-11-12 13:11:31 +0100618 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
619 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200620 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100621 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200622 LY_CHECK_GOTO(ret, cleanup);
623 }
624
625 /* done */
626 break;
627 } else {
628 /* this is the second time we are resolving the schema node, so it must succeed,
629 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100630 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
631 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200632 assert(is_attr);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200633 lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, lyd_parent(*first_p), &snode, &ext);
Radek Krejci1798aae2020-07-14 13:26:06 +0200634
635 if (snode != node->schema) {
636 continue;
637 }
638
639 /* match */
640 match++;
641 if (match != instance) {
642 continue;
643 }
644
645 LY_LIST_FOR(meta_container->child, meta_iter) {
646 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200647 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200648 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200649
Michal Vaskoad92b672020-11-12 13:11:31 +0100650 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200651 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200652 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100653 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100654 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200655 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100656 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200657 if (meta->name.prefix) {
658 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
659 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
660 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
661 meta->name.name);
662 } else {
663 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
664 meta->name.name);
665 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200666 ret = LY_EVALID;
667 goto cleanup;
668 }
669 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200670
Michal Vasko8cc3f662022-03-29 11:25:51 +0200671 /* add/correct flags */
672 ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, ext);
673 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200674 break;
675 }
676 }
677
678 if (match != instance) {
679 /* there is no corresponding data node for the metadata */
680 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100681 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
682 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200683 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200684 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100685 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
686 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200687 }
688 ret = LY_EVALID;
689 } else {
690 /* remove the opaq attr */
691 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200692 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200693 }
694 lyd_free_tree(attr);
695 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100696
Radek Krejciddace2c2021-01-08 11:30:56 +0100697 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100698 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200699 }
700
701cleanup:
702 lydict_remove(lydctx->jsonctx->ctx, prev);
703
Radek Krejciddace2c2021-01-08 11:30:56 +0100704 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200705 return ret;
706}
707
708/**
709 * @brief Parse a metadata member.
710 *
711 * @param[in] lydctx JSON data parser context.
712 * @param[in] snode Schema node of the metadata parent.
713 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
714 * so the data node does not exists. In such a case the metadata is stored in the context for the later
715 * processing by lydjson_metadata_finish().
716 * @return LY_SUCCESS on success
717 * @return Various LY_ERR values in case of failure.
718 */
719static LY_ERR
720lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
721{
Michal Vasko09e04632023-03-22 14:34:10 +0100722 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200723 enum LYJSON_PARSER_STATUS status;
724 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200725 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200726 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200727 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200728 size_t name_len, prefix_len = 0;
729 struct lys_module *mod;
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200731 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200732 struct lyd_node *prev = node;
Michal Vasko09d13b32022-12-01 13:49:50 +0100733 uint32_t instance = 0, val_hints;
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 uint16_t nodetype;
735
736 assert(snode || node);
737
738 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
Michal Vasko66c16312022-12-01 10:37:43 +0100739 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200740
741 /* move to the second item in the name/X pair */
Michal Vasko09e04632023-03-22 14:34:10 +0100742 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200743
744 /* check attribute encoding */
745 switch (nodetype) {
746 case LYS_LEAFLIST:
747 expected = "@name/array of objects/nulls";
Radek Krejci1798aae2020-07-14 13:26:06 +0200748 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
749
750next_entry:
Radek Krejci1798aae2020-07-14 13:26:06 +0200751 if (status == LYJSON_ARRAY_CLOSED) {
Michal Vasko57c8d432022-11-30 15:35:56 +0100752 /* no more metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 goto cleanup;
754 }
Michal Vasko09e04632023-03-22 14:34:10 +0100755
756 /* move into the array/next item */
757 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
758 instance++;
Michal Vasko57c8d432022-11-30 15:35:56 +0100759 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_NULL), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200760
Michal Vasko69730152020-10-09 16:30:07 +0200761 if (!node || (node->schema != prev->schema)) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200762 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%u of %s:%s to be coupled with metadata.",
Michal Vasko69730152020-10-09 16:30:07 +0200763 instance, prev->schema->module->name, prev->schema->name);
Michal Vasko09e04632023-03-22 14:34:10 +0100764 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200765 goto cleanup;
766 }
767
768 if (status == LYJSON_NULL) {
769 /* continue with the next entry in the leaf-list array */
770 prev = node;
771 node = node->next;
Michal Vasko09e04632023-03-22 14:34:10 +0100772
773 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200774 goto next_entry;
775 }
776 break;
777 case LYS_LEAF:
778 case LYS_ANYXML:
779 expected = "@name/object";
780
aPiecek5b115fc2021-05-24 14:32:42 +0200781 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200782 break;
783 case LYS_CONTAINER:
784 case LYS_LIST:
785 case LYS_ANYDATA:
786 case LYS_NOTIF:
787 case LYS_ACTION:
788 case LYS_RPC:
789 in_parent = 1;
790 expected = "@/object";
791 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
792 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200793 default:
Michal Vasko66c16312022-12-01 10:37:43 +0100794 LOGINT(ctx);
Michal Vasko09e04632023-03-22 14:34:10 +0100795 rc = LY_EINT;
Michal Vasko66c16312022-12-01 10:37:43 +0100796 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200797 }
798
799 /* process all the members inside a single metadata object */
800 assert(status == LYJSON_OBJECT);
Michal Vasko09e04632023-03-22 14:34:10 +0100801 do {
802 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
803 assert(status == LYJSON_OBJECT_NAME);
aPiecekd2c39372021-06-16 14:03:12 +0200804 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100805 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200806
Michal Vasko69d0ca12021-07-23 10:22:03 +0200807 if (!name_len) {
808 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
Michal Vasko09e04632023-03-22 14:34:10 +0100809 rc = LY_EVALID;
Michal Vasko69d0ca12021-07-23 10:22:03 +0200810 goto cleanup;
811 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100812 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100813 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Michal Vasko09e04632023-03-22 14:34:10 +0100814 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200815 goto cleanup;
816 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100817 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100818 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Michal Vasko09e04632023-03-22 14:34:10 +0100819 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200820 goto cleanup;
821 }
822
823 /* get the element module */
824 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
825 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100826 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100827 LOGVAL(ctx, LYVE_REFERENCE, "Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
Radek Krejci422afb12021-03-04 16:38:16 +0100828 (int)prefix_len, prefix, (int)name_len, name);
Michal Vasko09e04632023-03-22 14:34:10 +0100829 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 goto cleanup;
831 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100832 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200833 /* skip element with children */
Michal Vasko09e04632023-03-22 14:34:10 +0100834 LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup);
835 status = lyjson_ctx_status(lydctx->jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200836 /* end of the item */
837 continue;
838 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100839 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200840 }
841
842 /* get the value */
Michal Vasko09e04632023-03-22 14:34:10 +0100843 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Michal Vasko09d13b32022-12-01 13:49:50 +0100844
845 /* get value hints */
Michal Vasko09e04632023-03-22 14:34:10 +0100846 LY_CHECK_GOTO(rc = lydjson_value_type_hint(lydctx, &status, &val_hints), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200847
848 if (node->schema) {
849 /* create metadata */
Michal Vasko09e04632023-03-22 14:34:10 +0100850 rc = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, name, name_len, lydctx->jsonctx->value,
Michal Vasko09d13b32022-12-01 13:49:50 +0100851 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, val_hints, node->schema);
Michal Vasko09e04632023-03-22 14:34:10 +0100852 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200853
854 /* add/correct flags */
Michal Vasko09e04632023-03-22 14:34:10 +0100855 rc = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL);
856 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200857 } else {
858 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200859 const char *module_name;
860 size_t module_name_len;
861
862 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
863
Radek Krejci1798aae2020-07-14 13:26:06 +0200864 /* attr2 is always changed to the created attribute */
Michal Vasko09e04632023-03-22 14:34:10 +0100865 rc = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
Michal Vasko501af032020-11-11 20:27:44 +0100866 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Michal Vasko09d13b32022-12-01 13:49:50 +0100867 LY_VALUE_JSON, NULL, val_hints);
Michal Vasko09e04632023-03-22 14:34:10 +0100868 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200869 }
Michal Vasko09e04632023-03-22 14:34:10 +0100870
Radek Krejci1798aae2020-07-14 13:26:06 +0200871 /* next member */
Michal Vasko09e04632023-03-22 14:34:10 +0100872 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
873 } while (status == LYJSON_OBJECT_NEXT);
874 LY_CHECK_GOTO(status != LYJSON_OBJECT_CLOSED, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200875
876 if (nodetype == LYS_LEAFLIST) {
877 /* continue by processing another metadata object for the following
Michal Vaskob4043552022-12-01 10:11:55 +0100878 * leaf-list instance since they are always instantiated in JSON array */
Radek Krejci1798aae2020-07-14 13:26:06 +0200879 prev = node;
880 node = node->next;
Michal Vasko09e04632023-03-22 14:34:10 +0100881
882 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200883 goto next_entry;
884 }
885
Radek Krejci5813c362021-01-05 10:51:57 +0100886 /* success */
887 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200888
889representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100890 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200891 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
Michal Vasko5722c1b2022-01-24 07:47:49 +0100892 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
893 in_parent ? "" : "name");
Michal Vasko09e04632023-03-22 14:34:10 +0100894 rc = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100895
896cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200897 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100898 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko09e04632023-03-22 14:34:10 +0100899 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +0200900}
901
902/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200903 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
904 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200905 *
906 * @param[in] parent Parent node to insert to, can be NULL in case of top-level (or provided first_p).
Michal Vasko6ee6f432021-07-16 09:49:14 +0200907 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
908 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
909 * @param[in] last If set, always insert at the end.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200910 * @param[in] ext Extension instance of @p node_p, if any.
Radek Krejci1798aae2020-07-14 13:26:06 +0200911 */
912static void
Michal Vasko8cc3f662022-03-29 11:25:51 +0200913lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last,
914 struct lysc_ext_instance *ext)
Radek Krejci1798aae2020-07-14 13:26:06 +0200915{
Michal Vaskod027f382023-02-10 09:13:25 +0100916 if (!*node_p) {
917 return;
918 }
919
920 /* insert, keep first pointer correct */
921 if (ext) {
922 lyplg_ext_insert(parent, *node_p);
923 } else {
924 lyd_insert_node(parent, first_p, *node_p, last);
925 }
926 if (first_p) {
927 if (parent) {
928 *first_p = lyd_child(parent);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200929 } else {
Michal Vaskod027f382023-02-10 09:13:25 +0100930 while ((*first_p)->prev->next) {
931 *first_p = (*first_p)->prev;
Radek Krejci1798aae2020-07-14 13:26:06 +0200932 }
933 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200934 }
Michal Vaskod027f382023-02-10 09:13:25 +0100935 *node_p = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200936}
937
aPiecek4bf47212021-05-27 10:55:47 +0200938/**
939 * @brief Wrapper for ::lyd_create_opaq().
940 *
941 * @param[in] lydctx JSON data parser context.
942 * @param[in] name Name of the opaq node to create.
943 * @param[in] name_len Length of the @p name string.
944 * @param[in] prefix Prefix of the opaq node to create.
945 * @param[in] prefix_len Length of the @p prefx string.
946 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
947 * but must be set if @p first is not.
948 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
949 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
950 * @param[out] node_p Pointer to the created opaq node.
951 * @return LY_ERR value.
952 */
953static LY_ERR
954lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100955 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +0200956{
957 LY_ERR ret = LY_SUCCESS;
958 const char *value = NULL, *module_name;
959 size_t value_len = 0, module_name_len = 0;
960 ly_bool dynamic = 0;
961 uint32_t type_hint = 0;
962
Michal Vasko09e04632023-03-22 14:34:10 +0100963 if (*status_inner_p != LYJSON_OBJECT) {
aPiecek4bf47212021-05-27 10:55:47 +0200964 /* prepare for creating opaq node with a value */
965 value = lydctx->jsonctx->value;
966 value_len = lydctx->jsonctx->value_len;
967 dynamic = lydctx->jsonctx->dynamic;
968 lydctx->jsonctx->dynamic = 0;
969
970 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
971 }
972
973 /* create node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100974 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +0200975 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
976 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
977 if (dynamic) {
978 free((char *)value);
979 }
980
981 return ret;
982}
983
Michal Vaskoe0665742021-02-11 11:08:44 +0100984static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
985 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200986
987/**
988 * @brief Parse opaq node from the input.
989 *
990 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
991 *
992 * @param[in] lydctx JSON data parser context.
993 * @param[in] name Name of the opaq node to create.
994 * @param[in] name_len Length of the @p name string.
995 * @param[in] prefix Prefix of the opaq node to create.
996 * @param[in] prefix_len Length of the @p prefx string.
997 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
998 * but must be set if @p first is not.
999 * @param[in,out] status_p Pointer to the current status of the parser context,
1000 * since the function manipulates with the context and process the input, the status can be updated.
1001 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1002 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1003 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1004 * @param[out] node_p Pointer to the created opaq node.
1005 * @return LY_ERR value.
1006 */
1007static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001008lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001009 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1010 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001011{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001012 LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001013
Michal Vasko1a85d332021-08-27 10:35:28 +02001014 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1015 /* special array null value */
1016 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1017
1018 /* must be the only item */
1019 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
1020 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1021 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
1022 return LY_EVALID;
1023 }
1024
1025 goto finish;
1026 }
1027
Michal Vasko09e04632023-03-22 14:34:10 +01001028 while (*status_p == LYJSON_ARRAY) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001029 /* process another instance of the same node */
Michal Vasko09e04632023-03-22 14:34:10 +01001030 if (*status_inner_p == LYJSON_OBJECT) {
Michal Vasko1a85d332021-08-27 10:35:28 +02001031 /* array with objects, list */
1032 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
1033
Radek Krejci1798aae2020-07-14 13:26:06 +02001034 /* but first process children of the object in the array */
Michal Vasko09e04632023-03-22 14:34:10 +01001035 do {
Michal Vaskoe0665742021-02-11 11:08:44 +01001036 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Michal Vasko09e04632023-03-22 14:34:10 +01001037 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx);
1038 } while (*status_inner_p == LYJSON_OBJECT_NEXT);
Michal Vasko1a85d332021-08-27 10:35:28 +02001039 } else {
1040 /* array with values, leaf-list */
1041 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001042 }
1043
1044 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001045 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1046 goto finish;
1047 }
Michal Vasko09e04632023-03-22 14:34:10 +01001048 assert(*status_inner_p == LYJSON_ARRAY_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001049
1050 /* continue with the next instance */
Michal Vasko09e04632023-03-22 14:34:10 +01001051 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001052 assert(node_p);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001053 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001054 LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001055 }
1056
Michal Vasko09e04632023-03-22 14:34:10 +01001057 if (*status_p == LYJSON_OBJECT) {
aPiecekdbb8baf2021-05-27 11:01:51 +02001058 /* process children */
Michal Vasko09e04632023-03-22 14:34:10 +01001059 do {
aPiecekdbb8baf2021-05-27 11:01:51 +02001060 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Michal Vasko09e04632023-03-22 14:34:10 +01001061 *status_p = lyjson_ctx_status(lydctx->jsonctx);
1062 } while (*status_p == LYJSON_OBJECT_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001063 }
1064
aPiecekdbb8baf2021-05-27 11:01:51 +02001065finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001066 /* finish linking metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001067 return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001068}
1069
1070/**
aPiecek6cee5d02021-05-12 10:32:00 +02001071 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1072 *
1073 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1074 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1075 * for ::lydjson_parse_opaq().
1076 *
1077 * @param[in] lydctx JSON data parser context.
1078 * @param[in] name Name of the opaq node to create.
1079 * @param[in] name_len Length of the @p name string.
1080 * @param[in] prefix Prefix of the opaq node to create.
1081 * @param[in] prefix_len Length of the @p prefx string.
1082 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1083 * but must be set if @p first is not.
1084 * @param[in,out] status_p Pointer to the current status of the parser context,
1085 * since the function manipulates with the context and process the input, the status can be updated.
1086 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1087 * @param[out] node_p Pointer to the created opaq node.
1088 * @return LY_ERR value.
1089 */
1090static LY_ERR
Michal Vasko09e04632023-03-22 14:34:10 +01001091lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix,
1092 size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001093 struct lyd_node **first_p, struct lyd_node **node_p)
1094{
1095 enum LYJSON_PARSER_STATUS status_inner = 0;
1096
1097 /* move to the second item in the name/X pair */
1098 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1099
1100 if (*status_p == LYJSON_ARRAY) {
1101 /* move into the array */
1102 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1103 } else {
1104 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1105 status_inner = LYJSON_ERROR;
1106 }
1107
1108 if (status_inner == LYJSON_ERROR) {
1109 status_inner = *status_p;
1110 }
1111
1112 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001113 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1114 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001115
1116 return LY_SUCCESS;
1117}
1118
1119/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001120 * @brief Process the attribute container (starting by @)
1121 *
1122 * @param[in] lydctx JSON data parser context.
1123 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1124 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1125 * @param[in] name Name of the opaq node to create.
1126 * @param[in] name_len Length of the @p name string.
1127 * @param[in] prefix Prefix of the opaq node to create.
1128 * @param[in] prefix_len Length of the @p prefx string.
1129 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1130 * but must be set if @p first is not.
1131 * @param[in,out] status_p Pointer to the current status of the parser context,
1132 * since the function manipulates with the context and process the input, the status can be updated.
1133 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1134 * @param[out] node_p Pointer to the created opaq node.
1135 * @return LY_ERR value.
1136 */
1137static LY_ERR
1138lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001139 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1140 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001141{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001142 LY_ERR r;
Michal Vaskodcad5c82022-12-01 16:16:58 +01001143 const char *opaq_name, *mod_name;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001144 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001145
Michal Vaskofceac692022-12-01 13:50:27 +01001146 if (!snode && !prefix) {
1147 /* set the prefix */
1148 if (parent) {
1149 lydjson_get_node_prefix(parent, NULL, 0, &prefix, &prefix_len);
1150 } else {
1151 prefix = "";
1152 prefix_len = 0;
1153 }
1154 }
1155
1156 /* parse as an attribute to a (opaque) node */
1157 if (!attr_node) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001158 /* try to find the instance */
Michal Vaskofceac692022-12-01 13:50:27 +01001159 LY_LIST_FOR(*first_p, attr_node) {
1160 if (snode) {
1161 if (attr_node->schema) {
1162 if (attr_node->schema == snode) {
1163 break;
1164 }
1165 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001166 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1167 if (!strcmp(LYD_NAME(attr_node), snode->name) && mod_name && !strcmp(mod_name, snode->module->name)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001168 break;
1169 }
1170 }
1171 } else {
1172 if (attr_node->schema) {
1173 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) &&
1174 !ly_strncmp(attr_node->schema->module->name, prefix, prefix_len)) {
1175 break;
1176 }
1177 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001178 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1179 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) && mod_name &&
1180 !ly_strncmp(mod_name, prefix, prefix_len)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001181 break;
1182 }
1183 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001184 }
1185 }
1186 }
1187 if (!attr_node) {
1188 /* parse just as an opaq node with the name beginning with @,
1189 * later we have to check that it belongs to a standard node
1190 * and it is supposed to be converted to a metadata */
1191 uint32_t prev_opts;
1192
Radek Krejci1798aae2020-07-14 13:26:06 +02001193 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001194 prev_opts = lydctx->parse_opts;
1195 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1196 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001197
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001198 opaq_name = prefix ? prefix - 1 : name - 1;
1199 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1200 r = lydjson_ctx_next_parse_opaq(lydctx, opaq_name, opaq_name_len, NULL, 0, parent, status_p, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001201
1202 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001203 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001204 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001205 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001206 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001207 }
1208
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001209 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001210}
1211
1212/**
Michal Vasko76096ec2022-02-24 16:06:16 +01001213 * @brief Parse a single anydata/anyxml node.
1214 *
1215 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1216 * as before calling, despite it is necessary to process input data for checking.
1217 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001218 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko76096ec2022-02-24 16:06:16 +01001219 * @param[in,out] status JSON parser status, is updated.
1220 * @param[out] node Parsed data (or opaque) node.
1221 * @return LY_SUCCESS if a node was successfully parsed,
1222 * @return LY_ENOT in case of invalid JSON encoding,
1223 * @return LY_ERR on other errors.
1224 */
1225static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +02001226lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1227 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko76096ec2022-02-24 16:06:16 +01001228{
Michal Vaskod027f382023-02-10 09:13:25 +01001229 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +01001230 uint32_t prev_parse_opts, prev_int_opts;
1231 struct ly_in in_start;
Michal Vasko7ca33252022-12-21 09:30:32 +01001232 char *val = NULL;
Michal Vasko09e04632023-03-22 14:34:10 +01001233 const char *end;
Michal Vasko76096ec2022-02-24 16:06:16 +01001234 struct lyd_node *tree = NULL;
1235
1236 assert(snode->nodetype & LYD_NODE_ANY);
1237
1238 /* status check according to allowed JSON types */
1239 if (snode->nodetype == LYS_ANYXML) {
Michal Vasko09e04632023-03-22 14:34:10 +01001240 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) &&
1241 (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) &&
1242 (*status != LYJSON_NULL), LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001243 } else {
Michal Vasko09e04632023-03-22 14:34:10 +01001244 LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001245 }
1246
1247 /* create any node */
1248 switch (*status) {
1249 case LYJSON_OBJECT:
1250 /* parse any data tree with correct options, first backup the current options and then make the parser
1251 * process data as opaq nodes */
1252 prev_parse_opts = lydctx->parse_opts;
1253 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001254 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vasko76096ec2022-02-24 16:06:16 +01001255 prev_int_opts = lydctx->int_opts;
1256 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
1257
1258 /* process the anydata content */
Michal Vasko09e04632023-03-22 14:34:10 +01001259 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001260 r = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
1261 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1262
Michal Vasko09e04632023-03-22 14:34:10 +01001263 *status = lyjson_ctx_status(lydctx->jsonctx);
1264 } while (*status == LYJSON_OBJECT_NEXT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001265
1266 /* restore parser options */
1267 lydctx->parse_opts = prev_parse_opts;
1268 lydctx->int_opts = prev_int_opts;
1269
1270 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001271 r = lydjson_metadata_finish(lydctx, &tree);
1272 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001273
Michal Vaskod027f382023-02-10 09:13:25 +01001274 r = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
1275 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001276 break;
1277 case LYJSON_ARRAY:
1278 /* skip until the array end */
1279 in_start = *lydctx->jsonctx->in;
1280 LY_CHECK_RET(lydjson_data_skip(lydctx->jsonctx));
1281
Michal Vasko09e04632023-03-22 14:34:10 +01001282 /* return back by all the WS */
1283 end = lydctx->jsonctx->in->current;
1284 while (is_jsonws(end[-1])) {
1285 --end;
1286 }
1287
Michal Vasko76096ec2022-02-24 16:06:16 +01001288 /* make a copy of the whole array and store it */
Michal Vasko09e04632023-03-22 14:34:10 +01001289 if (asprintf(&val, "[%.*s", (int)(end - in_start.current), in_start.current) == -1) {
Michal Vasko76096ec2022-02-24 16:06:16 +01001290 LOGMEM(lydctx->jsonctx->ctx);
1291 return LY_EMEM;
1292 }
Michal Vaskod027f382023-02-10 09:13:25 +01001293 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1294 LY_CHECK_ERR_GOTO(r, rc = r, val_err);
Michal Vasko76096ec2022-02-24 16:06:16 +01001295 break;
1296 case LYJSON_STRING:
1297 /* string value */
1298 if (lydctx->jsonctx->dynamic) {
1299 LY_CHECK_RET(lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node));
1300 lydctx->jsonctx->dynamic = 0;
1301 } else {
1302 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1303 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1304
Michal Vaskod027f382023-02-10 09:13:25 +01001305 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
1306 LY_CHECK_ERR_GOTO(r, rc = r, val_err);
Michal Vasko76096ec2022-02-24 16:06:16 +01001307 }
1308 break;
1309 case LYJSON_NUMBER:
1310 case LYJSON_FALSE:
1311 case LYJSON_TRUE:
1312 /* JSON value */
1313 assert(!lydctx->jsonctx->dynamic);
1314 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1315 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1316
Michal Vaskod027f382023-02-10 09:13:25 +01001317 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1318 LY_CHECK_ERR_GOTO(r, rc = r, val_err);
Michal Vasko76096ec2022-02-24 16:06:16 +01001319 break;
1320 case LYJSON_NULL:
1321 /* no value */
Michal Vaskod027f382023-02-10 09:13:25 +01001322 r = lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node);
1323 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001324 break;
Michal Vasko76096ec2022-02-24 16:06:16 +01001325 default:
1326 LOGINT_RET(lydctx->jsonctx->ctx);
1327 }
1328
Michal Vaskod027f382023-02-10 09:13:25 +01001329cleanup:
1330 return rc;
Michal Vasko7ca33252022-12-21 09:30:32 +01001331
1332val_err:
1333 free(val);
1334 return rc;
Michal Vasko76096ec2022-02-24 16:06:16 +01001335}
1336
1337/**
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001338 * @brief Parse a single instance of an inner node.
1339 *
1340 * @param[in] lydctx JSON data parser context.
1341 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1342 * @param[in] ext Extension instance of @p snode, if any.
1343 * @param[in,out] status JSON parser status, is updated.
1344 * @param[out] node Parsed data (or opaque) node.
1345 * @return LY_SUCCESS if a node was successfully parsed,
1346 * @return LY_ENOT in case of invalid JSON encoding,
1347 * @return LY_ERR on other errors.
1348 */
1349static LY_ERR
1350lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1351 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
1352{
Michal Vaskod027f382023-02-10 09:13:25 +01001353 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001354 uint32_t prev_parse_opts = lydctx->parse_opts;
1355
Michal Vasko09e04632023-03-22 14:34:10 +01001356 LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001357
1358 /* create inner node */
1359 LY_CHECK_RET(lyd_create_inner(snode, node));
1360
1361 /* use it for logging */
1362 LOG_LOCSET(NULL, *node, NULL, NULL);
1363
1364 if (ext) {
1365 /* only parse these extension data and validate afterwards */
1366 lydctx->parse_opts |= LYD_PARSE_ONLY;
1367 }
1368
1369 /* process children */
Michal Vasko09e04632023-03-22 14:34:10 +01001370 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001371 r = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
1372 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1373
Michal Vasko09e04632023-03-22 14:34:10 +01001374 *status = lyjson_ctx_status(lydctx->jsonctx);
1375 } while (*status == LYJSON_OBJECT_NEXT);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001376
1377 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001378 r = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
1379 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001380
1381 if (snode->nodetype == LYS_LIST) {
1382 /* check all keys exist */
Michal Vaskod027f382023-02-10 09:13:25 +01001383 r = lyd_parse_check_keys(*node);
1384 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001385 }
1386
1387 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1388 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +01001389 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
1390 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001391
1392 /* add any missing default children */
Michal Vaskod027f382023-02-10 09:13:25 +01001393 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
1394 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1395 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001396 }
1397
1398cleanup:
1399 lydctx->parse_opts = prev_parse_opts;
1400 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001401 if (!(*node)->hash) {
1402 /* list without keys is unusable */
1403 lyd_free_tree(*node);
1404 *node = NULL;
1405 }
Michal Vaskod027f382023-02-10 09:13:25 +01001406 return rc;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001407}
1408
1409/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001410 * @brief Parse a single instance of a node.
1411 *
1412 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1413 * as before calling, despite it is necessary to process input data for checking.
1414 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1415 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1416 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001417 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko32ac9942020-08-12 14:35:12 +02001418 * @param[in] name Parsed JSON node name.
1419 * @param[in] name_len Lenght of @p name.
1420 * @param[in] prefix Parsed JSON node prefix.
1421 * @param[in] prefix_len Length of @p prefix.
1422 * @param[in,out] status JSON parser status, is updated.
1423 * @param[out] node Parsed data (or opaque) node.
1424 * @return LY_SUCCESS if a node was successfully parsed,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001425 * @return LY_ENOT in case of invalid JSON encoding,
Michal Vasko32ac9942020-08-12 14:35:12 +02001426 * @return LY_ERR on other errors.
1427 */
1428static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001429lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001430 const struct lysc_node *snode, struct lysc_ext_instance *ext, const char *name, size_t name_len,
1431 const char *prefix, size_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001432{
Michal Vaskod027f382023-02-10 09:13:25 +01001433 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +01001434 uint32_t type_hints = 0;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001435
1436 LOG_LOCSET(snode, NULL, NULL, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001437
Michal Vaskod027f382023-02-10 09:13:25 +01001438 r = lydjson_data_check_opaq(lydctx, snode, &type_hints);
1439 if (r == LY_SUCCESS) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001440 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1441 if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001442 if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
1443 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) {
Michal Vaskod027f382023-02-10 09:13:25 +01001444 rc = LY_ENOT;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001445 goto cleanup;
1446 }
Michal Vaskobbdadda2022-01-06 11:40:10 +01001447
Michal Vasko32ac9942020-08-12 14:35:12 +02001448 /* create terminal node */
Michal Vaskod027f382023-02-10 09:13:25 +01001449 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1450 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, type_hints, node);
1451 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001452
1453 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001454 if (*status == LYJSON_ARRAY) {
1455 /* only [null], 2 more moves are needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001456 r = lyjson_ctx_next(lydctx->jsonctx, status);
1457 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001458 assert(*status == LYJSON_NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001459
1460 r = lyjson_ctx_next(lydctx->jsonctx, status);
1461 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001462 assert(*status == LYJSON_ARRAY_CLOSED);
1463 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001464 } else if (snode->nodetype & LYD_NODE_INNER) {
1465 /* create inner node */
Michal Vaskod027f382023-02-10 09:13:25 +01001466 r = lydjson_parse_instance_inner(lydctx, snode, ext, status, node);
1467 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001468 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001469 /* create any node */
Michal Vaskod027f382023-02-10 09:13:25 +01001470 r = lydjson_parse_any(lydctx, snode, ext, status, node);
1471 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001472 }
Michal Vaskod027f382023-02-10 09:13:25 +01001473 LY_CHECK_GOTO(!*node, cleanup);
Michal Vaskocaf608d2021-07-23 13:51:23 +02001474
1475 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001476 r = lyd_parse_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext);
1477 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001478
Michal Vaskoeba23112022-08-26 08:35:41 +02001479 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1480 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001481 r = lyd_validate_node_ext(*node, &lydctx->ext_node);
1482 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001483 }
Michal Vaskod027f382023-02-10 09:13:25 +01001484 } else if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001485 /* parse it again as an opaq node */
Michal Vaskod027f382023-02-10 09:13:25 +01001486 r = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
1487 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001488
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001489 if (snode->nodetype == LYS_LIST) {
1490 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1491 } else if (snode->nodetype == LYS_LEAFLIST) {
1492 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001493 }
Michal Vaskoa32b6b72022-07-11 15:55:56 +02001494 } else {
1495 /* error */
Michal Vasko1e8746a2023-02-13 09:15:16 +01001496 rc = r;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001497 goto cleanup;
Michal Vasko32ac9942020-08-12 14:35:12 +02001498 }
1499
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001500cleanup:
1501 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001502 return rc;
Michal Vasko32ac9942020-08-12 14:35:12 +02001503}
1504
1505/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001506 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001507 *
1508 * @param[in] lydctx JSON data parser context.
1509 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1510 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
Michal Vaskoe0665742021-02-11 11:08:44 +01001511 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001512 * @return LY_ERR value.
1513 */
1514static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001515lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Radek Krejci1798aae2020-07-14 13:26:06 +02001516{
Michal Vaskod027f382023-02-10 09:13:25 +01001517 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko09e04632023-03-22 14:34:10 +01001518 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx);
aPiecek49e2a3a2021-05-13 10:36:46 +02001519 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001520 size_t name_len, prefix_len = 0;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001521 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001522 const struct lysc_node *snode = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001523 struct lysc_ext_instance *ext;
Michal Vasko32ac9942020-08-12 14:35:12 +02001524 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001525 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001526 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001527
1528 assert(parent || first_p);
Michal Vasko09e04632023-03-22 14:34:10 +01001529 assert((status == LYJSON_OBJECT) || (status == LYJSON_OBJECT_NEXT));
Radek Krejci1798aae2020-07-14 13:26:06 +02001530
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001531 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1532 /* all descendants should be parsed */
1533 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1534
Michal Vasko09e04632023-03-22 14:34:10 +01001535 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1536 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1537 if (status == LYJSON_OBJECT_CLOSED) {
1538 /* empty object, fine... */
1539 goto cleanup;
1540 }
1541
Radek Krejci1798aae2020-07-14 13:26:06 +02001542 /* process the node name */
Michal Vasko09e04632023-03-22 14:34:10 +01001543 assert(status == LYJSON_OBJECT_NAME);
aPiecekd2c39372021-06-16 14:03:12 +02001544 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001545 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001546
Michal Vaskocabe0702020-08-12 10:14:36 +02001547 if (!is_meta || name_len || prefix_len) {
1548 /* get the schema node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001549 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001550 if (r == LY_ENOT) {
1551 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001552 goto cleanup;
Michal Vasko514f6982023-02-17 09:07:39 +01001553 } else if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1554 rc = r;
1555
1556 /* skip the invalid data */
1557 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1558 rc = r;
1559 }
1560 goto cleanup;
1561 } else if (r) {
1562 /* error */
1563 rc = r;
1564 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001565 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001566
Michal Vaskocabe0702020-08-12 10:14:36 +02001567 if (!snode) {
1568 /* we will not be parsing it as metadata */
1569 is_meta = 0;
1570 }
1571 }
1572
1573 if (is_meta) {
1574 /* parse as metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001575 if (!name_len && !prefix_len && !parent) {
1576 LOGVAL(ctx, LYVE_SYNTAX_JSON,
1577 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1578 r = LY_EVALID;
1579 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1580 } else if (!name_len && !prefix_len) {
Michal Vaskocabe0702020-08-12 10:14:36 +02001581 /* parent's metadata without a name - use the schema from the parent */
Michal Vaskoe0665742021-02-11 11:08:44 +01001582 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001583 snode = attr_node->schema;
1584 }
Michal Vaskod027f382023-02-10 09:13:25 +01001585 r = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001586 first_p, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001587 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001588 } else if (!snode) {
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001589 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
1590 /* skip element with children */
1591 r = lydjson_data_skip(lydctx->jsonctx);
1592 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1593 } else {
1594 /* parse as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001595
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001596 /* opaq node cannot have an empty string as the name. */
1597 if (name_len == 0) {
Michal Vaskobfebf662023-02-17 09:07:22 +01001598 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "JSON object member name cannot be a zero-length string.");
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001599 r = LY_EVALID;
1600 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1601 }
1602
1603 /* move to the second item in the name/X pair and parse opaq */
1604 r = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node);
1605 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
aPiecekb7b29e62021-05-11 10:02:43 +02001606 }
Michal Vaskocabe0702020-08-12 10:14:36 +02001607 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001608 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001609
1610 /* move to the second item in the name/X pair */
Michal Vaskod027f382023-02-10 09:13:25 +01001611 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1612 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001613
Michal Vasko7ca33252022-12-21 09:30:32 +01001614 /* set expected representation */
1615 switch (snode->nodetype) {
1616 case LYS_LEAFLIST:
1617 expected = "name/array of values";
1618 break;
1619 case LYS_LIST:
1620 expected = "name/array of objects";
1621 break;
1622 case LYS_LEAF:
1623 if (status == LYJSON_ARRAY) {
1624 expected = "name/[null]";
1625 } else {
1626 expected = "name/value";
1627 }
1628 break;
1629 case LYS_CONTAINER:
1630 case LYS_NOTIF:
1631 case LYS_ACTION:
1632 case LYS_RPC:
1633 case LYS_ANYDATA:
1634 expected = "name/object";
1635 break;
1636 case LYS_ANYXML:
1637 if (status == LYJSON_ARRAY) {
1638 expected = "name/array";
1639 } else {
1640 expected = "name/value";
1641 }
1642 break;
1643 }
1644
1645 /* check the representation according to the nodetype and then continue with the content */
Radek Krejci1798aae2020-07-14 13:26:06 +02001646 switch (snode->nodetype) {
1647 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001648 case LYS_LIST:
Radek Krejci1798aae2020-07-14 13:26:06 +02001649 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1650
Michal Vasko32ac9942020-08-12 14:35:12 +02001651 /* process all the values/objects */
1652 do {
Michal Vasko09e04632023-03-22 14:34:10 +01001653 /* move into array/next value */
1654 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1655 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1656 if (status == LYJSON_ARRAY_CLOSED) {
1657 /* empty array, fine... */
1658 break;
1659 }
1660
Michal Vaskod027f382023-02-10 09:13:25 +01001661 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001662 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001663 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001664 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001665 }
Michal Vaskod027f382023-02-10 09:13:25 +01001666 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1667
Michal Vasko8cc3f662022-03-29 11:25:51 +02001668 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Radek Krejci1798aae2020-07-14 13:26:06 +02001669
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001670 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001671 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1672 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko09e04632023-03-22 14:34:10 +01001673 } while (status == LYJSON_ARRAY_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001674
1675 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001676 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001677 case LYS_CONTAINER:
1678 case LYS_NOTIF:
1679 case LYS_ACTION:
1680 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001681 case LYS_ANYDATA:
1682 case LYS_ANYXML:
Michal Vasko32ac9942020-08-12 14:35:12 +02001683 /* process the value/object */
Michal Vaskod027f382023-02-10 09:13:25 +01001684 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001685 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001686 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001687 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001688 }
Michal Vaskod027f382023-02-10 09:13:25 +01001689 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001690
Michal Vasko32ac9942020-08-12 14:35:12 +02001691 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001692 /* rememeber the RPC/action/notification */
1693 lydctx->op_node = node;
1694 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001695 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001696 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001697 }
1698
Michal Vasko32ac9942020-08-12 14:35:12 +02001699 /* finally connect the parsed node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001700 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Michal Vaskoe0665742021-02-11 11:08:44 +01001701
1702 /* rememeber a successfully parsed node */
Michal Vasko4e26adc2022-03-30 11:01:16 +02001703 if (parsed && node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001704 ly_set_add(parsed, node, 1, NULL);
1705 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001706
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001707 if (!parse_subtree) {
1708 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001709 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1710 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001711 }
1712
Radek Krejci5813c362021-01-05 10:51:57 +01001713 /* success */
1714 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001715
1716representation_error:
Michal Vaskobfebf662023-02-17 09:07:22 +01001717 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as name/%s.",
1718 expected, lys_nodetype2str(snode->nodetype), snode->name, lyjson_token2str(status));
Michal Vaskod027f382023-02-10 09:13:25 +01001719 rc = LY_EVALID;
1720 if (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) {
1721 /* try to skip the invalid data */
Michal Vasko560f9de2023-02-10 09:56:01 +01001722 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1723 rc = r;
1724 }
Michal Vaskod027f382023-02-10 09:13:25 +01001725 }
Radek Krejci5813c362021-01-05 10:51:57 +01001726
1727cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001728 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001729 lyd_free_tree(node);
Michal Vaskod027f382023-02-10 09:13:25 +01001730 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +02001731}
1732
1733/**
1734 * @brief Common start of JSON parser processing different types of the input data.
1735 *
1736 * @param[in] ctx libyang context
1737 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001738 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1739 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001740 * @param[out] lydctx_p Data parser context to finish validation.
1741 * @return LY_ERR value.
1742 */
1743static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001744lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
Michal Vasko09e04632023-03-22 14:34:10 +01001745 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001746{
1747 LY_ERR ret = LY_SUCCESS;
1748 struct lyd_json_ctx *lydctx;
Michal Vasko09e04632023-03-22 14:34:10 +01001749 enum LYJSON_PARSER_STATUS status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001750
Radek Krejcicd5f6222020-11-12 08:54:13 +01001751 assert(lydctx_p);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001752
Radek Krejci1798aae2020-07-14 13:26:06 +02001753 /* init context */
1754 lydctx = calloc(1, sizeof *lydctx);
1755 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001756 lydctx->parse_opts = parse_opts;
1757 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001758 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001759
Michal Vasko09e04632023-03-22 14:34:10 +01001760 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1761 status = lyjson_ctx_status(lydctx->jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001762
Michal Vasko09e04632023-03-22 14:34:10 +01001763 /* parse_opts & LYD_PARSE_SUBTREE not implemented */
1764 if (status != LYJSON_OBJECT) {
Radek Krejcicd5f6222020-11-12 08:54:13 +01001765 /* expecting top-level object */
Michal Vasko09e04632023-03-22 14:34:10 +01001766 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001767 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001768 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001769 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001770 }
Michal Vasko09e04632023-03-22 14:34:10 +01001771
1772 *lydctx_p = lydctx;
1773 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001774}
1775
1776LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001777lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001778 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
Michal Vaskoddd76592022-01-17 13:34:48 +01001779 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001780{
Michal Vaskod027f382023-02-10 09:13:25 +01001781 LY_ERR r, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001782 struct lyd_json_ctx *lydctx = NULL;
1783 enum LYJSON_PARSER_STATUS status;
1784
Michal Vasko09e04632023-03-22 14:34:10 +01001785 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx);
1786 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001787
Michal Vaskoe0665742021-02-11 11:08:44 +01001788 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001789 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001790
1791 /* find the operation node if it exists already */
1792 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1793
Radek Krejci1798aae2020-07-14 13:26:06 +02001794 /* read subtree(s) */
Michal Vasko09e04632023-03-22 14:34:10 +01001795 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001796 r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1797 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001798
Michal Vasko09e04632023-03-22 14:34:10 +01001799 status = lyjson_ctx_status(lydctx->jsonctx);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001800
1801 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001802 break;
1803 }
Michal Vasko09e04632023-03-22 14:34:10 +01001804 } while (status == LYJSON_OBJECT_NEXT);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001805 assert((status == LYJSON_END) || (status == LYJSON_OBJECT_CLOSED));
Michal Vaskoe0665742021-02-11 11:08:44 +01001806
1807 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
Michal Vasko09e04632023-03-22 14:34:10 +01001808 (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001809 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001810 r = LY_EVALID;
1811 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001812 }
1813 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1814 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001815 r = LY_EVALID;
1816 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001817 }
1818
1819 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001820 r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1821 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001822
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001823 if (parse_opts & LYD_PARSE_SUBTREE) {
1824 /* check for a sibling object */
1825 assert(subtree_sibling);
1826 if (lydctx->jsonctx->in->current[0] == ',') {
1827 *subtree_sibling = 1;
1828
1829 /* move to the next object */
1830 ly_in_skip(lydctx->jsonctx->in, 1);
1831 } else {
1832 *subtree_sibling = 0;
1833 }
1834 }
1835
Radek Krejci1798aae2020-07-14 13:26:06 +02001836cleanup:
1837 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001838 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001839 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001840
Michal Vaskod027f382023-02-10 09:13:25 +01001841 if (rc && (!lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001842 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001843 } else {
1844 *lydctx_p = (struct lyd_ctx *)lydctx;
Michal Vaskof8ebf132022-11-21 14:06:48 +01001845
1846 /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1847 lyjson_ctx_free(lydctx->jsonctx);
1848 lydctx->jsonctx = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001849 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001850 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001851}