blob: 71167d8ed5384733522a01d0f998ce7eaa095138 [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 Vasko61ad1ff2022-02-10 15:48:39 +01007 * Copyright (c) 2020 - 2022 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 Vaskoa4af6252022-05-20 10:38:54 +0200198 uint32_t orig_depth;
Radek Krejci1798aae2020-07-14 13:26:06 +0200199
200 status = lyjson_ctx_status(jsonctx, 0);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200201 orig_depth = jsonctx->depth;
202
203 /* next */
204 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
205
Michal Vaskod027f382023-02-10 09:13:25 +0100206 if ((status != LYJSON_OBJECT) && (status != LYJSON_ARRAY)) {
207 return LY_SUCCESS;
208 }
209
Michal Vaskoa09e8662022-06-15 07:58:24 +0200210 if ((status == LYJSON_OBJECT) && (current != LYJSON_OBJECT) && (current != LYJSON_ARRAY)) {
Michal Vaskoa4af6252022-05-20 10:38:54 +0200211 /* no nested objects */
Michal Vaskoa4af6252022-05-20 10:38:54 +0200212 return LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +0100213 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200214
215 /* skip after the content */
Michal Vaskoa4af6252022-05-20 10:38:54 +0200216 while ((jsonctx->depth > orig_depth) || (current != status + 1)) {
Michal Vasko4f04d762022-09-05 08:38:26 +0200217 if (current == LYJSON_ARRAY) {
218 /* skip the array separately */
219 LY_CHECK_RET(lydjson_data_skip(jsonctx));
220 current = lyjson_ctx_status(jsonctx, 0);
221 } else {
222 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
223 }
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200224
Michal Vaskoa4af6252022-05-20 10:38:54 +0200225 if (current == LYJSON_END) {
226 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200227 }
Michal Vaskoa4af6252022-05-20 10:38:54 +0200228 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200229
230 return LY_SUCCESS;
231}
232
233/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100234 * @brief Get schema node corresponding to the input parameters.
235 *
236 * @param[in] lydctx JSON data parser context.
237 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
238 * @param[in] prefix Requested node's prefix (module name).
239 * @param[in] prefix_len Length of the @p prefix.
240 * @param[in] name Requested node's name.
241 * @param[in] name_len Length of the @p name.
242 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200243 * @param[out] snode Found schema node corresponding to the input parameters. If NULL, parse as an opaque node.
244 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100245 * @return LY_SUCCES on success.
246 * @return LY_ENOT if the whole object was parsed (skipped or as an extension).
247 * @return LY_ERR on error.
248 */
249static LY_ERR
250lydjson_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 +0200251 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 +0100252{
253 LY_ERR ret = LY_SUCCESS, r;
254 struct lys_module *mod = NULL;
255 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
256
Michal Vasko8cc3f662022-03-29 11:25:51 +0200257 *snode = NULL;
258 *ext = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100259
Michal Vasko8cc3f662022-03-29 11:25:51 +0200260 /* get the element module, prefer parent context because of extensions */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100261 if (prefix_len) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200262 mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263 } else if (parent) {
264 if (parent->schema) {
265 mod = parent->schema->module;
266 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100267 } else if (!(lydctx->int_opts & LYD_INTOPT_ANY)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100268 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
269 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
270 ret = LY_EVALID;
271 goto cleanup;
272 }
273 if (!mod) {
274 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200275 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
276 if (r != LY_ENOT) {
277 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100278 ret = r;
279 goto cleanup;
280 }
281
282 /* unknown module */
283 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
284 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
285 ret = LY_EVALID;
286 goto cleanup;
287 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100288 }
289
290 /* get the schema node */
291 if (mod && (!parent || parent->schema)) {
292 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200293 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100294 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200295 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100296 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200297 if (!*snode) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100298 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200299 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
300 if (r != LY_ENOT) {
301 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100302 ret = r;
303 goto cleanup;
304 }
305
306 /* unknown data node */
307 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
308 if (parent) {
309 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
310 (int)name_len, name, LYD_NAME(parent));
311 } else if (lydctx->ext) {
312 if (lydctx->ext->argument) {
313 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
314 "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
315 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
316 } else {
317 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
318 (int)name_len, name, lydctx->ext->def->name);
319 }
320 } else {
321 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
322 (int)name_len, name, mod->name);
323 }
324 ret = LY_EVALID;
325 goto cleanup;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100326 }
327 } else {
328 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200329 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100330 }
331 }
332
333cleanup:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100334 return ret;
335}
336
337/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200338 * @brief Check that the input data are parseable as the @p list.
339 *
340 * Checks for all the list's keys. Function does not revert the context state.
341 *
342 * @param[in] jsonctx JSON parser context.
343 * @param[in] list List schema node corresponding to the input data object.
344 * @return LY_SUCCESS in case the data are ok for the @p list
345 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
346 */
347static LY_ERR
348lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
349{
350 LY_ERR ret = LY_SUCCESS;
351 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
352 struct ly_set key_set = {0};
353 const struct lysc_node *snode;
354 uint32_t i, status_count;
355
356 assert(list && (list->nodetype == LYS_LIST));
Radek Krejci1798aae2020-07-14 13:26:06 +0200357
358 /* get all keys into a set (keys do not have if-features or anything) */
359 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100360 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200361 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200362 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200363 }
364
Michal Vasko86fdf342022-12-01 11:08:29 +0100365 if (status == LYJSON_OBJECT) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200366 status_count = jsonctx->status.count;
367
Michal Vasko86fdf342022-12-01 11:08:29 +0100368 while (key_set.count && (status != LYJSON_OBJECT_CLOSED)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200369 const char *name, *prefix;
370 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200371 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200372
373 /* match the key */
374 snode = NULL;
375 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
376
377 if (!is_attr && !prefix) {
378 for (i = 0; i < key_set.count; ++i) {
379 snode = (const struct lysc_node *)key_set.objs[i];
380 if (!ly_strncmp(snode->name, name, name_len)) {
381 break;
382 }
383 }
384 /* go into the item to a) process it as a key or b) start skipping it as another list child */
385 ret = lyjson_ctx_next(jsonctx, &status);
386 LY_CHECK_GOTO(ret, cleanup);
387
388 if (snode) {
389 /* we have the key, validate the value */
390 if (status < LYJSON_NUMBER) {
391 /* not a terminal */
392 ret = LY_ENOT;
393 goto cleanup;
394 }
395
Radek Krejci8df109d2021-04-23 12:19:08 +0200396 ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200397 LY_CHECK_GOTO(ret, cleanup);
398
399 /* key with a valid value, remove from the set */
400 ly_set_rm_index(&key_set, i, NULL);
401 }
402 } else {
403 /* start skipping the member we are not interested in */
404 ret = lyjson_ctx_next(jsonctx, &status);
405 LY_CHECK_GOTO(ret, cleanup);
406 }
407 /* move to the next child */
408 while (status_count < jsonctx->status.count) {
409 ret = lyjson_ctx_next(jsonctx, &status);
410 LY_CHECK_GOTO(ret, cleanup);
411 }
412 }
413 }
414
415 if (key_set.count) {
416 /* some keys are missing/did not validate */
417 ret = LY_ENOT;
418 }
419
420cleanup:
421 ly_set_erase(&key_set, NULL);
422 return ret;
423}
424
425/**
426 * @brief Get the hint for the data type parsers according to the current JSON parser context.
427 *
428 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
429 * @param[in,out] status Pointer to the current context status,
430 * in some circumstances the function manipulates with the context so the status is updated.
431 * @param[out] type_hint_p Pointer to the variable to store the result.
432 * @return LY_SUCCESS in case of success.
433 * @return LY_EINVAL in case of invalid context status not referring to a value.
434 */
435static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200436lydjson_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 +0200437{
438 *type_hint_p = 0;
439
440 if (*status_p == LYJSON_ARRAY) {
441 /* only [null] */
442 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200443 if (*status_p != LYJSON_NULL) {
Michal Vaskod1336a32022-10-25 15:00:20 +0200444 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON,
445 "Expected JSON name/value or special name/[null], but input data contains name/[%s].",
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200446 lyjson_token2str(*status_p));
447 return LY_EINVAL;
448 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200449
450 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200451 if (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED) {
452 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.",
453 lyjson_token2str(*status_p));
454 return LY_EINVAL;
455 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200456
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200457 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200458 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200459 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200460 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200461 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200462 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200463 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200464 } else if (*status_p == LYJSON_NULL) {
465 *type_hint_p = 0;
466 } else {
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200467 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p));
Radek Krejci1798aae2020-07-14 13:26:06 +0200468 return LY_EINVAL;
469 }
470
471 return LY_SUCCESS;
472}
473
474/**
475 * @brief Check in advance if the input data are parsable according to the provided @p snode.
476 *
477 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
478 * is naturally done when the data are really parsed.
479 *
480 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
481 * as before calling, despite it is necessary to process input data for checking.
482 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
483 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
484 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
485 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
486 * @return LY_EINVAL in case of invalid leaf JSON encoding
487 * and they are expected to be parsed as opaq nodes.
488 */
489static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200490lydjson_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 +0200491{
492 LY_ERR ret = LY_SUCCESS;
493 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
494 enum LYJSON_PARSER_STATUS status;
495
496 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200497
Michal Vasko32ac9942020-08-12 14:35:12 +0200498 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
499 /* can always be parsed as a data node if we have the schema node */
500 return LY_SUCCESS;
501 }
502
Michal Vasko85430702021-07-21 14:29:56 +0200503 /* backup parser */
504 lyjson_ctx_backup(jsonctx);
505 status = lyjson_ctx_status(jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200506
Michal Vasko85430702021-07-21 14:29:56 +0200507 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200508 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
509 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200510 switch (snode->nodetype) {
511 case LYS_LEAFLIST:
512 case LYS_LEAF:
513 /* value may not be valid in which case we parse it as an opaque node */
514 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
515 if (ret) {
516 break;
517 }
518
Radek Krejci8df109d2021-04-23 12:19:08 +0200519 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200520 ret = LY_ENOT;
521 }
522 break;
523 case LYS_LIST:
524 /* lists may not have all its keys */
525 if (lydjson_check_list(jsonctx, snode)) {
Michal Vasko86fdf342022-12-01 11:08:29 +0100526 /* invalid list, parse as opaque if it misses/has invalid some keys */
Radek Krejci1798aae2020-07-14 13:26:06 +0200527 ret = LY_ENOT;
528 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200529 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200530 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200531 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200532 status = lyjson_ctx_status(jsonctx, 0);
533 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
534 }
535
Michal Vasko85430702021-07-21 14:29:56 +0200536 /* restore parser */
537 lyjson_ctx_restore(jsonctx);
538
Radek Krejci1798aae2020-07-14 13:26:06 +0200539 return ret;
540}
541
542/**
543 * @brief Join the forward-referencing metadata with their target data nodes.
544 *
545 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
546 *
547 * @param[in] lydctx JSON data parser context.
548 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
549 * as a starting point to search for the metadata's target data node
550 * @return LY_SUCCESS on success
551 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
552 */
553static LY_ERR
554lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
555{
556 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200557 struct lyd_node *node, *attr, *next, *meta_iter;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200558 struct lysc_ext_instance *ext;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200559 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200560 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100561 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200562
563 /* finish linking metadata */
564 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200565 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200566 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200567 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200568 const char *name, *prefix;
569 size_t name_len, prefix_len;
570 const struct lysc_node *snode;
571
Michal Vaskoad92b672020-11-12 13:11:31 +0100572 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200573 /* not an opaq metadata node */
574 continue;
575 }
576
Radek Krejciddace2c2021-01-08 11:30:56 +0100577 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100578 log_location_items++;
579
Michal Vaskoad92b672020-11-12 13:11:31 +0100580 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200581 /* metas' names are stored in dictionary, so checking pointers must works */
582 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100583 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200584 instance = 1;
585 } else {
586 instance++;
587 }
588
aPiecek5057c2e2021-05-17 10:28:03 +0200589 /* find the corresponding data node */
590 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200591 if (!node->schema) {
592 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100593 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200594 continue;
595 }
596
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200597 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100598 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
599 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200600 ret = LY_EVALID;
601 goto cleanup;
602 }
603
604 /* match */
605 match++;
606 if (match != instance) {
607 continue;
608 }
609
610 LY_LIST_FOR(meta_container->child, meta_iter) {
611 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200612 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100613
Michal Vaskoad92b672020-11-12 13:11:31 +0100614 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
615 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200616 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100617 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200618 LY_CHECK_GOTO(ret, cleanup);
619 }
620
621 /* done */
622 break;
623 } else {
624 /* this is the second time we are resolving the schema node, so it must succeed,
625 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100626 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
627 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200628 assert(is_attr);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200629 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 +0200630
631 if (snode != node->schema) {
632 continue;
633 }
634
635 /* match */
636 match++;
637 if (match != instance) {
638 continue;
639 }
640
641 LY_LIST_FOR(meta_container->child, meta_iter) {
642 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200643 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200644 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200645
Michal Vaskoad92b672020-11-12 13:11:31 +0100646 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200647 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200648 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100649 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100650 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200651 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100652 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200653 if (meta->name.prefix) {
654 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
655 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
656 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
657 meta->name.name);
658 } else {
659 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
660 meta->name.name);
661 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200662 ret = LY_EVALID;
663 goto cleanup;
664 }
665 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200666
Michal Vasko8cc3f662022-03-29 11:25:51 +0200667 /* add/correct flags */
668 ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, ext);
669 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200670 break;
671 }
672 }
673
674 if (match != instance) {
675 /* there is no corresponding data node for the metadata */
676 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100677 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
678 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200679 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200680 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100681 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
682 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200683 }
684 ret = LY_EVALID;
685 } else {
686 /* remove the opaq attr */
687 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200688 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200689 }
690 lyd_free_tree(attr);
691 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100692
Radek Krejciddace2c2021-01-08 11:30:56 +0100693 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100694 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200695 }
696
697cleanup:
698 lydict_remove(lydctx->jsonctx->ctx, prev);
699
Radek Krejciddace2c2021-01-08 11:30:56 +0100700 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200701 return ret;
702}
703
704/**
705 * @brief Parse a metadata member.
706 *
707 * @param[in] lydctx JSON data parser context.
708 * @param[in] snode Schema node of the metadata parent.
709 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
710 * so the data node does not exists. In such a case the metadata is stored in the context for the later
711 * processing by lydjson_metadata_finish().
712 * @return LY_SUCCESS on success
713 * @return Various LY_ERR values in case of failure.
714 */
715static LY_ERR
716lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
717{
718 LY_ERR ret = LY_SUCCESS;
719 enum LYJSON_PARSER_STATUS status;
720 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200721 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200722 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200723 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200724 size_t name_len, prefix_len = 0;
725 struct lys_module *mod;
Radek Krejci1798aae2020-07-14 13:26:06 +0200726 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200727 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200728 struct lyd_node *prev = node;
Michal Vasko09d13b32022-12-01 13:49:50 +0100729 uint32_t instance = 0, val_hints;
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 uint16_t nodetype;
731
732 assert(snode || node);
733
734 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
Michal Vasko66c16312022-12-01 10:37:43 +0100735 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200736
737 /* move to the second item in the name/X pair */
738 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
739 LY_CHECK_GOTO(ret, cleanup);
740
741 /* check attribute encoding */
742 switch (nodetype) {
743 case LYS_LEAFLIST:
744 expected = "@name/array of objects/nulls";
745
746 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
747
748next_entry:
749 instance++;
750
Michal Vasko57c8d432022-11-30 15:35:56 +0100751 /* move into array/next entry */
Radek Krejci1798aae2020-07-14 13:26:06 +0200752 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
753 LY_CHECK_GOTO(ret, cleanup);
754
755 if (status == LYJSON_ARRAY_CLOSED) {
Michal Vasko57c8d432022-11-30 15:35:56 +0100756 /* no more metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200757 goto cleanup;
758 }
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);
Radek Krejci1798aae2020-07-14 13:26:06 +0200764 ret = LY_EVALID;
765 goto cleanup;
766 }
767
768 if (status == LYJSON_NULL) {
769 /* continue with the next entry in the leaf-list array */
770 prev = node;
771 node = node->next;
772 goto next_entry;
773 }
774 break;
775 case LYS_LEAF:
776 case LYS_ANYXML:
777 expected = "@name/object";
778
aPiecek5b115fc2021-05-24 14:32:42 +0200779 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200780 break;
781 case LYS_CONTAINER:
782 case LYS_LIST:
783 case LYS_ANYDATA:
784 case LYS_NOTIF:
785 case LYS_ACTION:
786 case LYS_RPC:
787 in_parent = 1;
788 expected = "@/object";
789 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
790 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200791 default:
Michal Vasko66c16312022-12-01 10:37:43 +0100792 LOGINT(ctx);
793 ret = LY_EINT;
794 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200795 }
796
797 /* process all the members inside a single metadata object */
798 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +0200799 while (status != LYJSON_OBJECT_CLOSED) {
Michal Vaskoebcd9a92022-05-23 08:06:34 +0200800 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
801
aPiecekd2c39372021-06-16 14:03:12 +0200802 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 +0100803 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200804
Michal Vasko69d0ca12021-07-23 10:22:03 +0200805 if (!name_len) {
806 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
807 ret = LY_EVALID;
808 goto cleanup;
809 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100810 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100811 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200812 ret = LY_EVALID;
813 goto cleanup;
814 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100815 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100816 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200817 ret = LY_EVALID;
818 goto cleanup;
819 }
820
821 /* get the element module */
822 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
823 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100824 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100825 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 +0100826 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200827 ret = LY_EVALID;
828 goto cleanup;
829 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100830 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200831 /* skip element with children */
832 ret = lydjson_data_skip(lydctx->jsonctx);
833 LY_CHECK_GOTO(ret, cleanup);
834 status = lyjson_ctx_status(lydctx->jsonctx, 0);
835 /* end of the item */
836 continue;
837 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100838 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 }
840
841 /* get the value */
Michal Vasko09d13b32022-12-01 13:49:50 +0100842 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
843 LY_CHECK_GOTO(ret, cleanup);
844
845 /* get value hints */
846 ret = lydjson_value_type_hint(lydctx, &status, &val_hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200847 LY_CHECK_GOTO(ret, cleanup);
848
849 if (node->schema) {
850 /* create metadata */
Michal Vaskob4043552022-12-01 10:11:55 +0100851 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, name, name_len, lydctx->jsonctx->value,
Michal Vasko09d13b32022-12-01 13:49:50 +0100852 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, val_hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200853 LY_CHECK_GOTO(ret, cleanup);
854
855 /* add/correct flags */
Michal Vaskob4043552022-12-01 10:11:55 +0100856 ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200857 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200858 } else {
859 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200860 const char *module_name;
861 size_t module_name_len;
862
863 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
864
Radek Krejci1798aae2020-07-14 13:26:06 +0200865 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100866 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
867 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Michal Vasko09d13b32022-12-01 13:49:50 +0100868 LY_VALUE_JSON, NULL, val_hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200869 LY_CHECK_GOTO(ret, cleanup);
870 }
871 /* next member */
872 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
873 LY_CHECK_GOTO(ret, cleanup);
aPiecekde3c6922021-05-24 14:42:10 +0200874 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200875 }
876
877 if (nodetype == LYS_LEAFLIST) {
878 /* continue by processing another metadata object for the following
Michal Vaskob4043552022-12-01 10:11:55 +0100879 * leaf-list instance since they are always instantiated in JSON array */
Radek Krejci1798aae2020-07-14 13:26:06 +0200880 prev = node;
881 node = node->next;
882 goto next_entry;
883 }
884
Radek Krejci5813c362021-01-05 10:51:57 +0100885 /* success */
886 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200887
888representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100889 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200890 "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 +0100891 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
892 in_parent ? "" : "name");
Radek Krejci2efc45b2020-12-22 16:25:44 +0100893 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100894
895cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200896 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100897 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100898 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200899}
900
901/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200902 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
903 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200904 *
905 * @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 +0200906 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
907 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
908 * @param[in] last If set, always insert at the end.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200909 * @param[in] ext Extension instance of @p node_p, if any.
Radek Krejci1798aae2020-07-14 13:26:06 +0200910 */
911static void
Michal Vasko8cc3f662022-03-29 11:25:51 +0200912lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last,
913 struct lysc_ext_instance *ext)
Radek Krejci1798aae2020-07-14 13:26:06 +0200914{
Michal Vaskod027f382023-02-10 09:13:25 +0100915 if (!*node_p) {
916 return;
917 }
918
919 /* insert, keep first pointer correct */
920 if (ext) {
921 lyplg_ext_insert(parent, *node_p);
922 } else {
923 lyd_insert_node(parent, first_p, *node_p, last);
924 }
925 if (first_p) {
926 if (parent) {
927 *first_p = lyd_child(parent);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200928 } else {
Michal Vaskod027f382023-02-10 09:13:25 +0100929 while ((*first_p)->prev->next) {
930 *first_p = (*first_p)->prev;
Radek Krejci1798aae2020-07-14 13:26:06 +0200931 }
932 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200933 }
Michal Vaskod027f382023-02-10 09:13:25 +0100934 *node_p = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200935}
936
aPiecek4bf47212021-05-27 10:55:47 +0200937/**
938 * @brief Wrapper for ::lyd_create_opaq().
939 *
940 * @param[in] lydctx JSON data parser context.
941 * @param[in] name Name of the opaq node to create.
942 * @param[in] name_len Length of the @p name string.
943 * @param[in] prefix Prefix of the opaq node to create.
944 * @param[in] prefix_len Length of the @p prefx string.
945 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
946 * but must be set if @p first is not.
947 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
948 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
949 * @param[out] node_p Pointer to the created opaq node.
950 * @return LY_ERR value.
951 */
952static LY_ERR
953lydjson_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 +0100954 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +0200955{
956 LY_ERR ret = LY_SUCCESS;
957 const char *value = NULL, *module_name;
958 size_t value_len = 0, module_name_len = 0;
959 ly_bool dynamic = 0;
960 uint32_t type_hint = 0;
961
962 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
963 /* prepare for creating opaq node with a value */
964 value = lydctx->jsonctx->value;
965 value_len = lydctx->jsonctx->value_len;
966 dynamic = lydctx->jsonctx->dynamic;
967 lydctx->jsonctx->dynamic = 0;
968
969 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
970 }
971
972 /* create node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100973 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +0200974 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
975 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
976 if (dynamic) {
977 free((char *)value);
978 }
979
980 return ret;
981}
982
Michal Vaskoe0665742021-02-11 11:08:44 +0100983static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
984 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200985
986/**
987 * @brief Parse opaq node from the input.
988 *
989 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
990 *
991 * @param[in] lydctx JSON data parser context.
992 * @param[in] name Name of the opaq node to create.
993 * @param[in] name_len Length of the @p name string.
994 * @param[in] prefix Prefix of the opaq node to create.
995 * @param[in] prefix_len Length of the @p prefx string.
996 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
997 * but must be set if @p first is not.
998 * @param[in,out] status_p Pointer to the current status of the parser context,
999 * since the function manipulates with the context and process the input, the status can be updated.
1000 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1001 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1002 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1003 * @param[out] node_p Pointer to the created opaq node.
1004 * @return LY_ERR value.
1005 */
1006static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001007lydjson_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 +01001008 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1009 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001010{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001011 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 +02001012
Michal Vasko1a85d332021-08-27 10:35:28 +02001013 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1014 /* special array null value */
1015 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1016
1017 /* must be the only item */
1018 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
1019 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1020 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
1021 return LY_EVALID;
1022 }
1023
1024 goto finish;
1025 }
1026
aPiecekdbb8baf2021-05-27 11:01:51 +02001027 while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001028 /* process another instance of the same node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001029
Michal Vasko69730152020-10-09 16:30:07 +02001030 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
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 Vasko1a85d332021-08-27 10:35:28 +02001035 while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001036 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +02001037 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
1038 }
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 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001048
1049 /* continue with the next instance */
aPiecekdbb8baf2021-05-27 11:01:51 +02001050 assert(node_p);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001051 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001052 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 +02001053 }
1054
1055 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
1056 /* process children */
1057 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
1058 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
1059 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001060 }
1061 }
1062
aPiecekdbb8baf2021-05-27 11:01:51 +02001063finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001064 /* finish linking metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001065 return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001066}
1067
1068/**
aPiecek6cee5d02021-05-12 10:32:00 +02001069 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1070 *
1071 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1072 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1073 * for ::lydjson_parse_opaq().
1074 *
1075 * @param[in] lydctx JSON data parser context.
1076 * @param[in] name Name of the opaq node to create.
1077 * @param[in] name_len Length of the @p name string.
1078 * @param[in] prefix Prefix of the opaq node to create.
1079 * @param[in] prefix_len Length of the @p prefx string.
1080 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1081 * but must be set if @p first is not.
1082 * @param[in,out] status_p Pointer to the current status of the parser context,
1083 * since the function manipulates with the context and process the input, the status can be updated.
1084 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1085 * @param[out] node_p Pointer to the created opaq node.
1086 * @return LY_ERR value.
1087 */
1088static LY_ERR
1089lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001090 const char *prefix, size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001091 struct lyd_node **first_p, struct lyd_node **node_p)
1092{
1093 enum LYJSON_PARSER_STATUS status_inner = 0;
1094
1095 /* move to the second item in the name/X pair */
1096 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1097
1098 if (*status_p == LYJSON_ARRAY) {
1099 /* move into the array */
1100 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1101 } else {
1102 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1103 status_inner = LYJSON_ERROR;
1104 }
1105
1106 if (status_inner == LYJSON_ERROR) {
1107 status_inner = *status_p;
1108 }
1109
1110 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001111 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1112 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001113
1114 return LY_SUCCESS;
1115}
1116
1117/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001118 * @brief Process the attribute container (starting by @)
1119 *
1120 * @param[in] lydctx JSON data parser context.
1121 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1122 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1123 * @param[in] name Name of the opaq node to create.
1124 * @param[in] name_len Length of the @p name string.
1125 * @param[in] prefix Prefix of the opaq node to create.
1126 * @param[in] prefix_len Length of the @p prefx string.
1127 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1128 * but must be set if @p first is not.
1129 * @param[in,out] status_p Pointer to the current status of the parser context,
1130 * since the function manipulates with the context and process the input, the status can be updated.
1131 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1132 * @param[out] node_p Pointer to the created opaq node.
1133 * @return LY_ERR value.
1134 */
1135static LY_ERR
1136lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001137 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1138 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001139{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001140 LY_ERR r;
Michal Vaskodcad5c82022-12-01 16:16:58 +01001141 const char *opaq_name, *mod_name;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001142 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001143
Michal Vaskofceac692022-12-01 13:50:27 +01001144 if (!snode && !prefix) {
1145 /* set the prefix */
1146 if (parent) {
1147 lydjson_get_node_prefix(parent, NULL, 0, &prefix, &prefix_len);
1148 } else {
1149 prefix = "";
1150 prefix_len = 0;
1151 }
1152 }
1153
1154 /* parse as an attribute to a (opaque) node */
1155 if (!attr_node) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001156 /* try to find the instance */
Michal Vaskofceac692022-12-01 13:50:27 +01001157 LY_LIST_FOR(*first_p, attr_node) {
1158 if (snode) {
1159 if (attr_node->schema) {
1160 if (attr_node->schema == snode) {
1161 break;
1162 }
1163 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001164 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1165 if (!strcmp(LYD_NAME(attr_node), snode->name) && mod_name && !strcmp(mod_name, snode->module->name)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001166 break;
1167 }
1168 }
1169 } else {
1170 if (attr_node->schema) {
1171 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) &&
1172 !ly_strncmp(attr_node->schema->module->name, prefix, prefix_len)) {
1173 break;
1174 }
1175 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001176 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1177 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) && mod_name &&
1178 !ly_strncmp(mod_name, prefix, prefix_len)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001179 break;
1180 }
1181 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001182 }
1183 }
1184 }
1185 if (!attr_node) {
1186 /* parse just as an opaq node with the name beginning with @,
1187 * later we have to check that it belongs to a standard node
1188 * and it is supposed to be converted to a metadata */
1189 uint32_t prev_opts;
1190
Radek Krejci1798aae2020-07-14 13:26:06 +02001191 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001192 prev_opts = lydctx->parse_opts;
1193 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1194 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001195
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001196 opaq_name = prefix ? prefix - 1 : name - 1;
1197 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1198 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 +02001199
1200 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001201 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001202 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001203 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001204 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001205 }
1206
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001207 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001208}
1209
1210/**
Michal Vasko76096ec2022-02-24 16:06:16 +01001211 * @brief Parse a single anydata/anyxml node.
1212 *
1213 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1214 * as before calling, despite it is necessary to process input data for checking.
1215 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001216 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko76096ec2022-02-24 16:06:16 +01001217 * @param[in,out] status JSON parser status, is updated.
1218 * @param[out] node Parsed data (or opaque) node.
1219 * @return LY_SUCCESS if a node was successfully parsed,
1220 * @return LY_ENOT in case of invalid JSON encoding,
1221 * @return LY_ERR on other errors.
1222 */
1223static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +02001224lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1225 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko76096ec2022-02-24 16:06:16 +01001226{
Michal Vaskod027f382023-02-10 09:13:25 +01001227 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +01001228 uint32_t prev_parse_opts, prev_int_opts;
1229 struct ly_in in_start;
Michal Vasko7ca33252022-12-21 09:30:32 +01001230 char *val = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001231 struct lyd_node *tree = NULL;
1232
1233 assert(snode->nodetype & LYD_NODE_ANY);
1234
1235 /* status check according to allowed JSON types */
1236 if (snode->nodetype == LYS_ANYXML) {
1237 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY) &&
Michal Vasko7ca33252022-12-21 09:30:32 +01001238 (*status != LYJSON_ARRAY_EMPTY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
1239 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001240 } else {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001241 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001242 }
1243
1244 /* create any node */
1245 switch (*status) {
1246 case LYJSON_OBJECT:
1247 /* parse any data tree with correct options, first backup the current options and then make the parser
1248 * process data as opaq nodes */
1249 prev_parse_opts = lydctx->parse_opts;
1250 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001251 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vasko76096ec2022-02-24 16:06:16 +01001252 prev_int_opts = lydctx->int_opts;
1253 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
1254
1255 /* process the anydata content */
1256 while (*status != LYJSON_OBJECT_CLOSED) {
Michal Vaskod027f382023-02-10 09:13:25 +01001257 r = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
1258 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1259
Michal Vasko76096ec2022-02-24 16:06:16 +01001260 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1261 }
1262
1263 /* restore parser options */
1264 lydctx->parse_opts = prev_parse_opts;
1265 lydctx->int_opts = prev_int_opts;
1266
1267 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001268 r = lydjson_metadata_finish(lydctx, &tree);
1269 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001270
Michal Vaskod027f382023-02-10 09:13:25 +01001271 r = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
1272 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001273 break;
Michal Vasko7ca33252022-12-21 09:30:32 +01001274 case LYJSON_ARRAY_EMPTY:
1275 /* store the empty array */
1276 if (asprintf(&val, "[]") == -1) {
1277 LOGMEM(lydctx->jsonctx->ctx);
1278 return LY_EMEM;
1279 }
Michal Vaskod027f382023-02-10 09:13:25 +01001280 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1281 LY_CHECK_ERR_GOTO(r, rc = r, val_err);
Michal Vasko7ca33252022-12-21 09:30:32 +01001282 break;
Michal Vasko76096ec2022-02-24 16:06:16 +01001283 case LYJSON_ARRAY:
1284 /* skip until the array end */
1285 in_start = *lydctx->jsonctx->in;
1286 LY_CHECK_RET(lydjson_data_skip(lydctx->jsonctx));
1287
1288 /* make a copy of the whole array and store it */
1289 if (asprintf(&val, "[%.*s", (int)(lydctx->jsonctx->in->current - in_start.current), in_start.current) == -1) {
1290 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;
1325 case LYJSON_OBJECT_EMPTY:
1326 /* empty object */
Michal Vaskod027f382023-02-10 09:13:25 +01001327 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
1328 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001329 break;
1330 default:
1331 LOGINT_RET(lydctx->jsonctx->ctx);
1332 }
1333
Michal Vaskod027f382023-02-10 09:13:25 +01001334cleanup:
1335 return rc;
Michal Vasko7ca33252022-12-21 09:30:32 +01001336
1337val_err:
1338 free(val);
1339 return rc;
Michal Vasko76096ec2022-02-24 16:06:16 +01001340}
1341
1342/**
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001343 * @brief Parse a single instance of an inner node.
1344 *
1345 * @param[in] lydctx JSON data parser context.
1346 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1347 * @param[in] ext Extension instance of @p snode, if any.
1348 * @param[in,out] status JSON parser status, is updated.
1349 * @param[out] node Parsed data (or opaque) node.
1350 * @return LY_SUCCESS if a node was successfully parsed,
1351 * @return LY_ENOT in case of invalid JSON encoding,
1352 * @return LY_ERR on other errors.
1353 */
1354static LY_ERR
1355lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1356 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
1357{
Michal Vaskod027f382023-02-10 09:13:25 +01001358 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001359 uint32_t prev_parse_opts = lydctx->parse_opts;
1360
1361 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
1362
1363 /* create inner node */
1364 LY_CHECK_RET(lyd_create_inner(snode, node));
1365
1366 /* use it for logging */
1367 LOG_LOCSET(NULL, *node, NULL, NULL);
1368
1369 if (ext) {
1370 /* only parse these extension data and validate afterwards */
1371 lydctx->parse_opts |= LYD_PARSE_ONLY;
1372 }
1373
1374 /* process children */
1375 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
Michal Vaskod027f382023-02-10 09:13:25 +01001376 r = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
1377 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1378
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001379 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1380 }
1381
1382 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001383 r = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
1384 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001385
1386 if (snode->nodetype == LYS_LIST) {
1387 /* check all keys exist */
Michal Vaskod027f382023-02-10 09:13:25 +01001388 r = lyd_parse_check_keys(*node);
1389 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001390 }
1391
1392 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1393 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +01001394 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
1395 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001396
1397 /* add any missing default children */
Michal Vaskod027f382023-02-10 09:13:25 +01001398 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
1399 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1400 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001401 }
1402
1403cleanup:
1404 lydctx->parse_opts = prev_parse_opts;
1405 LOG_LOCBACK(0, 1, 0, 0);
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;
Radek Krejci1798aae2020-07-14 13:26:06 +02001518 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
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);
1529 assert(status == LYJSON_OBJECT);
1530
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
Radek Krejci1798aae2020-07-14 13:26:06 +02001535 /* process the node name */
aPiecekd2c39372021-06-16 14:03:12 +02001536 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 +01001537 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001538
Michal Vaskocabe0702020-08-12 10:14:36 +02001539 if (!is_meta || name_len || prefix_len) {
1540 /* get the schema node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001541 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001542 if (r == LY_ENOT) {
1543 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001544 goto cleanup;
Michal Vasko514f6982023-02-17 09:07:39 +01001545 } else if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1546 rc = r;
1547
1548 /* skip the invalid data */
1549 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1550 rc = r;
1551 }
1552 goto cleanup;
1553 } else if (r) {
1554 /* error */
1555 rc = r;
1556 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001557 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001558
Michal Vaskocabe0702020-08-12 10:14:36 +02001559 if (!snode) {
1560 /* we will not be parsing it as metadata */
1561 is_meta = 0;
1562 }
1563 }
1564
1565 if (is_meta) {
1566 /* parse as metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001567 if (!name_len && !prefix_len && !parent) {
1568 LOGVAL(ctx, LYVE_SYNTAX_JSON,
1569 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1570 r = LY_EVALID;
1571 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1572 } else if (!name_len && !prefix_len) {
Michal Vaskocabe0702020-08-12 10:14:36 +02001573 /* parent's metadata without a name - use the schema from the parent */
Michal Vaskoe0665742021-02-11 11:08:44 +01001574 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001575 snode = attr_node->schema;
1576 }
Michal Vaskod027f382023-02-10 09:13:25 +01001577 r = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001578 first_p, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001579 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001580 } else if (!snode) {
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001581 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
1582 /* skip element with children */
1583 r = lydjson_data_skip(lydctx->jsonctx);
1584 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1585 } else {
1586 /* parse as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001587
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001588 /* opaq node cannot have an empty string as the name. */
1589 if (name_len == 0) {
Michal Vaskobfebf662023-02-17 09:07:22 +01001590 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "JSON object member name cannot be a zero-length string.");
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001591 r = LY_EVALID;
1592 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1593 }
1594
1595 /* move to the second item in the name/X pair and parse opaq */
1596 r = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node);
1597 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
aPiecekb7b29e62021-05-11 10:02:43 +02001598 }
Michal Vaskocabe0702020-08-12 10:14:36 +02001599 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001600 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001601
1602 /* move to the second item in the name/X pair */
Michal Vaskod027f382023-02-10 09:13:25 +01001603 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1604 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001605
Michal Vasko7ca33252022-12-21 09:30:32 +01001606 /* set expected representation */
1607 switch (snode->nodetype) {
1608 case LYS_LEAFLIST:
1609 expected = "name/array of values";
1610 break;
1611 case LYS_LIST:
1612 expected = "name/array of objects";
1613 break;
1614 case LYS_LEAF:
1615 if (status == LYJSON_ARRAY) {
1616 expected = "name/[null]";
1617 } else {
1618 expected = "name/value";
1619 }
1620 break;
1621 case LYS_CONTAINER:
1622 case LYS_NOTIF:
1623 case LYS_ACTION:
1624 case LYS_RPC:
1625 case LYS_ANYDATA:
1626 expected = "name/object";
1627 break;
1628 case LYS_ANYXML:
1629 if (status == LYJSON_ARRAY) {
1630 expected = "name/array";
1631 } else {
1632 expected = "name/value";
1633 }
1634 break;
1635 }
1636
1637 /* check the representation according to the nodetype and then continue with the content */
Radek Krejci1798aae2020-07-14 13:26:06 +02001638 switch (snode->nodetype) {
1639 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001640 case LYS_LIST:
Michal Vasko4e26adc2022-03-30 11:01:16 +02001641 if (status == LYJSON_ARRAY_EMPTY) {
1642 /* no instances, skip */
1643 break;
1644 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001645 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1646
1647 /* move into array */
Michal Vaskod027f382023-02-10 09:13:25 +01001648 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1649 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001650
Michal Vasko32ac9942020-08-12 14:35:12 +02001651 /* process all the values/objects */
1652 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001653 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001654 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001655 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001656 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001657 }
Michal Vaskod027f382023-02-10 09:13:25 +01001658 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1659
Michal Vasko8cc3f662022-03-29 11:25:51 +02001660 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Radek Krejci1798aae2020-07-14 13:26:06 +02001661
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001662 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001663 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1664 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001665 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001666
1667 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001668 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001669 case LYS_CONTAINER:
1670 case LYS_NOTIF:
1671 case LYS_ACTION:
1672 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001673 case LYS_ANYDATA:
1674 case LYS_ANYXML:
Michal Vasko32ac9942020-08-12 14:35:12 +02001675 /* process the value/object */
Michal Vaskod027f382023-02-10 09:13:25 +01001676 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001677 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001678 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001679 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001680 }
Michal Vaskod027f382023-02-10 09:13:25 +01001681 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001682
Michal Vasko32ac9942020-08-12 14:35:12 +02001683 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001684 /* rememeber the RPC/action/notification */
1685 lydctx->op_node = node;
1686 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001687 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001688 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001689 }
1690
Michal Vasko32ac9942020-08-12 14:35:12 +02001691 /* finally connect the parsed node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001692 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Michal Vaskoe0665742021-02-11 11:08:44 +01001693
1694 /* rememeber a successfully parsed node */
Michal Vasko4e26adc2022-03-30 11:01:16 +02001695 if (parsed && node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001696 ly_set_add(parsed, node, 1, NULL);
1697 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001698
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001699 if (!parse_subtree) {
1700 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001701 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1702 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001703 }
1704
Radek Krejci5813c362021-01-05 10:51:57 +01001705 /* success */
1706 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001707
1708representation_error:
Michal Vaskobfebf662023-02-17 09:07:22 +01001709 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as name/%s.",
1710 expected, lys_nodetype2str(snode->nodetype), snode->name, lyjson_token2str(status));
Michal Vaskod027f382023-02-10 09:13:25 +01001711 rc = LY_EVALID;
1712 if (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) {
1713 /* try to skip the invalid data */
Michal Vasko560f9de2023-02-10 09:56:01 +01001714 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1715 rc = r;
1716 }
Michal Vaskod027f382023-02-10 09:13:25 +01001717 }
Radek Krejci5813c362021-01-05 10:51:57 +01001718
1719cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001720 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001721 lyd_free_tree(node);
Michal Vaskod027f382023-02-10 09:13:25 +01001722 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +02001723}
1724
1725/**
1726 * @brief Common start of JSON parser processing different types of the input data.
1727 *
1728 * @param[in] ctx libyang context
1729 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001730 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1731 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001732 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001733 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001734 * @return LY_ERR value.
1735 */
1736static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001737lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001738 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001739{
1740 LY_ERR ret = LY_SUCCESS;
1741 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001742 size_t i;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001743 ly_bool subtree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001744
Radek Krejcicd5f6222020-11-12 08:54:13 +01001745 assert(lydctx_p);
1746 assert(status);
1747
Radek Krejci1798aae2020-07-14 13:26:06 +02001748 /* init context */
1749 lydctx = calloc(1, sizeof *lydctx);
1750 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001751 lydctx->parse_opts = parse_opts;
1752 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001753 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001754
Radek Krejci284f31f2020-09-18 15:40:29 +02001755 /* starting top-level */
1756 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001757 if (in->current[i] == '\n') {
1758 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001759 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001760 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001761 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001762
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001763 subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0;
1764 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001765 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001766
Radek Krejcibb27df32020-11-13 15:39:16 +01001767 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001768 *lydctx_p = lydctx;
1769 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001770 } else {
1771 /* expecting top-level object */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001772 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001773 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001774 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001775 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001776 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001777}
1778
1779LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001780lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001781 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 +01001782 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001783{
Michal Vaskod027f382023-02-10 09:13:25 +01001784 LY_ERR r, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001785 struct lyd_json_ctx *lydctx = NULL;
1786 enum LYJSON_PARSER_STATUS status;
1787
Michal Vaskoe0665742021-02-11 11:08:44 +01001788 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1789 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001790
Radek Krejcicd5f6222020-11-12 08:54:13 +01001791 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001792
Michal Vaskoe0665742021-02-11 11:08:44 +01001793 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001794 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001795
1796 /* find the operation node if it exists already */
1797 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1798
Radek Krejci1798aae2020-07-14 13:26:06 +02001799 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001800 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
Michal Vaskod027f382023-02-10 09:13:25 +01001801 r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1802 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001803
1804 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001805
1806 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1807 break;
1808 }
1809 }
1810
1811 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1812 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1813 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001814 r = LY_EVALID;
1815 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001816 }
1817 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1818 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001819 r = LY_EVALID;
1820 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001821 }
1822
1823 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001824 r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1825 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001826
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001827 if (parse_opts & LYD_PARSE_SUBTREE) {
1828 /* check for a sibling object */
1829 assert(subtree_sibling);
1830 if (lydctx->jsonctx->in->current[0] == ',') {
1831 *subtree_sibling = 1;
1832
1833 /* move to the next object */
1834 ly_in_skip(lydctx->jsonctx->in, 1);
1835 } else {
1836 *subtree_sibling = 0;
1837 }
1838 }
1839
Radek Krejci1798aae2020-07-14 13:26:06 +02001840cleanup:
1841 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001842 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001843 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001844
Michal Vaskod027f382023-02-10 09:13:25 +01001845 if (rc && (!lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001846 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001847 } else {
1848 *lydctx_p = (struct lyd_ctx *)lydctx;
Michal Vaskof8ebf132022-11-21 14:06:48 +01001849
1850 /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1851 lyjson_ctx_free(lydctx->jsonctx);
1852 lydctx->jsonctx = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001853 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001854 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001855}