blob: b5edae907da29ee8afa5334eb46e604f60440830 [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko61ad1ff2022-02-10 15:48:39 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01005 * @brief JSON data parser for libyang
6 *
Michal Vasko09e04632023-03-22 14:34:10 +01007 * Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010017
Radek Krejci1798aae2020-07-14 13:26:06 +020018#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010020#include <stdlib.h>
21#include <string.h>
22
Michal Vaskobbdadda2022-01-06 11:40:10 +010023#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010024#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010025#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020027#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010028#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010029#include "ly_common.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
Radek Krejci1798aae2020-07-14 13:26:06 +0200164 while (node) {
165 if (node->schema) {
Michal Vaskob9cbb472023-05-19 12:15:40 +0200166 module_name = node->schema->module->name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200167 break;
168 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200169 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100170 if (onode->name.module_name) {
Michal Vaskob9cbb472023-05-19 12:15:40 +0200171 module_name = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200172 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100173 } else if (onode->name.prefix) {
Michal Vaskob9cbb472023-05-19 12:15:40 +0200174 module_name = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200175 break;
176 }
Michal Vasko9e685082021-01-29 14:49:09 +0100177 node = lyd_parent(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200178 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200179
Michal Vaskob9cbb472023-05-19 12:15:40 +0200180 *prefix_p = module_name;
181 *prefix_len_p = ly_strlen(module_name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200182 return LY_SUCCESS;
183}
184
185/**
Michal Vasko87c5d6b2023-02-14 15:27:41 +0100186 * @brief Skip the current JSON item (based on status).
187 *
188 * The JSON context is moved to the last "status" of the JSON item so to completely
189 * finish the skip, one more JSON context move is required.
Michal Vaskoa4af6252022-05-20 10:38:54 +0200190 *
Radek Krejci1798aae2020-07-14 13:26:06 +0200191 * @param[in] jsonctx JSON context with the input data to skip.
192 * @return LY_ERR value.
193 */
194static LY_ERR
195lydjson_data_skip(struct lyjson_ctx *jsonctx)
196{
197 enum LYJSON_PARSER_STATUS status, current;
Michal Vasko09e04632023-03-22 14:34:10 +0100198 uint32_t depth;
Radek Krejci1798aae2020-07-14 13:26:06 +0200199
Michal Vasko09e04632023-03-22 14:34:10 +0100200 status = lyjson_ctx_status(jsonctx);
201 depth = lyjson_ctx_depth(jsonctx);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200202
Michal Vasko09e04632023-03-22 14:34:10 +0100203 switch (status) {
204 case LYJSON_OBJECT:
205 ++depth;
Michal Vaskoa4af6252022-05-20 10:38:54 +0200206
Michal Vasko09e04632023-03-22 14:34:10 +0100207 /* skip until object closes */
208 do {
Michal Vasko4f04d762022-09-05 08:38:26 +0200209 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
Michal Vasko09e04632023-03-22 14:34:10 +0100210 } while ((current != LYJSON_OBJECT_CLOSED) || (depth != lyjson_ctx_depth(jsonctx)));
211 break;
212 case LYJSON_ARRAY:
213 ++depth;
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200214
Michal Vasko09e04632023-03-22 14:34:10 +0100215 /* skip until array closes */
216 do {
217 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
218 } while ((current != LYJSON_ARRAY_CLOSED) || (depth != lyjson_ctx_depth(jsonctx)));
219 break;
220 case LYJSON_OBJECT_NAME:
221 /* just get to the value */
222 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
223 if ((current == LYJSON_OBJECT) || (current == LYJSON_ARRAY)) {
224 LY_CHECK_RET(lydjson_data_skip(jsonctx));
Radek Krejci1798aae2020-07-14 13:26:06 +0200225 }
Michal Vasko09e04632023-03-22 14:34:10 +0100226 break;
227 default:
Michal Vaskob25cbfc2023-08-21 10:49:53 +0200228 /* no other status really expected, just go to next */
229 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
230 break;
Michal Vaskoa4af6252022-05-20 10:38:54 +0200231 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200232
233 return LY_SUCCESS;
234}
235
236/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100237 * @brief Get schema node corresponding to the input parameters.
238 *
239 * @param[in] lydctx JSON data parser context.
240 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
241 * @param[in] prefix Requested node's prefix (module name).
242 * @param[in] prefix_len Length of the @p prefix.
243 * @param[in] name Requested node's name.
244 * @param[in] name_len Length of the @p name.
245 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200246 * @param[out] snode Found schema node corresponding to the input parameters. If NULL, parse as an opaque node.
247 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100248 * @return LY_SUCCES on success.
249 * @return LY_ENOT if the whole object was parsed (skipped or as an extension).
250 * @return LY_ERR on error.
251 */
252static LY_ERR
253lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200254 size_t name_len, struct lyd_node *parent, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100255{
256 LY_ERR ret = LY_SUCCESS, r;
257 struct lys_module *mod = NULL;
258 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
259
Michal Vasko8cc3f662022-03-29 11:25:51 +0200260 *snode = NULL;
261 *ext = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100262
Michal Vasko8cc3f662022-03-29 11:25:51 +0200263 /* get the element module, prefer parent context because of extensions */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100264 if (prefix_len) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200265 mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100266 } else if (parent) {
267 if (parent->schema) {
268 mod = parent->schema->module;
269 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100270 } else if (!(lydctx->int_opts & LYD_INTOPT_ANY)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100271 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
272 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
273 ret = LY_EVALID;
274 goto cleanup;
275 }
276 if (!mod) {
277 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200278 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
279 if (r != LY_ENOT) {
280 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100281 ret = r;
282 goto cleanup;
283 }
284
285 /* unknown module */
286 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
287 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
288 ret = LY_EVALID;
289 goto cleanup;
290 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100291 }
292
293 /* get the schema node */
294 if (mod && (!parent || parent->schema)) {
295 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200296 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297 } else {
Michal Vaskoa878a892023-08-18 12:22:07 +0200298 *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200300 if (!*snode) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100301 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200302 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
303 if (r != LY_ENOT) {
304 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100305 ret = r;
306 goto cleanup;
307 }
308
309 /* unknown data node */
310 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
311 if (parent) {
312 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
313 (int)name_len, name, LYD_NAME(parent));
314 } else if (lydctx->ext) {
315 if (lydctx->ext->argument) {
316 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
317 "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
318 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
319 } else {
320 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
321 (int)name_len, name, lydctx->ext->def->name);
322 }
323 } else {
324 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
325 (int)name_len, name, mod->name);
326 }
327 ret = LY_EVALID;
328 goto cleanup;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100329 }
330 } else {
331 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200332 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100333 }
334 }
335
336cleanup:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100337 return ret;
338}
339
340/**
Michal Vasko583b4642023-05-25 10:39:34 +0200341 * @brief Get the hint for the data type parsers according to the current JSON parser context.
342 *
343 * @param[in] jsonctx JSON parser context. The context is supposed to be on a value.
344 * @param[in,out] status Pointer to the current context status,
345 * in some circumstances the function manipulates with the context so the status is updated.
346 * @param[out] type_hint_p Pointer to the variable to store the result.
347 * @return LY_SUCCESS in case of success.
348 * @return LY_EINVAL in case of invalid context status not referring to a value.
349 */
350static LY_ERR
351lydjson_value_type_hint(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p)
352{
353 *type_hint_p = 0;
354
355 if (*status_p == LYJSON_ARRAY) {
356 /* only [null] */
357 LY_CHECK_RET(lyjson_ctx_next(jsonctx, status_p));
358 if (*status_p != LYJSON_NULL) {
359 LOGVAL(jsonctx->ctx, LYVE_SYNTAX_JSON,
360 "Expected JSON name/value or special name/[null], but input data contains name/[%s].",
361 lyjson_token2str(*status_p));
362 return LY_EINVAL;
363 }
364
365 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
366 if (lyjson_ctx_status(jsonctx) != LYJSON_ARRAY_CLOSED) {
367 LOGVAL(jsonctx->ctx, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.",
368 lyjson_token2str(*status_p));
369 return LY_EINVAL;
370 }
371
372 *type_hint_p = LYD_VALHINT_EMPTY;
373 } else if (*status_p == LYJSON_STRING) {
374 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
375 } else if (*status_p == LYJSON_NUMBER) {
376 *type_hint_p = LYD_VALHINT_DECNUM;
377 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
378 *type_hint_p = LYD_VALHINT_BOOLEAN;
379 } else if (*status_p == LYJSON_NULL) {
380 *type_hint_p = 0;
381 } else {
382 LOGVAL(jsonctx->ctx, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p));
383 return LY_EINVAL;
384 }
385
386 return LY_SUCCESS;
387}
388
389/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200390 * @brief Check that the input data are parseable as the @p list.
391 *
392 * Checks for all the list's keys. Function does not revert the context state.
393 *
394 * @param[in] jsonctx JSON parser context.
395 * @param[in] list List schema node corresponding to the input data object.
396 * @return LY_SUCCESS in case the data are ok for the @p list
397 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
398 */
399static LY_ERR
400lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
401{
Michal Vasko09e04632023-03-22 14:34:10 +0100402 LY_ERR rc = LY_SUCCESS;
403 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200404 struct ly_set key_set = {0};
405 const struct lysc_node *snode;
Michal Vasko583b4642023-05-25 10:39:34 +0200406 uint32_t i, hints;
Radek Krejci1798aae2020-07-14 13:26:06 +0200407
408 assert(list && (list->nodetype == LYS_LIST));
Radek Krejci1798aae2020-07-14 13:26:06 +0200409
410 /* get all keys into a set (keys do not have if-features or anything) */
411 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100412 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Michal Vasko09e04632023-03-22 14:34:10 +0100413 rc = ly_set_add(&key_set, (void *)snode, 1, NULL);
414 LY_CHECK_GOTO(rc, cleanup);
415 }
416 if (!key_set.count) {
417 /* no keys */
418 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200419 }
420
Michal Vasko86fdf342022-12-01 11:08:29 +0100421 if (status == LYJSON_OBJECT) {
Michal Vasko09e04632023-03-22 14:34:10 +0100422 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200423 const char *name, *prefix;
424 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200425 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200426
427 /* match the key */
Michal Vasko09e04632023-03-22 14:34:10 +0100428 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
Michal Vaskob81b5eb2023-04-21 14:03:25 +0200429 if (status != LYJSON_OBJECT_NAME) {
430 break;
431 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200432 snode = NULL;
433 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
434
435 if (!is_attr && !prefix) {
436 for (i = 0; i < key_set.count; ++i) {
Michal Vaskod990ee52023-04-21 14:03:56 +0200437 if (!ly_strncmp(key_set.snodes[i]->name, name, name_len)) {
438 snode = key_set.snodes[i];
Radek Krejci1798aae2020-07-14 13:26:06 +0200439 break;
440 }
441 }
Michal Vasko09e04632023-03-22 14:34:10 +0100442
443 /* get the value */
444 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200445
446 if (snode) {
447 /* we have the key, validate the value */
Michal Vaskob677d8e2023-03-23 15:37:07 +0100448 if ((status < LYJSON_NUMBER) || (status > LYJSON_NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200449 /* not a terminal */
Michal Vasko09e04632023-03-22 14:34:10 +0100450 rc = LY_ENOT;
Radek Krejci1798aae2020-07-14 13:26:06 +0200451 goto cleanup;
452 }
453
Michal Vasko583b4642023-05-25 10:39:34 +0200454 rc = lydjson_value_type_hint(jsonctx, &status, &hints);
455 LY_CHECK_GOTO(rc, cleanup);
456 rc = ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL, hints);
Michal Vasko09e04632023-03-22 14:34:10 +0100457 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200458
459 /* key with a valid value, remove from the set */
460 ly_set_rm_index(&key_set, i, NULL);
461 }
Michal Vasko09e04632023-03-22 14:34:10 +0100462
463 /* next object */
464 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 } else {
Michal Vasko09e04632023-03-22 14:34:10 +0100466 /* skip the uninteresting object */
467 LY_CHECK_GOTO(rc = lydjson_data_skip(jsonctx), cleanup);
468 LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200469 }
Michal Vasko09e04632023-03-22 14:34:10 +0100470 } while (key_set.count && (status == LYJSON_OBJECT_NEXT));
Radek Krejci1798aae2020-07-14 13:26:06 +0200471 }
472
473 if (key_set.count) {
474 /* some keys are missing/did not validate */
Michal Vasko09e04632023-03-22 14:34:10 +0100475 rc = LY_ENOT;
Radek Krejci1798aae2020-07-14 13:26:06 +0200476 }
477
478cleanup:
479 ly_set_erase(&key_set, NULL);
Michal Vasko09e04632023-03-22 14:34:10 +0100480 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +0200481}
482
483/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200484 * @brief Check in advance if the input data are parsable according to the provided @p snode.
485 *
486 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
487 * is naturally done when the data are really parsed.
488 *
489 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
490 * as before calling, despite it is necessary to process input data for checking.
491 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
492 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
493 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
494 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
495 * @return LY_EINVAL in case of invalid leaf JSON encoding
496 * and they are expected to be parsed as opaq nodes.
497 */
498static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200499lydjson_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 +0200500{
501 LY_ERR ret = LY_SUCCESS;
502 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
503 enum LYJSON_PARSER_STATUS status;
Michal Vasko8a8fde02024-10-21 15:17:46 +0200504 uint32_t *prev_lo, temp_lo = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200505
506 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200507
Michal Vasko32ac9942020-08-12 14:35:12 +0200508 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
509 /* can always be parsed as a data node if we have the schema node */
510 return LY_SUCCESS;
511 }
512
Michal Vasko85430702021-07-21 14:29:56 +0200513 /* backup parser */
514 lyjson_ctx_backup(jsonctx);
Michal Vasko09e04632023-03-22 14:34:10 +0100515 status = lyjson_ctx_status(jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200516
Michal Vasko85430702021-07-21 14:29:56 +0200517 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200518 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
519 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200520 switch (snode->nodetype) {
521 case LYS_LEAFLIST:
522 case LYS_LEAF:
523 /* value may not be valid in which case we parse it as an opaque node */
Michal Vasko583b4642023-05-25 10:39:34 +0200524 if ((ret = lydjson_value_type_hint(jsonctx, &status, type_hint_p))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200525 break;
526 }
Michal Vasko8a8fde02024-10-21 15:17:46 +0200527
528 prev_lo = ly_temp_log_options(&temp_lo);
Michal Vasko583b4642023-05-25 10:39:34 +0200529 if (ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL, *type_hint_p)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200530 ret = LY_ENOT;
531 }
Michal Vasko8a8fde02024-10-21 15:17:46 +0200532 ly_temp_log_options(prev_lo);
Radek Krejci1798aae2020-07-14 13:26:06 +0200533 break;
534 case LYS_LIST:
535 /* lists may not have all its keys */
536 if (lydjson_check_list(jsonctx, snode)) {
Michal Vasko86fdf342022-12-01 11:08:29 +0100537 /* invalid list, parse as opaque if it misses/has invalid some keys */
Radek Krejci1798aae2020-07-14 13:26:06 +0200538 ret = LY_ENOT;
539 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200540 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200541 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200542 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vasko583b4642023-05-25 10:39:34 +0200543 ret = lydjson_value_type_hint(jsonctx, &status, type_hint_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200544 }
545
Michal Vasko85430702021-07-21 14:29:56 +0200546 /* restore parser */
547 lyjson_ctx_restore(jsonctx);
548
Radek Krejci1798aae2020-07-14 13:26:06 +0200549 return ret;
550}
551
552/**
553 * @brief Join the forward-referencing metadata with their target data nodes.
554 *
555 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
556 *
557 * @param[in] lydctx JSON data parser context.
558 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
559 * as a starting point to search for the metadata's target data node
560 * @return LY_SUCCESS on success
561 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
562 */
563static LY_ERR
564lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
565{
566 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200567 struct lyd_node *node, *attr, *next, *meta_iter;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200568 struct lysc_ext_instance *ext;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200569 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200570 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100571 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200572
573 /* finish linking metadata */
574 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200575 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200576 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200577 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 const char *name, *prefix;
579 size_t name_len, prefix_len;
580 const struct lysc_node *snode;
581
Michal Vaskoad92b672020-11-12 13:11:31 +0100582 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200583 /* not an opaq metadata node */
584 continue;
585 }
586
Michal Vasko7a266772024-01-23 11:02:38 +0100587 LOG_LOCSET(NULL, attr);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100588 log_location_items++;
589
Michal Vaskoad92b672020-11-12 13:11:31 +0100590 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200591 /* metas' names are stored in dictionary, so checking pointers must works */
592 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100593 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200594 instance = 1;
595 } else {
596 instance++;
597 }
598
aPiecek5057c2e2021-05-17 10:28:03 +0200599 /* find the corresponding data node */
600 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200601 if (!node->schema) {
602 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100603 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200604 continue;
605 }
606
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200607 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100608 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
609 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200610 ret = LY_EVALID;
611 goto cleanup;
612 }
613
614 /* match */
615 match++;
616 if (match != instance) {
617 continue;
618 }
619
620 LY_LIST_FOR(meta_container->child, meta_iter) {
621 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200622 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100623
Michal Vaskoad92b672020-11-12 13:11:31 +0100624 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
625 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200626 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100627 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200628 LY_CHECK_GOTO(ret, cleanup);
629 }
630
631 /* done */
632 break;
633 } else {
634 /* this is the second time we are resolving the schema node, so it must succeed,
635 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100636 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
637 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200638 assert(is_attr);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200639 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 +0200640
641 if (snode != node->schema) {
642 continue;
643 }
644
645 /* match */
646 match++;
647 if (match != instance) {
648 continue;
649 }
650
651 LY_LIST_FOR(meta_container->child, meta_iter) {
652 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200653 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200654 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200655
Michal Vaskoad92b672020-11-12 13:11:31 +0100656 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200657 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200658 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100659 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100660 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200661 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100662 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200663 if (meta->name.prefix) {
664 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
665 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
666 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
667 meta->name.name);
668 } else {
669 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
670 meta->name.name);
671 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200672 ret = LY_EVALID;
673 goto cleanup;
674 }
675 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200676
Michal Vasko8cc3f662022-03-29 11:25:51 +0200677 /* add/correct flags */
Michal Vasko0af83702024-10-08 13:42:19 +0200678 ret = lyd_parser_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200679 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200680 break;
681 }
682 }
683
684 if (match != instance) {
685 /* there is no corresponding data node for the metadata */
686 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100687 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
688 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200689 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200690 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100691 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
692 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200693 }
694 ret = LY_EVALID;
695 } else {
696 /* remove the opaq attr */
697 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200698 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200699 }
700 lyd_free_tree(attr);
701 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100702
Michal Vasko7a266772024-01-23 11:02:38 +0100703 LOG_LOCBACK(0, log_location_items);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100704 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200705 }
706
707cleanup:
708 lydict_remove(lydctx->jsonctx->ctx, prev);
709
Michal Vasko7a266772024-01-23 11:02:38 +0100710 LOG_LOCBACK(0, log_location_items);
Radek Krejci1798aae2020-07-14 13:26:06 +0200711 return ret;
712}
713
714/**
715 * @brief Parse a metadata member.
716 *
717 * @param[in] lydctx JSON data parser context.
718 * @param[in] snode Schema node of the metadata parent.
719 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
720 * so the data node does not exists. In such a case the metadata is stored in the context for the later
721 * processing by lydjson_metadata_finish().
722 * @return LY_SUCCESS on success
723 * @return Various LY_ERR values in case of failure.
724 */
725static LY_ERR
726lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
727{
Michal Vaskob25cbfc2023-08-21 10:49:53 +0200728 LY_ERR rc = LY_SUCCESS, r;
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 enum LYJSON_PARSER_STATUS status;
730 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200731 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200732 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200733 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 size_t name_len, prefix_len = 0;
735 struct lys_module *mod;
Radek Krejci1798aae2020-07-14 13:26:06 +0200736 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200737 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200738 struct lyd_node *prev = node;
Michal Vasko09d13b32022-12-01 13:49:50 +0100739 uint32_t instance = 0, val_hints;
Radek Krejci1798aae2020-07-14 13:26:06 +0200740 uint16_t nodetype;
741
742 assert(snode || node);
743
744 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
Michal Vasko7a266772024-01-23 11:02:38 +0100745 if (snode) {
746 LOG_LOCSET(snode, NULL);
747 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200748
749 /* move to the second item in the name/X pair */
Michal Vasko09e04632023-03-22 14:34:10 +0100750 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200751
752 /* check attribute encoding */
753 switch (nodetype) {
754 case LYS_LEAFLIST:
755 expected = "@name/array of objects/nulls";
Radek Krejci1798aae2020-07-14 13:26:06 +0200756 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
757
758next_entry:
Radek Krejci1798aae2020-07-14 13:26:06 +0200759 if (status == LYJSON_ARRAY_CLOSED) {
Michal Vasko57c8d432022-11-30 15:35:56 +0100760 /* no more metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200761 goto cleanup;
762 }
Michal Vasko09e04632023-03-22 14:34:10 +0100763
764 /* move into the array/next item */
765 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
766 instance++;
Michal Vasko57c8d432022-11-30 15:35:56 +0100767 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_NULL), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200768
Michal Vasko69730152020-10-09 16:30:07 +0200769 if (!node || (node->schema != prev->schema)) {
Michal Vasko21eaa392024-02-20 15:48:42 +0100770 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%" PRIu32
771 " of %s:%s to be coupled with metadata.", instance, prev->schema->module->name, prev->schema->name);
Michal Vasko09e04632023-03-22 14:34:10 +0100772 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200773 goto cleanup;
774 }
775
776 if (status == LYJSON_NULL) {
777 /* continue with the next entry in the leaf-list array */
778 prev = node;
779 node = node->next;
Michal Vasko09e04632023-03-22 14:34:10 +0100780
781 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200782 goto next_entry;
783 }
784 break;
785 case LYS_LEAF:
786 case LYS_ANYXML:
787 expected = "@name/object";
788
aPiecek5b115fc2021-05-24 14:32:42 +0200789 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200790 break;
791 case LYS_CONTAINER:
792 case LYS_LIST:
793 case LYS_ANYDATA:
794 case LYS_NOTIF:
795 case LYS_ACTION:
796 case LYS_RPC:
797 in_parent = 1;
798 expected = "@/object";
799 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
800 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200801 default:
Michal Vasko66c16312022-12-01 10:37:43 +0100802 LOGINT(ctx);
Michal Vasko09e04632023-03-22 14:34:10 +0100803 rc = LY_EINT;
Michal Vasko66c16312022-12-01 10:37:43 +0100804 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200805 }
806
807 /* process all the members inside a single metadata object */
808 assert(status == LYJSON_OBJECT);
Michal Vasko09e04632023-03-22 14:34:10 +0100809 do {
810 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Michal Vasko5a713272023-03-27 09:01:43 +0200811 LY_CHECK_GOTO(status != LYJSON_OBJECT_NAME, representation_error);
812
aPiecekd2c39372021-06-16 14:03:12 +0200813 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 +0100814 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200815
Michal Vasko69d0ca12021-07-23 10:22:03 +0200816 if (!name_len) {
817 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
Michal Vasko09e04632023-03-22 14:34:10 +0100818 rc = LY_EVALID;
Michal Vasko69d0ca12021-07-23 10:22:03 +0200819 goto cleanup;
820 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100821 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100822 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Michal Vasko09e04632023-03-22 14:34:10 +0100823 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200824 goto cleanup;
825 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100826 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100827 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Michal Vasko09e04632023-03-22 14:34:10 +0100828 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200829 goto cleanup;
830 }
831
832 /* get the element module */
833 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
834 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100835 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100836 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 +0100837 (int)prefix_len, prefix, (int)name_len, name);
Michal Vasko09e04632023-03-22 14:34:10 +0100838 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 goto cleanup;
840 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100841 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200842 /* skip element with children */
Michal Vasko09e04632023-03-22 14:34:10 +0100843 LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup);
844 status = lyjson_ctx_status(lydctx->jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200845 /* end of the item */
846 continue;
847 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100848 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200849 }
850
851 /* get the value */
Michal Vasko09e04632023-03-22 14:34:10 +0100852 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Michal Vasko09d13b32022-12-01 13:49:50 +0100853
854 /* get value hints */
Michal Vasko583b4642023-05-25 10:39:34 +0200855 LY_CHECK_GOTO(rc = lydjson_value_type_hint(lydctx->jsonctx, &status, &val_hints), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200856
857 if (node->schema) {
858 /* create metadata */
Michal Vasko09e04632023-03-22 14:34:10 +0100859 rc = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, name, name_len, lydctx->jsonctx->value,
Michal Vasko09d13b32022-12-01 13:49:50 +0100860 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, val_hints, node->schema);
Michal Vasko09e04632023-03-22 14:34:10 +0100861 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200862
863 /* add/correct flags */
Michal Vasko0af83702024-10-08 13:42:19 +0200864 rc = lyd_parser_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL);
Michal Vasko09e04632023-03-22 14:34:10 +0100865 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200866 } else {
867 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200868 const char *module_name;
869 size_t module_name_len;
870
871 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
872
Radek Krejci1798aae2020-07-14 13:26:06 +0200873 /* attr2 is always changed to the created attribute */
Michal Vasko09e04632023-03-22 14:34:10 +0100874 rc = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
Michal Vasko501af032020-11-11 20:27:44 +0100875 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Michal Vasko09d13b32022-12-01 13:49:50 +0100876 LY_VALUE_JSON, NULL, val_hints);
Michal Vasko09e04632023-03-22 14:34:10 +0100877 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200878 }
Michal Vasko09e04632023-03-22 14:34:10 +0100879
Radek Krejci1798aae2020-07-14 13:26:06 +0200880 /* next member */
Michal Vasko09e04632023-03-22 14:34:10 +0100881 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
882 } while (status == LYJSON_OBJECT_NEXT);
883 LY_CHECK_GOTO(status != LYJSON_OBJECT_CLOSED, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200884
885 if (nodetype == LYS_LEAFLIST) {
886 /* continue by processing another metadata object for the following
Michal Vaskob4043552022-12-01 10:11:55 +0100887 * leaf-list instance since they are always instantiated in JSON array */
Radek Krejci1798aae2020-07-14 13:26:06 +0200888 prev = node;
889 node = node->next;
Michal Vasko09e04632023-03-22 14:34:10 +0100890
891 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200892 goto next_entry;
893 }
894
Radek Krejci5813c362021-01-05 10:51:57 +0100895 /* success */
896 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200897
898representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100899 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200900 "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 +0100901 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
902 in_parent ? "" : "name");
Michal Vasko09e04632023-03-22 14:34:10 +0100903 rc = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100904
905cleanup:
Michal Vaskob25cbfc2023-08-21 10:49:53 +0200906 if ((rc == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
907 /* try to skip the invalid data */
908 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
909 rc = r;
910 }
911 }
aPiecek303fb252021-06-16 11:54:26 +0200912 free(dynamic_prefname);
Michal Vasko7a266772024-01-23 11:02:38 +0100913 LOG_LOCBACK(snode ? 1 : 0, 0);
Michal Vasko09e04632023-03-22 14:34:10 +0100914 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +0200915}
916
917/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200918 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
919 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200920 *
921 * @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 +0200922 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
923 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
924 * @param[in] last If set, always insert at the end.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200925 * @param[in] ext Extension instance of @p node_p, if any.
Radek Krejci1798aae2020-07-14 13:26:06 +0200926 */
927static void
Michal Vasko8cc3f662022-03-29 11:25:51 +0200928lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last,
929 struct lysc_ext_instance *ext)
Radek Krejci1798aae2020-07-14 13:26:06 +0200930{
Michal Vaskod027f382023-02-10 09:13:25 +0100931 if (!*node_p) {
932 return;
933 }
934
935 /* insert, keep first pointer correct */
936 if (ext) {
937 lyplg_ext_insert(parent, *node_p);
938 } else {
939 lyd_insert_node(parent, first_p, *node_p, last);
940 }
941 if (first_p) {
942 if (parent) {
943 *first_p = lyd_child(parent);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200944 } else {
Michal Vaskod027f382023-02-10 09:13:25 +0100945 while ((*first_p)->prev->next) {
946 *first_p = (*first_p)->prev;
Radek Krejci1798aae2020-07-14 13:26:06 +0200947 }
948 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200949 }
Michal Vaskod027f382023-02-10 09:13:25 +0100950 *node_p = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200951}
952
aPiecek4bf47212021-05-27 10:55:47 +0200953/**
954 * @brief Wrapper for ::lyd_create_opaq().
955 *
956 * @param[in] lydctx JSON data parser context.
957 * @param[in] name Name of the opaq node to create.
958 * @param[in] name_len Length of the @p name string.
959 * @param[in] prefix Prefix of the opaq node to create.
960 * @param[in] prefix_len Length of the @p prefx string.
Michal Vasko80623c52023-04-28 14:22:06 +0200961 * @param[in] parent Data parent of the opaq node, to inherit module name from.
aPiecek4bf47212021-05-27 10:55:47 +0200962 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
963 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
964 * @param[out] node_p Pointer to the created opaq node.
965 * @return LY_ERR value.
966 */
967static LY_ERR
968lydjson_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 +0100969 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +0200970{
971 LY_ERR ret = LY_SUCCESS;
972 const char *value = NULL, *module_name;
973 size_t value_len = 0, module_name_len = 0;
974 ly_bool dynamic = 0;
975 uint32_t type_hint = 0;
976
Michal Vasko09e04632023-03-22 14:34:10 +0100977 if (*status_inner_p != LYJSON_OBJECT) {
aPiecek4bf47212021-05-27 10:55:47 +0200978 /* prepare for creating opaq node with a value */
979 value = lydctx->jsonctx->value;
980 value_len = lydctx->jsonctx->value_len;
981 dynamic = lydctx->jsonctx->dynamic;
982 lydctx->jsonctx->dynamic = 0;
983
Michal Vasko583b4642023-05-25 10:39:34 +0200984 LY_CHECK_RET(lydjson_value_type_hint(lydctx->jsonctx, status_inner_p, &type_hint));
aPiecek4bf47212021-05-27 10:55:47 +0200985 }
986
Michal Vasko80623c52023-04-28 14:22:06 +0200987 /* get the module name */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100988 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vasko80623c52023-04-28 14:22:06 +0200989 if (!module_name && !parent && lydctx->any_schema) {
990 /* in an anyxml/anydata tree, parsing first node, use the previous any schema node */
991 module_name = lydctx->any_schema->module->name;
992 module_name_len = strlen(module_name);
993 }
994
995 /* create node */
aPiecek4bf47212021-05-27 10:55:47 +0200996 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
997 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
998 if (dynamic) {
999 free((char *)value);
1000 }
1001
1002 return ret;
1003}
1004
Michal Vaskoe0665742021-02-11 11:08:44 +01001005static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
1006 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +02001007
1008/**
1009 * @brief Parse opaq node from the input.
1010 *
1011 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
1012 *
1013 * @param[in] lydctx JSON data parser context.
1014 * @param[in] name Name of the opaq node to create.
1015 * @param[in] name_len Length of the @p name string.
1016 * @param[in] prefix Prefix of the opaq node to create.
1017 * @param[in] prefix_len Length of the @p prefx string.
1018 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1019 * but must be set if @p first is not.
1020 * @param[in,out] status_p Pointer to the current status of the parser context,
1021 * since the function manipulates with the context and process the input, the status can be updated.
1022 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1023 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1024 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1025 * @param[out] node_p Pointer to the created opaq node.
1026 * @return LY_ERR value.
1027 */
1028static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001029lydjson_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 +01001030 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1031 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001032{
Michal Vaskoa878a892023-08-18 12:22:07 +02001033 LY_ERR ret = LY_SUCCESS;
1034
1035 LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p), cleanup);
1036
1037 assert(*node_p);
Michal Vasko7a266772024-01-23 11:02:38 +01001038 LOG_LOCSET(NULL, *node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001039
Michal Vasko1a85d332021-08-27 10:35:28 +02001040 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1041 /* special array null value */
1042 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1043
1044 /* must be the only item */
Michal Vaskoa878a892023-08-18 12:22:07 +02001045 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup);
Michal Vasko1a85d332021-08-27 10:35:28 +02001046 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1047 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
Michal Vaskoa878a892023-08-18 12:22:07 +02001048 ret = LY_EVALID;
1049 goto cleanup;
Michal Vasko1a85d332021-08-27 10:35:28 +02001050 }
1051
1052 goto finish;
1053 }
1054
Michal Vasko09e04632023-03-22 14:34:10 +01001055 while (*status_p == LYJSON_ARRAY) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001056 /* process another instance of the same node */
Michal Vasko09e04632023-03-22 14:34:10 +01001057 if (*status_inner_p == LYJSON_OBJECT) {
Michal Vasko1a85d332021-08-27 10:35:28 +02001058 /* array with objects, list */
1059 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
1060
Radek Krejci1798aae2020-07-14 13:26:06 +02001061 /* but first process children of the object in the array */
Michal Vasko09e04632023-03-22 14:34:10 +01001062 do {
Michal Vaskoa878a892023-08-18 12:22:07 +02001063 LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL), cleanup);
Michal Vasko09e04632023-03-22 14:34:10 +01001064 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx);
1065 } while (*status_inner_p == LYJSON_OBJECT_NEXT);
Michal Vasko1a85d332021-08-27 10:35:28 +02001066 } else {
1067 /* array with values, leaf-list */
1068 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001069 }
1070
Michal Vaskoa878a892023-08-18 12:22:07 +02001071 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup);
aPiecekdbb8baf2021-05-27 11:01:51 +02001072 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1073 goto finish;
1074 }
Michal Vasko09e04632023-03-22 14:34:10 +01001075 assert(*status_inner_p == LYJSON_ARRAY_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001076
1077 /* continue with the next instance */
Michal Vaskoa878a892023-08-18 12:22:07 +02001078 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup);
1079 assert(*node_p);
aPiecek1462ab12024-02-07 09:13:29 +01001080 lydjson_maintain_children(parent, first_p, node_p,
1081 lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT, NULL);
Michal Vaskoa878a892023-08-18 12:22:07 +02001082
Michal Vasko7a266772024-01-23 11:02:38 +01001083 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +02001084
1085 LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p), cleanup);
1086
1087 assert(*node_p);
Michal Vasko7a266772024-01-23 11:02:38 +01001088 LOG_LOCSET(NULL, *node_p);
aPiecekdbb8baf2021-05-27 11:01:51 +02001089 }
1090
Michal Vasko09e04632023-03-22 14:34:10 +01001091 if (*status_p == LYJSON_OBJECT) {
stewegcbbae422024-04-22 11:38:25 +02001092 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_CONTAINER;
aPiecekdbb8baf2021-05-27 11:01:51 +02001093 /* process children */
Michal Vasko09e04632023-03-22 14:34:10 +01001094 do {
Michal Vaskoa878a892023-08-18 12:22:07 +02001095 LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL), cleanup);
Michal Vasko09e04632023-03-22 14:34:10 +01001096 *status_p = lyjson_ctx_status(lydctx->jsonctx);
1097 } while (*status_p == LYJSON_OBJECT_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001098 }
1099
aPiecekdbb8baf2021-05-27 11:01:51 +02001100finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001101 /* finish linking metadata */
Michal Vaskoa878a892023-08-18 12:22:07 +02001102 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
1103
1104cleanup:
1105 if (*node_p) {
Michal Vasko7a266772024-01-23 11:02:38 +01001106 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +02001107 }
1108 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001109}
1110
1111/**
aPiecek6cee5d02021-05-12 10:32:00 +02001112 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1113 *
1114 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1115 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1116 * for ::lydjson_parse_opaq().
1117 *
1118 * @param[in] lydctx JSON data parser context.
1119 * @param[in] name Name of the opaq node to create.
1120 * @param[in] name_len Length of the @p name string.
1121 * @param[in] prefix Prefix of the opaq node to create.
1122 * @param[in] prefix_len Length of the @p prefx string.
1123 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1124 * but must be set if @p first is not.
1125 * @param[in,out] status_p Pointer to the current status of the parser context,
1126 * since the function manipulates with the context and process the input, the status can be updated.
1127 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1128 * @param[out] node_p Pointer to the created opaq node.
1129 * @return LY_ERR value.
1130 */
1131static LY_ERR
Michal Vasko09e04632023-03-22 14:34:10 +01001132lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix,
1133 size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001134 struct lyd_node **first_p, struct lyd_node **node_p)
1135{
1136 enum LYJSON_PARSER_STATUS status_inner = 0;
1137
1138 /* move to the second item in the name/X pair */
1139 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1140
1141 if (*status_p == LYJSON_ARRAY) {
1142 /* move into the array */
1143 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1144 } else {
1145 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1146 status_inner = LYJSON_ERROR;
1147 }
1148
1149 if (status_inner == LYJSON_ERROR) {
1150 status_inner = *status_p;
1151 }
1152
1153 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001154 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1155 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001156
1157 return LY_SUCCESS;
1158}
1159
1160/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001161 * @brief Process the attribute container (starting by @)
1162 *
1163 * @param[in] lydctx JSON data parser context.
1164 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1165 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1166 * @param[in] name Name of the opaq node to create.
1167 * @param[in] name_len Length of the @p name string.
1168 * @param[in] prefix Prefix of the opaq node to create.
1169 * @param[in] prefix_len Length of the @p prefx string.
1170 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1171 * but must be set if @p first is not.
1172 * @param[in,out] status_p Pointer to the current status of the parser context,
1173 * since the function manipulates with the context and process the input, the status can be updated.
1174 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1175 * @param[out] node_p Pointer to the created opaq node.
1176 * @return LY_ERR value.
1177 */
1178static LY_ERR
1179lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001180 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1181 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001182{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001183 LY_ERR r;
Michal Vaskodcad5c82022-12-01 16:16:58 +01001184 const char *opaq_name, *mod_name;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001185 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001186
Michal Vaskofceac692022-12-01 13:50:27 +01001187 if (!snode && !prefix) {
1188 /* set the prefix */
1189 if (parent) {
1190 lydjson_get_node_prefix(parent, NULL, 0, &prefix, &prefix_len);
1191 } else {
1192 prefix = "";
1193 prefix_len = 0;
1194 }
1195 }
1196
1197 /* parse as an attribute to a (opaque) node */
1198 if (!attr_node) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001199 /* try to find the instance */
Michal Vaskofa257692024-05-28 10:45:39 +02001200 LY_LIST_FOR(parent ? lyd_child(parent) : *first_p, attr_node) {
Michal Vaskofceac692022-12-01 13:50:27 +01001201 if (snode) {
1202 if (attr_node->schema) {
1203 if (attr_node->schema == snode) {
1204 break;
1205 }
1206 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001207 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1208 if (!strcmp(LYD_NAME(attr_node), snode->name) && mod_name && !strcmp(mod_name, snode->module->name)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001209 break;
1210 }
1211 }
1212 } else {
1213 if (attr_node->schema) {
1214 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) &&
1215 !ly_strncmp(attr_node->schema->module->name, prefix, prefix_len)) {
1216 break;
1217 }
1218 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001219 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1220 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) && mod_name &&
1221 !ly_strncmp(mod_name, prefix, prefix_len)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001222 break;
1223 }
1224 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001225 }
1226 }
1227 }
1228 if (!attr_node) {
1229 /* parse just as an opaq node with the name beginning with @,
1230 * later we have to check that it belongs to a standard node
1231 * and it is supposed to be converted to a metadata */
1232 uint32_t prev_opts;
1233
Radek Krejci1798aae2020-07-14 13:26:06 +02001234 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001235 prev_opts = lydctx->parse_opts;
1236 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1237 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001238
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001239 opaq_name = prefix ? prefix - 1 : name - 1;
1240 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1241 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 +02001242
1243 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001244 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001245 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001246 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001247 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001248 }
1249
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001250 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001251}
1252
1253/**
Michal Vasko76096ec2022-02-24 16:06:16 +01001254 * @brief Parse a single anydata/anyxml node.
1255 *
1256 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1257 * as before calling, despite it is necessary to process input data for checking.
1258 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001259 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko76096ec2022-02-24 16:06:16 +01001260 * @param[in,out] status JSON parser status, is updated.
1261 * @param[out] node Parsed data (or opaque) node.
1262 * @return LY_SUCCESS if a node was successfully parsed,
1263 * @return LY_ENOT in case of invalid JSON encoding,
1264 * @return LY_ERR on other errors.
1265 */
1266static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +02001267lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1268 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko76096ec2022-02-24 16:06:16 +01001269{
Michal Vaskod027f382023-02-10 09:13:25 +01001270 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko85be65e2023-06-13 09:44:17 +02001271 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
Michal Vasko76096ec2022-02-24 16:06:16 +01001272 struct ly_in in_start;
Michal Vasko7ca33252022-12-21 09:30:32 +01001273 char *val = NULL;
Michal Vasko09e04632023-03-22 14:34:10 +01001274 const char *end;
Michal Vasko85be65e2023-06-13 09:44:17 +02001275 struct lyd_node *child = NULL;
1276 ly_bool log_node = 0;
Michal Vasko76096ec2022-02-24 16:06:16 +01001277
1278 assert(snode->nodetype & LYD_NODE_ANY);
1279
Michal Vasko85be65e2023-06-13 09:44:17 +02001280 *node = NULL;
1281
Michal Vasko76096ec2022-02-24 16:06:16 +01001282 /* status check according to allowed JSON types */
1283 if (snode->nodetype == LYS_ANYXML) {
Michal Vasko09e04632023-03-22 14:34:10 +01001284 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) &&
1285 (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) &&
1286 (*status != LYJSON_NULL), LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001287 } else {
Michal Vasko09e04632023-03-22 14:34:10 +01001288 LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001289 }
1290
1291 /* create any node */
1292 switch (*status) {
1293 case LYJSON_OBJECT:
Michal Vasko85be65e2023-06-13 09:44:17 +02001294 /* create node */
1295 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
1296 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1297
1298 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +01001299 LOG_LOCSET(NULL, *node);
Michal Vasko85be65e2023-06-13 09:44:17 +02001300 log_node = 1;
1301
Michal Vasko76096ec2022-02-24 16:06:16 +01001302 /* parse any data tree with correct options, first backup the current options and then make the parser
1303 * process data as opaq nodes */
Michal Vasko76096ec2022-02-24 16:06:16 +01001304 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001305 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vasko76096ec2022-02-24 16:06:16 +01001306 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vasko80623c52023-04-28 14:22:06 +02001307 lydctx->any_schema = snode;
Michal Vasko76096ec2022-02-24 16:06:16 +01001308
1309 /* process the anydata content */
Michal Vasko09e04632023-03-22 14:34:10 +01001310 do {
Michal Vasko85be65e2023-06-13 09:44:17 +02001311 r = lydjson_subtree_r(lydctx, NULL, &child, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001312 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1313
Michal Vasko09e04632023-03-22 14:34:10 +01001314 *status = lyjson_ctx_status(lydctx->jsonctx);
1315 } while (*status == LYJSON_OBJECT_NEXT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001316
Michal Vasko76096ec2022-02-24 16:06:16 +01001317 /* finish linking metadata */
Michal Vasko85be65e2023-06-13 09:44:17 +02001318 r = lydjson_metadata_finish(lydctx, &child);
Michal Vaskod027f382023-02-10 09:13:25 +01001319 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001320
Michal Vasko85be65e2023-06-13 09:44:17 +02001321 /* assign the data tree */
1322 ((struct lyd_node_any *)*node)->value.tree = child;
1323 child = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001324 break;
1325 case LYJSON_ARRAY:
1326 /* skip until the array end */
1327 in_start = *lydctx->jsonctx->in;
Michal Vasko85be65e2023-06-13 09:44:17 +02001328 LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001329
Michal Vasko09e04632023-03-22 14:34:10 +01001330 /* return back by all the WS */
1331 end = lydctx->jsonctx->in->current;
1332 while (is_jsonws(end[-1])) {
1333 --end;
1334 }
1335
Michal Vasko76096ec2022-02-24 16:06:16 +01001336 /* make a copy of the whole array and store it */
Michal Vasko09e04632023-03-22 14:34:10 +01001337 if (asprintf(&val, "[%.*s", (int)(end - in_start.current), in_start.current) == -1) {
Michal Vasko76096ec2022-02-24 16:06:16 +01001338 LOGMEM(lydctx->jsonctx->ctx);
Michal Vasko85be65e2023-06-13 09:44:17 +02001339 rc = LY_EMEM;
1340 goto cleanup;
Michal Vasko76096ec2022-02-24 16:06:16 +01001341 }
Michal Vaskod027f382023-02-10 09:13:25 +01001342 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
Michal Vasko85be65e2023-06-13 09:44:17 +02001343 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1344 val = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001345 break;
1346 case LYJSON_STRING:
1347 /* string value */
1348 if (lydctx->jsonctx->dynamic) {
Michal Vasko85be65e2023-06-13 09:44:17 +02001349 LY_CHECK_GOTO(rc = lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node), cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001350 lydctx->jsonctx->dynamic = 0;
1351 } else {
1352 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
Michal Vasko85be65e2023-06-13 09:44:17 +02001353 LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001354
Michal Vaskod027f382023-02-10 09:13:25 +01001355 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
Michal Vasko85be65e2023-06-13 09:44:17 +02001356 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1357 val = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001358 }
1359 break;
1360 case LYJSON_NUMBER:
1361 case LYJSON_FALSE:
1362 case LYJSON_TRUE:
1363 /* JSON value */
1364 assert(!lydctx->jsonctx->dynamic);
1365 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
Michal Vasko85be65e2023-06-13 09:44:17 +02001366 LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001367
Michal Vaskod027f382023-02-10 09:13:25 +01001368 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
Michal Vasko85be65e2023-06-13 09:44:17 +02001369 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1370 val = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001371 break;
1372 case LYJSON_NULL:
1373 /* no value */
Michal Vaskod027f382023-02-10 09:13:25 +01001374 r = lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node);
1375 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001376 break;
Michal Vasko76096ec2022-02-24 16:06:16 +01001377 default:
Michal Vasko85be65e2023-06-13 09:44:17 +02001378 LOGINT(lydctx->jsonctx->ctx);
1379 rc = LY_EINT;
1380 goto cleanup;
Michal Vasko76096ec2022-02-24 16:06:16 +01001381 }
1382
Michal Vaskod027f382023-02-10 09:13:25 +01001383cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +02001384 if (log_node) {
Michal Vasko7a266772024-01-23 11:02:38 +01001385 LOG_LOCBACK(0, 1);
Michal Vasko85be65e2023-06-13 09:44:17 +02001386 }
1387 lydctx->parse_opts = prev_parse_opts;
1388 lydctx->int_opts = prev_int_opts;
1389 lydctx->any_schema = NULL;
Michal Vasko7ca33252022-12-21 09:30:32 +01001390 free(val);
Michal Vasko85be65e2023-06-13 09:44:17 +02001391 lyd_free_tree(child);
Michal Vasko7ca33252022-12-21 09:30:32 +01001392 return rc;
Michal Vasko76096ec2022-02-24 16:06:16 +01001393}
1394
1395/**
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001396 * @brief Parse a single instance of an inner node.
1397 *
1398 * @param[in] lydctx JSON data parser context.
1399 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1400 * @param[in] ext Extension instance of @p snode, if any.
1401 * @param[in,out] status JSON parser status, is updated.
1402 * @param[out] node Parsed data (or opaque) node.
1403 * @return LY_SUCCESS if a node was successfully parsed,
1404 * @return LY_ENOT in case of invalid JSON encoding,
1405 * @return LY_ERR on other errors.
1406 */
1407static LY_ERR
1408lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1409 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
1410{
Michal Vaskod027f382023-02-10 09:13:25 +01001411 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001412 uint32_t prev_parse_opts = lydctx->parse_opts;
1413
Michal Vasko09e04632023-03-22 14:34:10 +01001414 LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001415
1416 /* create inner node */
1417 LY_CHECK_RET(lyd_create_inner(snode, node));
1418
1419 /* use it for logging */
Michal Vasko7a266772024-01-23 11:02:38 +01001420 LOG_LOCSET(NULL, *node);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001421
1422 if (ext) {
1423 /* only parse these extension data and validate afterwards */
1424 lydctx->parse_opts |= LYD_PARSE_ONLY;
1425 }
1426
1427 /* process children */
Michal Vasko09e04632023-03-22 14:34:10 +01001428 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001429 r = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
1430 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1431
Michal Vasko09e04632023-03-22 14:34:10 +01001432 *status = lyjson_ctx_status(lydctx->jsonctx);
1433 } while (*status == LYJSON_OBJECT_NEXT);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001434
1435 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001436 r = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
1437 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001438
1439 if (snode->nodetype == LYS_LIST) {
1440 /* check all keys exist */
Michal Vasko0af83702024-10-08 13:42:19 +02001441 r = lyd_parser_check_keys(*node);
Michal Vaskod027f382023-02-10 09:13:25 +01001442 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001443 }
1444
Michal Vasko40015732023-11-20 13:48:15 +01001445 if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) {
1446 /* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +01001447 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
1448 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001449
1450 /* add any missing default children */
Michal Vaskod027f382023-02-10 09:13:25 +01001451 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
1452 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1453 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001454 }
1455
1456cleanup:
1457 lydctx->parse_opts = prev_parse_opts;
Michal Vasko7a266772024-01-23 11:02:38 +01001458 LOG_LOCBACK(0, 1);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001459 if (!(*node)->hash) {
1460 /* list without keys is unusable */
1461 lyd_free_tree(*node);
1462 *node = NULL;
1463 }
Michal Vaskod027f382023-02-10 09:13:25 +01001464 return rc;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001465}
1466
1467/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001468 * @brief Parse a single instance of a node.
1469 *
1470 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1471 * as before calling, despite it is necessary to process input data for checking.
1472 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1473 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1474 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001475 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko32ac9942020-08-12 14:35:12 +02001476 * @param[in] name Parsed JSON node name.
1477 * @param[in] name_len Lenght of @p name.
1478 * @param[in] prefix Parsed JSON node prefix.
1479 * @param[in] prefix_len Length of @p prefix.
1480 * @param[in,out] status JSON parser status, is updated.
1481 * @param[out] node Parsed data (or opaque) node.
1482 * @return LY_SUCCESS if a node was successfully parsed,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001483 * @return LY_ENOT in case of invalid JSON encoding,
Michal Vasko32ac9942020-08-12 14:35:12 +02001484 * @return LY_ERR on other errors.
1485 */
1486static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001487lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001488 const struct lysc_node *snode, struct lysc_ext_instance *ext, const char *name, size_t name_len,
1489 const char *prefix, size_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001490{
Michal Vaskod027f382023-02-10 09:13:25 +01001491 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +01001492 uint32_t type_hints = 0;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001493
Michal Vasko7a266772024-01-23 11:02:38 +01001494 LOG_LOCSET(snode, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001495
Michal Vaskod027f382023-02-10 09:13:25 +01001496 r = lydjson_data_check_opaq(lydctx, snode, &type_hints);
1497 if (r == LY_SUCCESS) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001498 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
steweg68f6eaf2024-04-05 09:24:08 +02001499 if ((lydctx->parse_opts & LYD_PARSE_JSON_NULL) && (*status == LYJSON_NULL)) {
1500 /* do not do anything if value is JSON 'null' */
1501 goto cleanup;
1502 } else if (snode->nodetype & LYD_NODE_TERM) {
1503 if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001504 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) {
Michal Vaskod027f382023-02-10 09:13:25 +01001505 rc = LY_ENOT;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001506 goto cleanup;
1507 }
Michal Vaskobbdadda2022-01-06 11:40:10 +01001508
Michal Vasko32ac9942020-08-12 14:35:12 +02001509 /* create terminal node */
Michal Vaskod027f382023-02-10 09:13:25 +01001510 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1511 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, type_hints, node);
1512 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001513
1514 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001515 if (*status == LYJSON_ARRAY) {
1516 /* only [null], 2 more moves are needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001517 r = lyjson_ctx_next(lydctx->jsonctx, status);
1518 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001519 assert(*status == LYJSON_NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001520
1521 r = lyjson_ctx_next(lydctx->jsonctx, status);
1522 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001523 assert(*status == LYJSON_ARRAY_CLOSED);
1524 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001525 } else if (snode->nodetype & LYD_NODE_INNER) {
1526 /* create inner node */
Michal Vaskod027f382023-02-10 09:13:25 +01001527 r = lydjson_parse_instance_inner(lydctx, snode, ext, status, node);
1528 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001529 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001530 /* create any node */
Michal Vaskod027f382023-02-10 09:13:25 +01001531 r = lydjson_parse_any(lydctx, snode, ext, status, node);
1532 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001533 }
Michal Vaskod027f382023-02-10 09:13:25 +01001534 LY_CHECK_GOTO(!*node, cleanup);
Michal Vaskocaf608d2021-07-23 13:51:23 +02001535
1536 /* add/correct flags */
Michal Vasko0af83702024-10-08 13:42:19 +02001537 r = lyd_parser_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext);
Michal Vaskod027f382023-02-10 09:13:25 +01001538 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001539
Michal Vaskoeba23112022-08-26 08:35:41 +02001540 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1541 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001542 r = lyd_validate_node_ext(*node, &lydctx->ext_node);
1543 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001544 }
Michal Vaskod027f382023-02-10 09:13:25 +01001545 } else if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001546 /* parse it again as an opaq node */
Michal Vaskod027f382023-02-10 09:13:25 +01001547 r = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
1548 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001549
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001550 if (snode->nodetype == LYS_LIST) {
1551 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1552 } else if (snode->nodetype == LYS_LEAFLIST) {
1553 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001554 }
Michal Vaskoa32b6b72022-07-11 15:55:56 +02001555 } else {
1556 /* error */
Michal Vasko1e8746a2023-02-13 09:15:16 +01001557 rc = r;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001558 goto cleanup;
Michal Vasko32ac9942020-08-12 14:35:12 +02001559 }
1560
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001561cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +01001562 LOG_LOCBACK(1, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001563 return rc;
Michal Vasko32ac9942020-08-12 14:35:12 +02001564}
1565
1566/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001567 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001568 *
1569 * @param[in] lydctx JSON data parser context.
1570 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1571 * @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 +01001572 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001573 * @return LY_ERR value.
1574 */
1575static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001576lydjson_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 +02001577{
Michal Vaskod027f382023-02-10 09:13:25 +01001578 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko09e04632023-03-22 14:34:10 +01001579 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx);
aPiecek49e2a3a2021-05-13 10:36:46 +02001580 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001581 size_t name_len, prefix_len = 0;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001582 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001583 const struct lysc_node *snode = NULL;
lePici21a58fe2023-07-04 08:02:53 +02001584 struct lysc_ext_instance *ext = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001585 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001586 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001587 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001588
1589 assert(parent || first_p);
Michal Vasko09e04632023-03-22 14:34:10 +01001590 assert((status == LYJSON_OBJECT) || (status == LYJSON_OBJECT_NEXT));
Radek Krejci1798aae2020-07-14 13:26:06 +02001591
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001592 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1593 /* all descendants should be parsed */
1594 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1595
Michal Vasko09e04632023-03-22 14:34:10 +01001596 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1597 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1598 if (status == LYJSON_OBJECT_CLOSED) {
1599 /* empty object, fine... */
1600 goto cleanup;
1601 }
1602
Radek Krejci1798aae2020-07-14 13:26:06 +02001603 /* process the node name */
Michal Vasko09e04632023-03-22 14:34:10 +01001604 assert(status == LYJSON_OBJECT_NAME);
aPiecekd2c39372021-06-16 14:03:12 +02001605 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 +01001606 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001607
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001608 if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && !is_meta && name_len && !prefix_len &&
1609 !ly_strncmp("eventTime", name, name_len)) {
1610 /* parse eventTime */
1611 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1612 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1613
1614 if (status != LYJSON_STRING) {
1615 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_STRING),
1616 lyjson_token2str(status));
1617 rc = LY_EVALID;
1618 goto cleanup;
1619 }
1620
1621 /* create node */
1622 r = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, prefix, prefix_len,
1623 lydctx->jsonctx->value, lydctx->jsonctx->value_len, NULL, LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, &node);
1624 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1625
1626 /* validate the value */
1627 r = lyd_parser_notif_eventtime_validate(node);
1628 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1629
1630 goto node_parsed;
1631 } else if (!is_meta || name_len || prefix_len) {
Michal Vaskocabe0702020-08-12 10:14:36 +02001632 /* get the schema node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001633 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001634 if (r == LY_ENOT) {
1635 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001636 goto cleanup;
Michal Vasko514f6982023-02-17 09:07:39 +01001637 } else if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1638 rc = r;
1639
1640 /* skip the invalid data */
1641 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1642 rc = r;
1643 }
Michal Vasko1241ebc2023-04-04 10:03:45 +02001644 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1645 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko514f6982023-02-17 09:07:39 +01001646 goto cleanup;
1647 } else if (r) {
1648 /* error */
1649 rc = r;
1650 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001651 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001652
Michal Vaskocabe0702020-08-12 10:14:36 +02001653 if (!snode) {
1654 /* we will not be parsing it as metadata */
1655 is_meta = 0;
1656 }
1657 }
1658
1659 if (is_meta) {
1660 /* parse as metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001661 if (!name_len && !prefix_len && !parent) {
1662 LOGVAL(ctx, LYVE_SYNTAX_JSON,
1663 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1664 r = LY_EVALID;
1665 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1666 } else if (!name_len && !prefix_len) {
Michal Vaskocabe0702020-08-12 10:14:36 +02001667 /* parent's metadata without a name - use the schema from the parent */
Michal Vaskoe0665742021-02-11 11:08:44 +01001668 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001669 snode = attr_node->schema;
1670 }
Michal Vaskod027f382023-02-10 09:13:25 +01001671 r = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001672 first_p, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001673 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001674 } else if (!snode) {
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001675 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
1676 /* skip element with children */
1677 r = lydjson_data_skip(lydctx->jsonctx);
1678 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1679 } else {
1680 /* parse as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001681
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001682 /* opaq node cannot have an empty string as the name. */
1683 if (name_len == 0) {
Michal Vaskobfebf662023-02-17 09:07:22 +01001684 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "JSON object member name cannot be a zero-length string.");
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001685 r = LY_EVALID;
1686 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1687 }
1688
1689 /* move to the second item in the name/X pair and parse opaq */
1690 r = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node);
1691 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
aPiecekb7b29e62021-05-11 10:02:43 +02001692 }
Michal Vaskocabe0702020-08-12 10:14:36 +02001693 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001694 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001695
1696 /* move to the second item in the name/X pair */
Michal Vaskod027f382023-02-10 09:13:25 +01001697 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1698 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001699
Michal Vasko7ca33252022-12-21 09:30:32 +01001700 /* set expected representation */
1701 switch (snode->nodetype) {
1702 case LYS_LEAFLIST:
1703 expected = "name/array of values";
1704 break;
1705 case LYS_LIST:
1706 expected = "name/array of objects";
1707 break;
1708 case LYS_LEAF:
1709 if (status == LYJSON_ARRAY) {
1710 expected = "name/[null]";
1711 } else {
1712 expected = "name/value";
1713 }
1714 break;
1715 case LYS_CONTAINER:
1716 case LYS_NOTIF:
1717 case LYS_ACTION:
1718 case LYS_RPC:
1719 case LYS_ANYDATA:
1720 expected = "name/object";
1721 break;
1722 case LYS_ANYXML:
1723 if (status == LYJSON_ARRAY) {
1724 expected = "name/array";
1725 } else {
1726 expected = "name/value";
1727 }
1728 break;
1729 }
1730
1731 /* check the representation according to the nodetype and then continue with the content */
Radek Krejci1798aae2020-07-14 13:26:06 +02001732 switch (snode->nodetype) {
1733 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001734 case LYS_LIST:
Radek Krejci1798aae2020-07-14 13:26:06 +02001735 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1736
Michal Vasko32ac9942020-08-12 14:35:12 +02001737 /* process all the values/objects */
1738 do {
Michal Vasko09e04632023-03-22 14:34:10 +01001739 /* move into array/next value */
1740 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1741 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1742 if (status == LYJSON_ARRAY_CLOSED) {
1743 /* empty array, fine... */
1744 break;
1745 }
1746
Michal Vaskod027f382023-02-10 09:13:25 +01001747 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001748 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001749 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001750 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001751 }
Michal Vaskod027f382023-02-10 09:13:25 +01001752 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1753
aPiecek1462ab12024-02-07 09:13:29 +01001754 lydjson_maintain_children(parent, first_p, &node,
1755 lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT, ext);
Radek Krejci1798aae2020-07-14 13:26:06 +02001756
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001757 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001758 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1759 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko09e04632023-03-22 14:34:10 +01001760 } while (status == LYJSON_ARRAY_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001761
1762 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001763 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001764 case LYS_CONTAINER:
1765 case LYS_NOTIF:
1766 case LYS_ACTION:
1767 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001768 case LYS_ANYDATA:
1769 case LYS_ANYXML:
Michal Vasko32ac9942020-08-12 14:35:12 +02001770 /* process the value/object */
Michal Vaskod027f382023-02-10 09:13:25 +01001771 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001772 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001773 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001774 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001775 }
Michal Vaskod027f382023-02-10 09:13:25 +01001776 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001777
Michal Vasko32ac9942020-08-12 14:35:12 +02001778 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001779 /* rememeber the RPC/action/notification */
1780 lydctx->op_node = node;
1781 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001782 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001783 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001784 }
1785
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001786node_parsed:
Michal Vaskoe0665742021-02-11 11:08:44 +01001787 /* rememeber a successfully parsed node */
Michal Vasko4e26adc2022-03-30 11:01:16 +02001788 if (parsed && node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001789 ly_set_add(parsed, node, 1, NULL);
1790 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001791
Michal Vasko32d3d972023-09-05 13:37:03 +02001792 /* finally connect the parsed node, is zeroed */
aPiecek1462ab12024-02-07 09:13:29 +01001793 lydjson_maintain_children(parent, first_p, &node,
1794 lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT, ext);
Michal Vasko32d3d972023-09-05 13:37:03 +02001795
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001796 if (!parse_subtree) {
1797 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001798 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1799 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001800 }
1801
Radek Krejci5813c362021-01-05 10:51:57 +01001802 /* success */
1803 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001804
1805representation_error:
Michal Vaskobfebf662023-02-17 09:07:22 +01001806 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as name/%s.",
1807 expected, lys_nodetype2str(snode->nodetype), snode->name, lyjson_token2str(status));
Michal Vaskod027f382023-02-10 09:13:25 +01001808 rc = LY_EVALID;
1809 if (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) {
1810 /* try to skip the invalid data */
Michal Vasko560f9de2023-02-10 09:56:01 +01001811 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1812 rc = r;
1813 }
Michal Vaskod027f382023-02-10 09:13:25 +01001814 }
Radek Krejci5813c362021-01-05 10:51:57 +01001815
1816cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001817 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001818 lyd_free_tree(node);
Michal Vaskod027f382023-02-10 09:13:25 +01001819 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +02001820}
1821
1822/**
1823 * @brief Common start of JSON parser processing different types of the input data.
1824 *
1825 * @param[in] ctx libyang context
1826 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001827 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1828 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001829 * @param[out] lydctx_p Data parser context to finish validation.
1830 * @return LY_ERR value.
1831 */
1832static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001833lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
Michal Vasko09e04632023-03-22 14:34:10 +01001834 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001835{
1836 LY_ERR ret = LY_SUCCESS;
1837 struct lyd_json_ctx *lydctx;
Michal Vasko09e04632023-03-22 14:34:10 +01001838 enum LYJSON_PARSER_STATUS status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001839
Radek Krejcicd5f6222020-11-12 08:54:13 +01001840 assert(lydctx_p);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001841
Radek Krejci1798aae2020-07-14 13:26:06 +02001842 /* init context */
1843 lydctx = calloc(1, sizeof *lydctx);
1844 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001845 lydctx->parse_opts = parse_opts;
1846 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001847 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001848
Michal Vasko09e04632023-03-22 14:34:10 +01001849 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1850 status = lyjson_ctx_status(lydctx->jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001851
Michal Vasko09e04632023-03-22 14:34:10 +01001852 /* parse_opts & LYD_PARSE_SUBTREE not implemented */
1853 if (status != LYJSON_OBJECT) {
Radek Krejcicd5f6222020-11-12 08:54:13 +01001854 /* expecting top-level object */
Michal Vasko09e04632023-03-22 14:34:10 +01001855 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001856 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001857 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001858 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001859 }
Michal Vasko09e04632023-03-22 14:34:10 +01001860
1861 *lydctx_p = lydctx;
1862 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001863}
1864
1865LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001866lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001867 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 +01001868 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001869{
Michal Vaskod027f382023-02-10 09:13:25 +01001870 LY_ERR r, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001871 struct lyd_json_ctx *lydctx = NULL;
1872 enum LYJSON_PARSER_STATUS status;
1873
Michal Vasko09e04632023-03-22 14:34:10 +01001874 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx);
1875 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001876
Michal Vaskoe0665742021-02-11 11:08:44 +01001877 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001878 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001879
1880 /* find the operation node if it exists already */
1881 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1882
Radek Krejci1798aae2020-07-14 13:26:06 +02001883 /* read subtree(s) */
Michal Vasko09e04632023-03-22 14:34:10 +01001884 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001885 r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1886 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001887
Michal Vasko09e04632023-03-22 14:34:10 +01001888 status = lyjson_ctx_status(lydctx->jsonctx);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001889
1890 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001891 break;
1892 }
Michal Vasko09e04632023-03-22 14:34:10 +01001893 } while (status == LYJSON_OBJECT_NEXT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001894
Michal Vasko5df28522023-08-25 08:04:37 +02001895 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001896 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001897 r = LY_EVALID;
1898 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001899 }
1900 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1901 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001902 r = LY_EVALID;
1903 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001904 }
1905
1906 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001907 r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1908 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001909
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001910 if (parse_opts & LYD_PARSE_SUBTREE) {
1911 /* check for a sibling object */
1912 assert(subtree_sibling);
Michal Vasko5df28522023-08-25 08:04:37 +02001913 if (status == LYJSON_OBJECT_NEXT) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001914 *subtree_sibling = 1;
1915
1916 /* move to the next object */
1917 ly_in_skip(lydctx->jsonctx->in, 1);
1918 } else {
1919 *subtree_sibling = 0;
1920 }
1921 }
1922
Radek Krejci1798aae2020-07-14 13:26:06 +02001923cleanup:
1924 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001925 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001926 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001927
Michal Vaskod027f382023-02-10 09:13:25 +01001928 if (rc && (!lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001929 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001930 } else {
1931 *lydctx_p = (struct lyd_ctx *)lydctx;
Michal Vaskof8ebf132022-11-21 14:06:48 +01001932
1933 /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1934 lyjson_ctx_free(lydctx->jsonctx);
1935 lydctx->jsonctx = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001936 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001937 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001938}
Michal Vasko820efe82023-05-12 15:47:43 +02001939
1940/**
1941 * @brief Parse a specific JSON object into an opaque node.
1942 *
1943 * @param[in] jsonctx JSON parser context.
1944 * @param[in] name Name of the object.
1945 * @param[in] module Module name of the object, NULL if none expected.
Michal Vasko820efe82023-05-12 15:47:43 +02001946 * @param[out] evnp Parsed envelope (opaque node).
1947 * @return LY_SUCCESS on success.
1948 * @return LY_ENOT if the specified object did not match.
1949 * @return LY_ERR value on error.
1950 */
1951static LY_ERR
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001952lydjson_envelope(struct lyjson_ctx *jsonctx, const char *name, const char *module, struct lyd_node **envp)
Michal Vasko820efe82023-05-12 15:47:43 +02001953{
1954 LY_ERR rc = LY_SUCCESS, r;
1955 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001956 const char *nam, *prefix;
1957 size_t nam_len, prefix_len;
Michal Vasko820efe82023-05-12 15:47:43 +02001958 ly_bool is_meta;
1959
1960 assert(status == LYJSON_OBJECT);
1961
1962 *envp = NULL;
1963
1964 r = lyjson_ctx_next(jsonctx, &status);
1965 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1966 if (status == LYJSON_OBJECT_CLOSED) {
1967 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Empty JSON object.");
1968 rc = LY_EVALID;
1969 goto cleanup;
1970 }
1971
1972 /* process the name */
1973 assert(status == LYJSON_OBJECT_NAME);
1974 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &nam, &nam_len, &prefix, &prefix_len, &is_meta);
1975 if (is_meta) {
1976 LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected metadata.");
1977 rc = LY_EVALID;
1978 goto cleanup;
1979 } else if (module && ly_strncmp(module, prefix, prefix_len)) {
1980 LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected module \"%.*s\" instead of \"%s\".", (int)prefix_len, prefix, module);
1981 rc = LY_EVALID;
1982 goto cleanup;
1983 } else if (ly_strncmp(name, nam, nam_len)) {
1984 LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected object \"%.*s\" instead of \"%s\".", (int)nam_len, nam, name);
1985 rc = LY_EVALID;
1986 goto cleanup;
1987 }
1988
1989 r = lyjson_ctx_next(jsonctx, &status);
1990 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1991
Michal Vasko820efe82023-05-12 15:47:43 +02001992 /* create node */
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001993 rc = lyd_create_opaq(jsonctx->ctx, name, strlen(name), prefix, prefix_len, prefix, prefix_len, NULL, 0, NULL,
1994 LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001995 LY_CHECK_GOTO(rc, cleanup);
1996
1997cleanup:
1998 if (rc) {
1999 lyd_free_tree(*envp);
2000 *envp = NULL;
2001 }
2002 return rc;
2003}
2004
Michal Vasko820efe82023-05-12 15:47:43 +02002005LY_ERR
2006lyd_parse_json_restconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
2007 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
2008 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
2009{
2010 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko33b93082023-08-08 13:48:16 +02002011 struct lyd_json_ctx *lydctx = NULL;
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002012 struct lyd_node *node;
Michal Vasko820efe82023-05-12 15:47:43 +02002013 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vasko820efe82023-05-12 15:47:43 +02002014
2015 assert(ctx && in && lydctx_p);
2016 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
2017 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
2018
2019 assert((data_type == LYD_TYPE_RPC_RESTCONF) || (data_type == LYD_TYPE_NOTIF_RESTCONF) ||
2020 (data_type == LYD_TYPE_REPLY_RESTCONF));
2021 assert(!(parse_opts & LYD_PARSE_SUBTREE));
2022
2023 /* init context */
2024 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx);
2025 LY_CHECK_GOTO(rc, cleanup);
2026 lydctx->ext = ext;
2027
2028 switch (data_type) {
2029 case LYD_TYPE_RPC_RESTCONF:
2030 assert(parent);
2031
2032 /* parse "input" */
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002033 rc = lydjson_envelope(lydctx->jsonctx, "input", lyd_node_module(parent)->name, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02002034 LY_CHECK_GOTO(rc, cleanup);
2035
2036 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
2037 close_elem = 1;
2038 break;
2039 case LYD_TYPE_NOTIF_RESTCONF:
2040 assert(!parent);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002041
2042 /* parse "notification" */
2043 rc = lydjson_envelope(lydctx->jsonctx, "notification", "ietf-restconf", envp);
Michal Vasko820efe82023-05-12 15:47:43 +02002044 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002045
2046 /* RESTCONF notification and eventTime */
2047 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;
2048 close_elem = 1;
Michal Vasko820efe82023-05-12 15:47:43 +02002049 break;
2050 case LYD_TYPE_REPLY_RESTCONF:
2051 assert(parent);
2052
2053 /* parse "output" */
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002054 rc = lydjson_envelope(lydctx->jsonctx, "output", lyd_node_module(parent)->name, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02002055 LY_CHECK_GOTO(rc, cleanup);
2056
2057 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
2058 close_elem = 1;
2059 break;
2060 default:
2061 LOGINT(ctx);
2062 rc = LY_EINT;
2063 goto cleanup;
2064 }
2065
2066 lydctx->int_opts = int_opts;
2067
2068 /* find the operation node if it exists already */
2069 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
2070
2071 /* read subtree(s) */
2072 do {
2073 r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
2074 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002075 } while (lyjson_ctx_status(lydctx->jsonctx) == LYJSON_OBJECT_NEXT);
Michal Vasko820efe82023-05-12 15:47:43 +02002076
2077 /* close all opened elements */
2078 for (i = 0; i < close_elem; ++i) {
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002079 if (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_OBJECT_CLOSED) {
Michal Vasko820efe82023-05-12 15:47:43 +02002080 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_OBJECT_CLOSED),
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002081 lyjson_token2str(lyjson_ctx_status(lydctx->jsonctx)));
Michal Vasko820efe82023-05-12 15:47:43 +02002082 rc = LY_EVALID;
2083 goto cleanup;
2084 }
2085
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002086 r = lyjson_ctx_next(lydctx->jsonctx, NULL);
Michal Vasko820efe82023-05-12 15:47:43 +02002087 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2088 }
2089
Michal Vasko820efe82023-05-12 15:47:43 +02002090 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
2091 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
2092 r = LY_EVALID;
2093 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
2094 }
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002095 if (int_opts & LYD_INTOPT_EVENTTIME) {
2096 /* parse as a child of the envelope */
2097 node = (*first_p)->prev;
2098 if (node->schema) {
2099 LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node.");
2100 r = LY_EVALID;
2101 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
2102 } else {
2103 /* can be the only opaque node and an operation had to be parsed */
2104 assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
Michal Vasko2e784f82024-01-11 09:51:22 +01002105 lyd_unlink(node);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002106 assert(*envp);
2107 lyd_insert_child(*envp, node);
2108 }
2109 }
Michal Vasko820efe82023-05-12 15:47:43 +02002110
2111 /* finish linking metadata */
2112 r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
2113 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2114
Michal Vasko820efe82023-05-12 15:47:43 +02002115cleanup:
2116 /* there should be no unres stored if validation should be skipped */
Michal Vasko33b93082023-08-08 13:48:16 +02002117 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko820efe82023-05-12 15:47:43 +02002118 !lydctx->node_when.count));
2119
2120 if (rc) {
2121 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
2122 } else {
2123 *lydctx_p = (struct lyd_ctx *)lydctx;
2124
2125 /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */
2126 lyjson_ctx_free(lydctx->jsonctx);
2127 lydctx->jsonctx = NULL;
2128 }
2129 return rc;
2130}