blob: 701e89712688c70f5b0ec16136c1b64c1ee96b7a [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko61ad1ff2022-02-10 15:48:39 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01005 * @brief JSON data parser for libyang
6 *
Michal Vasko09e04632023-03-22 14:34:10 +01007 * Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010017
Radek Krejci1798aae2020-07-14 13:26:06 +020018#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010020#include <stdlib.h>
21#include <string.h>
22
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vaskobbdadda2022-01-06 11:40:10 +010024#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010025#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020028#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010030#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020031#include "parser_internal.h"
Michal Vasko61ad1ff2022-02-10 15:48:39 +010032#include "plugins_exts.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "set.h"
34#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020035#include "tree_data.h"
36#include "tree_data_internal.h"
37#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010038#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020039#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010040
41/**
Radek Krejci1798aae2020-07-14 13:26:06 +020042 * @brief Free the JSON data parser context.
43 *
44 * JSON implementation of lyd_ctx_free_clb().
45 */
46static void
47lyd_json_ctx_free(struct lyd_ctx *lydctx)
48{
49 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
50
51 if (lydctx) {
52 lyd_ctx_free(lydctx);
53 lyjson_ctx_free(ctx->jsonctx);
54 free(ctx);
55 }
56}
57
58/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +010059 * @brief Pass the responsibility for releasing the dynamic values to @p dst.
aPiecekd2c39372021-06-16 14:03:12 +020060 *
61 * @param[in] jsonctx JSON context which contains the dynamic value.
62 * @param[in,out] dst Pointer to which the responsibility will be submited.
Michal Vasko61ad1ff2022-02-10 15:48:39 +010063 * If the pointer is already pointing to some allocated memory, it is released beforehand.
aPiecekd2c39372021-06-16 14:03:12 +020064 */
65static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010066lyjson_ctx_give_dynamic_value(struct lyjson_ctx *jsonctx, char **dst)
aPiecekd2c39372021-06-16 14:03:12 +020067{
68 assert(jsonctx && dst);
69
70 if (!jsonctx->dynamic) {
71 return;
72 }
73
74 if (dst) {
75 free(*dst);
76 }
77 *dst = NULL;
78
Michal Vasko61ad1ff2022-02-10 15:48:39 +010079 /* give the dynamic value */
aPiecekd2c39372021-06-16 14:03:12 +020080 *dst = (char *)jsonctx->value;
81
Michal Vasko61ad1ff2022-02-10 15:48:39 +010082 /* responsibility for the release is now passed to dst */
aPiecekd2c39372021-06-16 14:03:12 +020083 jsonctx->dynamic = 0;
84}
85
86/**
Radek Krejci1798aae2020-07-14 13:26:06 +020087 * @brief Parse JSON member-name as [\@][prefix:][name]
88 *
Michal Vaskocabe0702020-08-12 10:14:36 +020089 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020090 * prefix - name of the module of the data node
91 * name - name of the data node
92 *
93 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
94 *
95 * @param[in] value String to parse
96 * @param[in] value_len Length of the @p str.
97 * @param[out] name_p Pointer to the beginning of the parsed name.
98 * @param[out] name_len_p Pointer to the length of the parsed name.
99 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
100 * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0.
Michal Vaskocabe0702020-08-12 10:14:36 +0200101 * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise.
Radek Krejci1798aae2020-07-14 13:26:06 +0200102 */
103static void
Michal Vaskocabe0702020-08-12 10:14:36 +0200104lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p,
Radek Krejci857189e2020-09-01 13:26:36 +0200105 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200106{
107 const char *name, *prefix = NULL;
108 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200109 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200110
111 name = memchr(value, ':', value_len);
112 if (name != NULL) {
113 prefix = value;
114 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200115 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 prefix++;
117 }
118 prefix_len = name - prefix;
119 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200120 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200121 } else {
122 name = value;
123 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200124 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200125 name++;
126 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200127 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100128 }
129
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 *name_p = name;
131 *name_len_p = name_len;
132 *prefix_p = prefix;
133 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200134 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200135}
136
137/**
138 * @brief Get correct prefix (module_name) inside the @p node.
139 *
140 * @param[in] node Data node to get inherited prefix.
141 * @param[in] local_prefix Local prefix to replace the inherited prefix.
142 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
143 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
144 * and no context @p node to get inherited prefix.
145 * @param[out] prefix_len_p Pointer to the length of the resulting @p prefix_p string. Note that the result can be 0 in case
146 * of no local prefix and no context @p node to get inherited prefix.
147 * @return LY_ERR value.
148 */
149static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200150lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200151 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200152{
153 struct lyd_node_opaq *onode;
154 const char *module_name = NULL;
155
156 assert(prefix_p && prefix_len_p);
157
158 if (local_prefix_len) {
159 *prefix_p = local_prefix;
160 *prefix_len_p = local_prefix_len;
161 return LY_SUCCESS;
162 }
163
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:
228 /* no other status expected */
229 LOGINT(jsonctx->ctx);
230 return LY_EINT;
Michal Vaskoa4af6252022-05-20 10:38:54 +0200231 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200232
233 return LY_SUCCESS;
234}
235
236/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100237 * @brief Get schema node corresponding to the input parameters.
238 *
239 * @param[in] lydctx JSON data parser context.
240 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
241 * @param[in] prefix Requested node's prefix (module name).
242 * @param[in] prefix_len Length of the @p prefix.
243 * @param[in] name Requested node's name.
244 * @param[in] name_len Length of the @p name.
245 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200246 * @param[out] snode Found schema node corresponding to the input parameters. If NULL, parse as an opaque node.
247 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100248 * @return LY_SUCCES on success.
249 * @return LY_ENOT if the whole object was parsed (skipped or as an extension).
250 * @return LY_ERR on error.
251 */
252static LY_ERR
253lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200254 size_t name_len, struct lyd_node *parent, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100255{
256 LY_ERR ret = LY_SUCCESS, r;
257 struct lys_module *mod = NULL;
258 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
259
Michal Vasko8cc3f662022-03-29 11:25:51 +0200260 *snode = NULL;
261 *ext = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100262
Michal Vasko8cc3f662022-03-29 11:25:51 +0200263 /* get the element module, prefer parent context because of extensions */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100264 if (prefix_len) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200265 mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100266 } else if (parent) {
267 if (parent->schema) {
268 mod = parent->schema->module;
269 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100270 } else if (!(lydctx->int_opts & LYD_INTOPT_ANY)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100271 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
272 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
273 ret = LY_EVALID;
274 goto cleanup;
275 }
276 if (!mod) {
277 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200278 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
279 if (r != LY_ENOT) {
280 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100281 ret = r;
282 goto cleanup;
283 }
284
285 /* unknown module */
286 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
287 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
288 ret = LY_EVALID;
289 goto cleanup;
290 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100291 }
292
293 /* get the schema node */
294 if (mod && (!parent || parent->schema)) {
295 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200296 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200298 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200300 if (!*snode) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100301 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200302 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
303 if (r != LY_ENOT) {
304 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100305 ret = r;
306 goto cleanup;
307 }
308
309 /* unknown data node */
310 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
311 if (parent) {
312 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
313 (int)name_len, name, LYD_NAME(parent));
314 } else if (lydctx->ext) {
315 if (lydctx->ext->argument) {
316 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
317 "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
318 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
319 } else {
320 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
321 (int)name_len, name, lydctx->ext->def->name);
322 }
323 } else {
324 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
325 (int)name_len, name, mod->name);
326 }
327 ret = LY_EVALID;
328 goto cleanup;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100329 }
330 } else {
331 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200332 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100333 }
334 }
335
336cleanup:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100337 return ret;
338}
339
340/**
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;
504
505 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200506
Michal Vasko32ac9942020-08-12 14:35:12 +0200507 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
508 /* can always be parsed as a data node if we have the schema node */
509 return LY_SUCCESS;
510 }
511
Michal Vasko85430702021-07-21 14:29:56 +0200512 /* backup parser */
513 lyjson_ctx_backup(jsonctx);
Michal Vasko09e04632023-03-22 14:34:10 +0100514 status = lyjson_ctx_status(jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200515
Michal Vasko85430702021-07-21 14:29:56 +0200516 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200517 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
518 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200519 switch (snode->nodetype) {
520 case LYS_LEAFLIST:
521 case LYS_LEAF:
522 /* value may not be valid in which case we parse it as an opaque node */
Michal Vasko583b4642023-05-25 10:39:34 +0200523 if ((ret = lydjson_value_type_hint(jsonctx, &status, type_hint_p))) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200524 break;
525 }
Michal Vasko583b4642023-05-25 10:39:34 +0200526 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 +0200527 ret = LY_ENOT;
528 }
529 break;
530 case LYS_LIST:
531 /* lists may not have all its keys */
532 if (lydjson_check_list(jsonctx, snode)) {
Michal Vasko86fdf342022-12-01 11:08:29 +0100533 /* invalid list, parse as opaque if it misses/has invalid some keys */
Radek Krejci1798aae2020-07-14 13:26:06 +0200534 ret = LY_ENOT;
535 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200536 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200537 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200538 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vasko583b4642023-05-25 10:39:34 +0200539 ret = lydjson_value_type_hint(jsonctx, &status, type_hint_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200540 }
541
Michal Vasko85430702021-07-21 14:29:56 +0200542 /* restore parser */
543 lyjson_ctx_restore(jsonctx);
544
Radek Krejci1798aae2020-07-14 13:26:06 +0200545 return ret;
546}
547
548/**
549 * @brief Join the forward-referencing metadata with their target data nodes.
550 *
551 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
552 *
553 * @param[in] lydctx JSON data parser context.
554 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
555 * as a starting point to search for the metadata's target data node
556 * @return LY_SUCCESS on success
557 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
558 */
559static LY_ERR
560lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
561{
562 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200563 struct lyd_node *node, *attr, *next, *meta_iter;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200564 struct lysc_ext_instance *ext;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200565 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200566 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100567 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200568
569 /* finish linking metadata */
570 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200571 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200572 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200573 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200574 const char *name, *prefix;
575 size_t name_len, prefix_len;
576 const struct lysc_node *snode;
577
Michal Vaskoad92b672020-11-12 13:11:31 +0100578 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200579 /* not an opaq metadata node */
580 continue;
581 }
582
Radek Krejciddace2c2021-01-08 11:30:56 +0100583 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100584 log_location_items++;
585
Michal Vaskoad92b672020-11-12 13:11:31 +0100586 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200587 /* metas' names are stored in dictionary, so checking pointers must works */
588 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100589 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200590 instance = 1;
591 } else {
592 instance++;
593 }
594
aPiecek5057c2e2021-05-17 10:28:03 +0200595 /* find the corresponding data node */
596 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200597 if (!node->schema) {
598 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100599 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200600 continue;
601 }
602
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200603 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100604 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
605 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200606 ret = LY_EVALID;
607 goto cleanup;
608 }
609
610 /* match */
611 match++;
612 if (match != instance) {
613 continue;
614 }
615
616 LY_LIST_FOR(meta_container->child, meta_iter) {
617 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200618 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100619
Michal Vaskoad92b672020-11-12 13:11:31 +0100620 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
621 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200622 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100623 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200624 LY_CHECK_GOTO(ret, cleanup);
625 }
626
627 /* done */
628 break;
629 } else {
630 /* this is the second time we are resolving the schema node, so it must succeed,
631 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100632 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
633 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200634 assert(is_attr);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200635 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 +0200636
637 if (snode != node->schema) {
638 continue;
639 }
640
641 /* match */
642 match++;
643 if (match != instance) {
644 continue;
645 }
646
647 LY_LIST_FOR(meta_container->child, meta_iter) {
648 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200649 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200650 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200651
Michal Vaskoad92b672020-11-12 13:11:31 +0100652 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200653 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200654 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100655 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100656 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200657 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100658 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200659 if (meta->name.prefix) {
660 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
661 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
662 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
663 meta->name.name);
664 } else {
665 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
666 meta->name.name);
667 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200668 ret = LY_EVALID;
669 goto cleanup;
670 }
671 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200672
Michal Vasko8cc3f662022-03-29 11:25:51 +0200673 /* add/correct flags */
674 ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, ext);
675 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200676 break;
677 }
678 }
679
680 if (match != instance) {
681 /* there is no corresponding data node for the metadata */
682 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100683 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
684 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200685 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200686 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100687 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
688 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200689 }
690 ret = LY_EVALID;
691 } else {
692 /* remove the opaq attr */
693 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200694 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200695 }
696 lyd_free_tree(attr);
697 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100698
Radek Krejciddace2c2021-01-08 11:30:56 +0100699 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100700 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200701 }
702
703cleanup:
704 lydict_remove(lydctx->jsonctx->ctx, prev);
705
Radek Krejciddace2c2021-01-08 11:30:56 +0100706 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200707 return ret;
708}
709
710/**
711 * @brief Parse a metadata member.
712 *
713 * @param[in] lydctx JSON data parser context.
714 * @param[in] snode Schema node of the metadata parent.
715 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
716 * so the data node does not exists. In such a case the metadata is stored in the context for the later
717 * processing by lydjson_metadata_finish().
718 * @return LY_SUCCESS on success
719 * @return Various LY_ERR values in case of failure.
720 */
721static LY_ERR
722lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
723{
Michal Vasko09e04632023-03-22 14:34:10 +0100724 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200725 enum LYJSON_PARSER_STATUS status;
726 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200727 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200728 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200729 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 size_t name_len, prefix_len = 0;
731 struct lys_module *mod;
Radek Krejci1798aae2020-07-14 13:26:06 +0200732 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200733 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 struct lyd_node *prev = node;
Michal Vasko09d13b32022-12-01 13:49:50 +0100735 uint32_t instance = 0, val_hints;
Radek Krejci1798aae2020-07-14 13:26:06 +0200736 uint16_t nodetype;
737
738 assert(snode || node);
739
740 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
Michal Vasko66c16312022-12-01 10:37:43 +0100741 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200742
743 /* move to the second item in the name/X pair */
Michal Vasko09e04632023-03-22 14:34:10 +0100744 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200745
746 /* check attribute encoding */
747 switch (nodetype) {
748 case LYS_LEAFLIST:
749 expected = "@name/array of objects/nulls";
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
751
752next_entry:
Radek Krejci1798aae2020-07-14 13:26:06 +0200753 if (status == LYJSON_ARRAY_CLOSED) {
Michal Vasko57c8d432022-11-30 15:35:56 +0100754 /* no more metadata */
Radek Krejci1798aae2020-07-14 13:26:06 +0200755 goto cleanup;
756 }
Michal Vasko09e04632023-03-22 14:34:10 +0100757
758 /* move into the array/next item */
759 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
760 instance++;
Michal Vasko57c8d432022-11-30 15:35:56 +0100761 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_NULL), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200762
Michal Vasko69730152020-10-09 16:30:07 +0200763 if (!node || (node->schema != prev->schema)) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200764 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%u of %s:%s to be coupled with metadata.",
Michal Vasko69730152020-10-09 16:30:07 +0200765 instance, prev->schema->module->name, prev->schema->name);
Michal Vasko09e04632023-03-22 14:34:10 +0100766 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200767 goto cleanup;
768 }
769
770 if (status == LYJSON_NULL) {
771 /* continue with the next entry in the leaf-list array */
772 prev = node;
773 node = node->next;
Michal Vasko09e04632023-03-22 14:34:10 +0100774
775 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200776 goto next_entry;
777 }
778 break;
779 case LYS_LEAF:
780 case LYS_ANYXML:
781 expected = "@name/object";
782
aPiecek5b115fc2021-05-24 14:32:42 +0200783 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200784 break;
785 case LYS_CONTAINER:
786 case LYS_LIST:
787 case LYS_ANYDATA:
788 case LYS_NOTIF:
789 case LYS_ACTION:
790 case LYS_RPC:
791 in_parent = 1;
792 expected = "@/object";
793 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
794 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200795 default:
Michal Vasko66c16312022-12-01 10:37:43 +0100796 LOGINT(ctx);
Michal Vasko09e04632023-03-22 14:34:10 +0100797 rc = LY_EINT;
Michal Vasko66c16312022-12-01 10:37:43 +0100798 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200799 }
800
801 /* process all the members inside a single metadata object */
802 assert(status == LYJSON_OBJECT);
Michal Vasko09e04632023-03-22 14:34:10 +0100803 do {
804 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Michal Vasko5a713272023-03-27 09:01:43 +0200805 LY_CHECK_GOTO(status != LYJSON_OBJECT_NAME, representation_error);
806
aPiecekd2c39372021-06-16 14:03:12 +0200807 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 +0100808 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200809
Michal Vasko69d0ca12021-07-23 10:22:03 +0200810 if (!name_len) {
811 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
Michal Vasko09e04632023-03-22 14:34:10 +0100812 rc = LY_EVALID;
Michal Vasko69d0ca12021-07-23 10:22:03 +0200813 goto cleanup;
814 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100815 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100816 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Michal Vasko09e04632023-03-22 14:34:10 +0100817 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200818 goto cleanup;
819 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100820 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100821 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Michal Vasko09e04632023-03-22 14:34:10 +0100822 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200823 goto cleanup;
824 }
825
826 /* get the element module */
827 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
828 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100829 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100830 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 +0100831 (int)prefix_len, prefix, (int)name_len, name);
Michal Vasko09e04632023-03-22 14:34:10 +0100832 rc = LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200833 goto cleanup;
834 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100835 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200836 /* skip element with children */
Michal Vasko09e04632023-03-22 14:34:10 +0100837 LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup);
838 status = lyjson_ctx_status(lydctx->jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 /* end of the item */
840 continue;
841 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100842 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200843 }
844
845 /* get the value */
Michal Vasko09e04632023-03-22 14:34:10 +0100846 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Michal Vasko09d13b32022-12-01 13:49:50 +0100847
848 /* get value hints */
Michal Vasko583b4642023-05-25 10:39:34 +0200849 LY_CHECK_GOTO(rc = lydjson_value_type_hint(lydctx->jsonctx, &status, &val_hints), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200850
851 if (node->schema) {
852 /* create metadata */
Michal Vasko09e04632023-03-22 14:34:10 +0100853 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 +0100854 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, val_hints, node->schema);
Michal Vasko09e04632023-03-22 14:34:10 +0100855 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200856
857 /* add/correct flags */
Michal Vasko09e04632023-03-22 14:34:10 +0100858 rc = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL);
859 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200860 } else {
861 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200862 const char *module_name;
863 size_t module_name_len;
864
865 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
866
Radek Krejci1798aae2020-07-14 13:26:06 +0200867 /* attr2 is always changed to the created attribute */
Michal Vasko09e04632023-03-22 14:34:10 +0100868 rc = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
Michal Vasko501af032020-11-11 20:27:44 +0100869 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Michal Vasko09d13b32022-12-01 13:49:50 +0100870 LY_VALUE_JSON, NULL, val_hints);
Michal Vasko09e04632023-03-22 14:34:10 +0100871 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200872 }
Michal Vasko09e04632023-03-22 14:34:10 +0100873
Radek Krejci1798aae2020-07-14 13:26:06 +0200874 /* next member */
Michal Vasko09e04632023-03-22 14:34:10 +0100875 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
876 } while (status == LYJSON_OBJECT_NEXT);
877 LY_CHECK_GOTO(status != LYJSON_OBJECT_CLOSED, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200878
879 if (nodetype == LYS_LEAFLIST) {
880 /* continue by processing another metadata object for the following
Michal Vaskob4043552022-12-01 10:11:55 +0100881 * leaf-list instance since they are always instantiated in JSON array */
Radek Krejci1798aae2020-07-14 13:26:06 +0200882 prev = node;
883 node = node->next;
Michal Vasko09e04632023-03-22 14:34:10 +0100884
885 LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200886 goto next_entry;
887 }
888
Radek Krejci5813c362021-01-05 10:51:57 +0100889 /* success */
890 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200891
892representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100893 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200894 "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 +0100895 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
896 in_parent ? "" : "name");
Michal Vasko09e04632023-03-22 14:34:10 +0100897 rc = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100898
899cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200900 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100901 LOG_LOCBACK(1, 0, 0, 0);
Michal Vasko09e04632023-03-22 14:34:10 +0100902 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +0200903}
904
905/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200906 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
907 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200908 *
909 * @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 +0200910 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
911 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
912 * @param[in] last If set, always insert at the end.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200913 * @param[in] ext Extension instance of @p node_p, if any.
Radek Krejci1798aae2020-07-14 13:26:06 +0200914 */
915static void
Michal Vasko8cc3f662022-03-29 11:25:51 +0200916lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last,
917 struct lysc_ext_instance *ext)
Radek Krejci1798aae2020-07-14 13:26:06 +0200918{
Michal Vaskod027f382023-02-10 09:13:25 +0100919 if (!*node_p) {
920 return;
921 }
922
923 /* insert, keep first pointer correct */
924 if (ext) {
925 lyplg_ext_insert(parent, *node_p);
926 } else {
927 lyd_insert_node(parent, first_p, *node_p, last);
928 }
929 if (first_p) {
930 if (parent) {
931 *first_p = lyd_child(parent);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200932 } else {
Michal Vaskod027f382023-02-10 09:13:25 +0100933 while ((*first_p)->prev->next) {
934 *first_p = (*first_p)->prev;
Radek Krejci1798aae2020-07-14 13:26:06 +0200935 }
936 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200937 }
Michal Vaskod027f382023-02-10 09:13:25 +0100938 *node_p = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200939}
940
aPiecek4bf47212021-05-27 10:55:47 +0200941/**
942 * @brief Wrapper for ::lyd_create_opaq().
943 *
944 * @param[in] lydctx JSON data parser context.
945 * @param[in] name Name of the opaq node to create.
946 * @param[in] name_len Length of the @p name string.
947 * @param[in] prefix Prefix of the opaq node to create.
948 * @param[in] prefix_len Length of the @p prefx string.
Michal Vasko80623c52023-04-28 14:22:06 +0200949 * @param[in] parent Data parent of the opaq node, to inherit module name from.
aPiecek4bf47212021-05-27 10:55:47 +0200950 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
951 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
952 * @param[out] node_p Pointer to the created opaq node.
953 * @return LY_ERR value.
954 */
955static LY_ERR
956lydjson_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 +0100957 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +0200958{
959 LY_ERR ret = LY_SUCCESS;
960 const char *value = NULL, *module_name;
961 size_t value_len = 0, module_name_len = 0;
962 ly_bool dynamic = 0;
963 uint32_t type_hint = 0;
964
Michal Vasko09e04632023-03-22 14:34:10 +0100965 if (*status_inner_p != LYJSON_OBJECT) {
aPiecek4bf47212021-05-27 10:55:47 +0200966 /* prepare for creating opaq node with a value */
967 value = lydctx->jsonctx->value;
968 value_len = lydctx->jsonctx->value_len;
969 dynamic = lydctx->jsonctx->dynamic;
970 lydctx->jsonctx->dynamic = 0;
971
Michal Vasko583b4642023-05-25 10:39:34 +0200972 LY_CHECK_RET(lydjson_value_type_hint(lydctx->jsonctx, status_inner_p, &type_hint));
aPiecek4bf47212021-05-27 10:55:47 +0200973 }
974
Michal Vasko80623c52023-04-28 14:22:06 +0200975 /* get the module name */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100976 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
Michal Vasko80623c52023-04-28 14:22:06 +0200977 if (!module_name && !parent && lydctx->any_schema) {
978 /* in an anyxml/anydata tree, parsing first node, use the previous any schema node */
979 module_name = lydctx->any_schema->module->name;
980 module_name_len = strlen(module_name);
981 }
982
983 /* create node */
aPiecek4bf47212021-05-27 10:55:47 +0200984 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
985 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
986 if (dynamic) {
987 free((char *)value);
988 }
989
990 return ret;
991}
992
Michal Vaskoe0665742021-02-11 11:08:44 +0100993static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
994 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200995
996/**
997 * @brief Parse opaq node from the input.
998 *
999 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
1000 *
1001 * @param[in] lydctx JSON data parser context.
1002 * @param[in] name Name of the opaq node to create.
1003 * @param[in] name_len Length of the @p name string.
1004 * @param[in] prefix Prefix of the opaq node to create.
1005 * @param[in] prefix_len Length of the @p prefx string.
1006 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1007 * but must be set if @p first is not.
1008 * @param[in,out] status_p Pointer to the current status of the parser context,
1009 * since the function manipulates with the context and process the input, the status can be updated.
1010 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1011 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1012 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1013 * @param[out] node_p Pointer to the created opaq node.
1014 * @return LY_ERR value.
1015 */
1016static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001017lydjson_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 +01001018 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1019 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001020{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001021 LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001022
Michal Vasko1a85d332021-08-27 10:35:28 +02001023 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1024 /* special array null value */
1025 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1026
1027 /* must be the only item */
1028 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
1029 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1030 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
1031 return LY_EVALID;
1032 }
1033
1034 goto finish;
1035 }
1036
Michal Vasko09e04632023-03-22 14:34:10 +01001037 while (*status_p == LYJSON_ARRAY) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001038 /* process another instance of the same node */
Michal Vasko09e04632023-03-22 14:34:10 +01001039 if (*status_inner_p == LYJSON_OBJECT) {
Michal Vasko1a85d332021-08-27 10:35:28 +02001040 /* array with objects, list */
1041 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
1042
Radek Krejci1798aae2020-07-14 13:26:06 +02001043 /* but first process children of the object in the array */
Michal Vasko09e04632023-03-22 14:34:10 +01001044 do {
Michal Vaskoe0665742021-02-11 11:08:44 +01001045 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Michal Vasko09e04632023-03-22 14:34:10 +01001046 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx);
1047 } while (*status_inner_p == LYJSON_OBJECT_NEXT);
Michal Vasko1a85d332021-08-27 10:35:28 +02001048 } else {
1049 /* array with values, leaf-list */
1050 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001051 }
1052
1053 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001054 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1055 goto finish;
1056 }
Michal Vasko09e04632023-03-22 14:34:10 +01001057 assert(*status_inner_p == LYJSON_ARRAY_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001058
1059 /* continue with the next instance */
Michal Vasko09e04632023-03-22 14:34:10 +01001060 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001061 assert(node_p);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001062 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001063 LY_CHECK_RET(lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001064 }
1065
Michal Vasko09e04632023-03-22 14:34:10 +01001066 if (*status_p == LYJSON_OBJECT) {
aPiecekdbb8baf2021-05-27 11:01:51 +02001067 /* process children */
Michal Vasko09e04632023-03-22 14:34:10 +01001068 do {
aPiecekdbb8baf2021-05-27 11:01:51 +02001069 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Michal Vasko09e04632023-03-22 14:34:10 +01001070 *status_p = lyjson_ctx_status(lydctx->jsonctx);
1071 } while (*status_p == LYJSON_OBJECT_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001072 }
1073
aPiecekdbb8baf2021-05-27 11:01:51 +02001074finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001075 /* finish linking metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001076 return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001077}
1078
1079/**
aPiecek6cee5d02021-05-12 10:32:00 +02001080 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1081 *
1082 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1083 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1084 * for ::lydjson_parse_opaq().
1085 *
1086 * @param[in] lydctx JSON data parser context.
1087 * @param[in] name Name of the opaq node to create.
1088 * @param[in] name_len Length of the @p name string.
1089 * @param[in] prefix Prefix of the opaq node to create.
1090 * @param[in] prefix_len Length of the @p prefx string.
1091 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1092 * but must be set if @p first is not.
1093 * @param[in,out] status_p Pointer to the current status of the parser context,
1094 * since the function manipulates with the context and process the input, the status can be updated.
1095 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1096 * @param[out] node_p Pointer to the created opaq node.
1097 * @return LY_ERR value.
1098 */
1099static LY_ERR
Michal Vasko09e04632023-03-22 14:34:10 +01001100lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix,
1101 size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001102 struct lyd_node **first_p, struct lyd_node **node_p)
1103{
1104 enum LYJSON_PARSER_STATUS status_inner = 0;
1105
1106 /* move to the second item in the name/X pair */
1107 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1108
1109 if (*status_p == LYJSON_ARRAY) {
1110 /* move into the array */
1111 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1112 } else {
1113 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1114 status_inner = LYJSON_ERROR;
1115 }
1116
1117 if (status_inner == LYJSON_ERROR) {
1118 status_inner = *status_p;
1119 }
1120
1121 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001122 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1123 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001124
1125 return LY_SUCCESS;
1126}
1127
1128/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001129 * @brief Process the attribute container (starting by @)
1130 *
1131 * @param[in] lydctx JSON data parser context.
1132 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1133 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1134 * @param[in] name Name of the opaq node to create.
1135 * @param[in] name_len Length of the @p name string.
1136 * @param[in] prefix Prefix of the opaq node to create.
1137 * @param[in] prefix_len Length of the @p prefx string.
1138 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1139 * but must be set if @p first is not.
1140 * @param[in,out] status_p Pointer to the current status of the parser context,
1141 * since the function manipulates with the context and process the input, the status can be updated.
1142 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1143 * @param[out] node_p Pointer to the created opaq node.
1144 * @return LY_ERR value.
1145 */
1146static LY_ERR
1147lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001148 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1149 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001150{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001151 LY_ERR r;
Michal Vaskodcad5c82022-12-01 16:16:58 +01001152 const char *opaq_name, *mod_name;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001153 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001154
Michal Vaskofceac692022-12-01 13:50:27 +01001155 if (!snode && !prefix) {
1156 /* set the prefix */
1157 if (parent) {
1158 lydjson_get_node_prefix(parent, NULL, 0, &prefix, &prefix_len);
1159 } else {
1160 prefix = "";
1161 prefix_len = 0;
1162 }
1163 }
1164
1165 /* parse as an attribute to a (opaque) node */
1166 if (!attr_node) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001167 /* try to find the instance */
Michal Vaskofceac692022-12-01 13:50:27 +01001168 LY_LIST_FOR(*first_p, attr_node) {
1169 if (snode) {
1170 if (attr_node->schema) {
1171 if (attr_node->schema == snode) {
1172 break;
1173 }
1174 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001175 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1176 if (!strcmp(LYD_NAME(attr_node), snode->name) && mod_name && !strcmp(mod_name, snode->module->name)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001177 break;
1178 }
1179 }
1180 } else {
1181 if (attr_node->schema) {
1182 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) &&
1183 !ly_strncmp(attr_node->schema->module->name, prefix, prefix_len)) {
1184 break;
1185 }
1186 } else {
Michal Vaskodcad5c82022-12-01 16:16:58 +01001187 mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
1188 if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) && mod_name &&
1189 !ly_strncmp(mod_name, prefix, prefix_len)) {
Michal Vaskofceac692022-12-01 13:50:27 +01001190 break;
1191 }
1192 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001193 }
1194 }
1195 }
1196 if (!attr_node) {
1197 /* parse just as an opaq node with the name beginning with @,
1198 * later we have to check that it belongs to a standard node
1199 * and it is supposed to be converted to a metadata */
1200 uint32_t prev_opts;
1201
Radek Krejci1798aae2020-07-14 13:26:06 +02001202 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001203 prev_opts = lydctx->parse_opts;
1204 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1205 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001206
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001207 opaq_name = prefix ? prefix - 1 : name - 1;
1208 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1209 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 +02001210
1211 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001212 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001213 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001214 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001215 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001216 }
1217
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001218 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001219}
1220
1221/**
Michal Vasko76096ec2022-02-24 16:06:16 +01001222 * @brief Parse a single anydata/anyxml node.
1223 *
1224 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1225 * as before calling, despite it is necessary to process input data for checking.
1226 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001227 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko76096ec2022-02-24 16:06:16 +01001228 * @param[in,out] status JSON parser status, is updated.
1229 * @param[out] node Parsed data (or opaque) node.
1230 * @return LY_SUCCESS if a node was successfully parsed,
1231 * @return LY_ENOT in case of invalid JSON encoding,
1232 * @return LY_ERR on other errors.
1233 */
1234static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +02001235lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1236 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko76096ec2022-02-24 16:06:16 +01001237{
Michal Vaskod027f382023-02-10 09:13:25 +01001238 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko85be65e2023-06-13 09:44:17 +02001239 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
Michal Vasko76096ec2022-02-24 16:06:16 +01001240 struct ly_in in_start;
Michal Vasko7ca33252022-12-21 09:30:32 +01001241 char *val = NULL;
Michal Vasko09e04632023-03-22 14:34:10 +01001242 const char *end;
Michal Vasko85be65e2023-06-13 09:44:17 +02001243 struct lyd_node *child = NULL;
1244 ly_bool log_node = 0;
Michal Vasko76096ec2022-02-24 16:06:16 +01001245
1246 assert(snode->nodetype & LYD_NODE_ANY);
1247
Michal Vasko85be65e2023-06-13 09:44:17 +02001248 *node = NULL;
1249
Michal Vasko76096ec2022-02-24 16:06:16 +01001250 /* status check according to allowed JSON types */
1251 if (snode->nodetype == LYS_ANYXML) {
Michal Vasko09e04632023-03-22 14:34:10 +01001252 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) &&
1253 (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) &&
1254 (*status != LYJSON_NULL), LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001255 } else {
Michal Vasko09e04632023-03-22 14:34:10 +01001256 LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001257 }
1258
1259 /* create any node */
1260 switch (*status) {
1261 case LYJSON_OBJECT:
Michal Vasko85be65e2023-06-13 09:44:17 +02001262 /* create node */
1263 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
1264 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1265
1266 assert(*node);
1267 LOG_LOCSET(NULL, *node, NULL, NULL);
1268 log_node = 1;
1269
Michal Vasko76096ec2022-02-24 16:06:16 +01001270 /* parse any data tree with correct options, first backup the current options and then make the parser
1271 * process data as opaq nodes */
Michal Vasko76096ec2022-02-24 16:06:16 +01001272 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001273 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vasko76096ec2022-02-24 16:06:16 +01001274 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vasko80623c52023-04-28 14:22:06 +02001275 lydctx->any_schema = snode;
Michal Vasko76096ec2022-02-24 16:06:16 +01001276
1277 /* process the anydata content */
Michal Vasko09e04632023-03-22 14:34:10 +01001278 do {
Michal Vasko85be65e2023-06-13 09:44:17 +02001279 r = lydjson_subtree_r(lydctx, NULL, &child, NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001280 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1281
Michal Vasko09e04632023-03-22 14:34:10 +01001282 *status = lyjson_ctx_status(lydctx->jsonctx);
1283 } while (*status == LYJSON_OBJECT_NEXT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001284
Michal Vasko76096ec2022-02-24 16:06:16 +01001285 /* finish linking metadata */
Michal Vasko85be65e2023-06-13 09:44:17 +02001286 r = lydjson_metadata_finish(lydctx, &child);
Michal Vaskod027f382023-02-10 09:13:25 +01001287 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001288
Michal Vasko85be65e2023-06-13 09:44:17 +02001289 /* assign the data tree */
1290 ((struct lyd_node_any *)*node)->value.tree = child;
1291 child = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001292 break;
1293 case LYJSON_ARRAY:
1294 /* skip until the array end */
1295 in_start = *lydctx->jsonctx->in;
Michal Vasko85be65e2023-06-13 09:44:17 +02001296 LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001297
Michal Vasko09e04632023-03-22 14:34:10 +01001298 /* return back by all the WS */
1299 end = lydctx->jsonctx->in->current;
1300 while (is_jsonws(end[-1])) {
1301 --end;
1302 }
1303
Michal Vasko76096ec2022-02-24 16:06:16 +01001304 /* make a copy of the whole array and store it */
Michal Vasko09e04632023-03-22 14:34:10 +01001305 if (asprintf(&val, "[%.*s", (int)(end - in_start.current), in_start.current) == -1) {
Michal Vasko76096ec2022-02-24 16:06:16 +01001306 LOGMEM(lydctx->jsonctx->ctx);
Michal Vasko85be65e2023-06-13 09:44:17 +02001307 rc = LY_EMEM;
1308 goto cleanup;
Michal Vasko76096ec2022-02-24 16:06:16 +01001309 }
Michal Vaskod027f382023-02-10 09:13:25 +01001310 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
Michal Vasko85be65e2023-06-13 09:44:17 +02001311 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1312 val = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001313 break;
1314 case LYJSON_STRING:
1315 /* string value */
1316 if (lydctx->jsonctx->dynamic) {
Michal Vasko85be65e2023-06-13 09:44:17 +02001317 LY_CHECK_GOTO(rc = lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node), cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001318 lydctx->jsonctx->dynamic = 0;
1319 } else {
1320 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
Michal Vasko85be65e2023-06-13 09:44:17 +02001321 LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001322
Michal Vaskod027f382023-02-10 09:13:25 +01001323 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
Michal Vasko85be65e2023-06-13 09:44:17 +02001324 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1325 val = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001326 }
1327 break;
1328 case LYJSON_NUMBER:
1329 case LYJSON_FALSE:
1330 case LYJSON_TRUE:
1331 /* JSON value */
1332 assert(!lydctx->jsonctx->dynamic);
1333 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
Michal Vasko85be65e2023-06-13 09:44:17 +02001334 LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001335
Michal Vaskod027f382023-02-10 09:13:25 +01001336 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
Michal Vasko85be65e2023-06-13 09:44:17 +02001337 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1338 val = NULL;
Michal Vasko76096ec2022-02-24 16:06:16 +01001339 break;
1340 case LYJSON_NULL:
1341 /* no value */
Michal Vaskod027f382023-02-10 09:13:25 +01001342 r = lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node);
1343 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001344 break;
Michal Vasko76096ec2022-02-24 16:06:16 +01001345 default:
Michal Vasko85be65e2023-06-13 09:44:17 +02001346 LOGINT(lydctx->jsonctx->ctx);
1347 rc = LY_EINT;
1348 goto cleanup;
Michal Vasko76096ec2022-02-24 16:06:16 +01001349 }
1350
Michal Vaskod027f382023-02-10 09:13:25 +01001351cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +02001352 if (log_node) {
1353 LOG_LOCBACK(0, 1, 0, 0);
1354 }
1355 lydctx->parse_opts = prev_parse_opts;
1356 lydctx->int_opts = prev_int_opts;
1357 lydctx->any_schema = NULL;
Michal Vasko7ca33252022-12-21 09:30:32 +01001358 free(val);
Michal Vasko85be65e2023-06-13 09:44:17 +02001359 lyd_free_tree(child);
Michal Vasko7ca33252022-12-21 09:30:32 +01001360 return rc;
Michal Vasko76096ec2022-02-24 16:06:16 +01001361}
1362
1363/**
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001364 * @brief Parse a single instance of an inner node.
1365 *
1366 * @param[in] lydctx JSON data parser context.
1367 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1368 * @param[in] ext Extension instance of @p snode, if any.
1369 * @param[in,out] status JSON parser status, is updated.
1370 * @param[out] node Parsed data (or opaque) node.
1371 * @return LY_SUCCESS if a node was successfully parsed,
1372 * @return LY_ENOT in case of invalid JSON encoding,
1373 * @return LY_ERR on other errors.
1374 */
1375static LY_ERR
1376lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1377 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
1378{
Michal Vaskod027f382023-02-10 09:13:25 +01001379 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001380 uint32_t prev_parse_opts = lydctx->parse_opts;
1381
Michal Vasko09e04632023-03-22 14:34:10 +01001382 LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001383
1384 /* create inner node */
1385 LY_CHECK_RET(lyd_create_inner(snode, node));
1386
1387 /* use it for logging */
1388 LOG_LOCSET(NULL, *node, NULL, NULL);
1389
1390 if (ext) {
1391 /* only parse these extension data and validate afterwards */
1392 lydctx->parse_opts |= LYD_PARSE_ONLY;
1393 }
1394
1395 /* process children */
Michal Vasko09e04632023-03-22 14:34:10 +01001396 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001397 r = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
1398 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1399
Michal Vasko09e04632023-03-22 14:34:10 +01001400 *status = lyjson_ctx_status(lydctx->jsonctx);
1401 } while (*status == LYJSON_OBJECT_NEXT);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001402
1403 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001404 r = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
1405 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001406
1407 if (snode->nodetype == LYS_LIST) {
1408 /* check all keys exist */
Michal Vaskod027f382023-02-10 09:13:25 +01001409 r = lyd_parse_check_keys(*node);
1410 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001411 }
1412
1413 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1414 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +01001415 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
1416 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001417
1418 /* add any missing default children */
Michal Vaskod027f382023-02-10 09:13:25 +01001419 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
1420 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1421 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001422 }
1423
1424cleanup:
1425 lydctx->parse_opts = prev_parse_opts;
1426 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001427 if (!(*node)->hash) {
1428 /* list without keys is unusable */
1429 lyd_free_tree(*node);
1430 *node = NULL;
1431 }
Michal Vaskod027f382023-02-10 09:13:25 +01001432 return rc;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001433}
1434
1435/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001436 * @brief Parse a single instance of a node.
1437 *
1438 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1439 * as before calling, despite it is necessary to process input data for checking.
1440 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1441 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1442 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001443 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko32ac9942020-08-12 14:35:12 +02001444 * @param[in] name Parsed JSON node name.
1445 * @param[in] name_len Lenght of @p name.
1446 * @param[in] prefix Parsed JSON node prefix.
1447 * @param[in] prefix_len Length of @p prefix.
1448 * @param[in,out] status JSON parser status, is updated.
1449 * @param[out] node Parsed data (or opaque) node.
1450 * @return LY_SUCCESS if a node was successfully parsed,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001451 * @return LY_ENOT in case of invalid JSON encoding,
Michal Vasko32ac9942020-08-12 14:35:12 +02001452 * @return LY_ERR on other errors.
1453 */
1454static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001455lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001456 const struct lysc_node *snode, struct lysc_ext_instance *ext, const char *name, size_t name_len,
1457 const char *prefix, size_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001458{
Michal Vaskod027f382023-02-10 09:13:25 +01001459 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +01001460 uint32_t type_hints = 0;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001461
1462 LOG_LOCSET(snode, NULL, NULL, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001463
Michal Vaskod027f382023-02-10 09:13:25 +01001464 r = lydjson_data_check_opaq(lydctx, snode, &type_hints);
1465 if (r == LY_SUCCESS) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001466 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1467 if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001468 if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
1469 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) {
Michal Vaskod027f382023-02-10 09:13:25 +01001470 rc = LY_ENOT;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001471 goto cleanup;
1472 }
Michal Vaskobbdadda2022-01-06 11:40:10 +01001473
Michal Vasko32ac9942020-08-12 14:35:12 +02001474 /* create terminal node */
Michal Vaskod027f382023-02-10 09:13:25 +01001475 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1476 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, type_hints, node);
1477 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001478
1479 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001480 if (*status == LYJSON_ARRAY) {
1481 /* only [null], 2 more moves are needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001482 r = lyjson_ctx_next(lydctx->jsonctx, status);
1483 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001484 assert(*status == LYJSON_NULL);
Michal Vaskod027f382023-02-10 09:13:25 +01001485
1486 r = lyjson_ctx_next(lydctx->jsonctx, status);
1487 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001488 assert(*status == LYJSON_ARRAY_CLOSED);
1489 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001490 } else if (snode->nodetype & LYD_NODE_INNER) {
1491 /* create inner node */
Michal Vaskod027f382023-02-10 09:13:25 +01001492 r = lydjson_parse_instance_inner(lydctx, snode, ext, status, node);
1493 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001494 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001495 /* create any node */
Michal Vaskod027f382023-02-10 09:13:25 +01001496 r = lydjson_parse_any(lydctx, snode, ext, status, node);
1497 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001498 }
Michal Vaskod027f382023-02-10 09:13:25 +01001499 LY_CHECK_GOTO(!*node, cleanup);
Michal Vaskocaf608d2021-07-23 13:51:23 +02001500
1501 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001502 r = lyd_parse_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext);
1503 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001504
Michal Vaskoeba23112022-08-26 08:35:41 +02001505 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1506 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001507 r = lyd_validate_node_ext(*node, &lydctx->ext_node);
1508 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001509 }
Michal Vaskod027f382023-02-10 09:13:25 +01001510 } else if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001511 /* parse it again as an opaq node */
Michal Vaskod027f382023-02-10 09:13:25 +01001512 r = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
1513 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001514
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001515 if (snode->nodetype == LYS_LIST) {
1516 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1517 } else if (snode->nodetype == LYS_LEAFLIST) {
1518 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001519 }
Michal Vaskoa32b6b72022-07-11 15:55:56 +02001520 } else {
1521 /* error */
Michal Vasko1e8746a2023-02-13 09:15:16 +01001522 rc = r;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001523 goto cleanup;
Michal Vasko32ac9942020-08-12 14:35:12 +02001524 }
1525
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001526cleanup:
1527 LOG_LOCBACK(1, 0, 0, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001528 return rc;
Michal Vasko32ac9942020-08-12 14:35:12 +02001529}
1530
1531/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001532 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001533 *
1534 * @param[in] lydctx JSON data parser context.
1535 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1536 * @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 +01001537 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001538 * @return LY_ERR value.
1539 */
1540static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001541lydjson_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 +02001542{
Michal Vaskod027f382023-02-10 09:13:25 +01001543 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko09e04632023-03-22 14:34:10 +01001544 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx);
aPiecek49e2a3a2021-05-13 10:36:46 +02001545 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001546 size_t name_len, prefix_len = 0;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001547 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001548 const struct lysc_node *snode = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001549 struct lysc_ext_instance *ext;
Michal Vasko32ac9942020-08-12 14:35:12 +02001550 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001551 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001552 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001553
1554 assert(parent || first_p);
Michal Vasko09e04632023-03-22 14:34:10 +01001555 assert((status == LYJSON_OBJECT) || (status == LYJSON_OBJECT_NEXT));
Radek Krejci1798aae2020-07-14 13:26:06 +02001556
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001557 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1558 /* all descendants should be parsed */
1559 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1560
Michal Vasko09e04632023-03-22 14:34:10 +01001561 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1562 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1563 if (status == LYJSON_OBJECT_CLOSED) {
1564 /* empty object, fine... */
1565 goto cleanup;
1566 }
1567
Radek Krejci1798aae2020-07-14 13:26:06 +02001568 /* process the node name */
Michal Vasko09e04632023-03-22 14:34:10 +01001569 assert(status == LYJSON_OBJECT_NAME);
aPiecekd2c39372021-06-16 14:03:12 +02001570 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 +01001571 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001572
Michal Vaskocabe0702020-08-12 10:14:36 +02001573 if (!is_meta || name_len || prefix_len) {
1574 /* get the schema node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001575 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001576 if (r == LY_ENOT) {
1577 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001578 goto cleanup;
Michal Vasko514f6982023-02-17 09:07:39 +01001579 } else if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1580 rc = r;
1581
1582 /* skip the invalid data */
1583 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1584 rc = r;
1585 }
Michal Vasko1241ebc2023-04-04 10:03:45 +02001586 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1587 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko514f6982023-02-17 09:07:39 +01001588 goto cleanup;
1589 } else if (r) {
1590 /* error */
1591 rc = r;
1592 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001593 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001594
Michal Vaskocabe0702020-08-12 10:14:36 +02001595 if (!snode) {
1596 /* we will not be parsing it as metadata */
1597 is_meta = 0;
1598 }
1599 }
1600
1601 if (is_meta) {
1602 /* parse as metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001603 if (!name_len && !prefix_len && !parent) {
1604 LOGVAL(ctx, LYVE_SYNTAX_JSON,
1605 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
1606 r = LY_EVALID;
1607 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1608 } else if (!name_len && !prefix_len) {
Michal Vaskocabe0702020-08-12 10:14:36 +02001609 /* parent's metadata without a name - use the schema from the parent */
Michal Vaskoe0665742021-02-11 11:08:44 +01001610 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001611 snode = attr_node->schema;
1612 }
Michal Vaskod027f382023-02-10 09:13:25 +01001613 r = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001614 first_p, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001615 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001616 } else if (!snode) {
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001617 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
1618 /* skip element with children */
1619 r = lydjson_data_skip(lydctx->jsonctx);
1620 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1621 } else {
1622 /* parse as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001623
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001624 /* opaq node cannot have an empty string as the name. */
1625 if (name_len == 0) {
Michal Vaskobfebf662023-02-17 09:07:22 +01001626 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "JSON object member name cannot be a zero-length string.");
Michal Vasko87c5d6b2023-02-14 15:27:41 +01001627 r = LY_EVALID;
1628 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1629 }
1630
1631 /* move to the second item in the name/X pair and parse opaq */
1632 r = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node);
1633 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
aPiecekb7b29e62021-05-11 10:02:43 +02001634 }
Michal Vaskocabe0702020-08-12 10:14:36 +02001635 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001636 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001637
1638 /* move to the second item in the name/X pair */
Michal Vaskod027f382023-02-10 09:13:25 +01001639 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1640 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001641
Michal Vasko7ca33252022-12-21 09:30:32 +01001642 /* set expected representation */
1643 switch (snode->nodetype) {
1644 case LYS_LEAFLIST:
1645 expected = "name/array of values";
1646 break;
1647 case LYS_LIST:
1648 expected = "name/array of objects";
1649 break;
1650 case LYS_LEAF:
1651 if (status == LYJSON_ARRAY) {
1652 expected = "name/[null]";
1653 } else {
1654 expected = "name/value";
1655 }
1656 break;
1657 case LYS_CONTAINER:
1658 case LYS_NOTIF:
1659 case LYS_ACTION:
1660 case LYS_RPC:
1661 case LYS_ANYDATA:
1662 expected = "name/object";
1663 break;
1664 case LYS_ANYXML:
1665 if (status == LYJSON_ARRAY) {
1666 expected = "name/array";
1667 } else {
1668 expected = "name/value";
1669 }
1670 break;
1671 }
1672
1673 /* check the representation according to the nodetype and then continue with the content */
Radek Krejci1798aae2020-07-14 13:26:06 +02001674 switch (snode->nodetype) {
1675 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001676 case LYS_LIST:
Radek Krejci1798aae2020-07-14 13:26:06 +02001677 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1678
Michal Vasko32ac9942020-08-12 14:35:12 +02001679 /* process all the values/objects */
1680 do {
Michal Vasko09e04632023-03-22 14:34:10 +01001681 /* move into array/next value */
1682 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1683 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1684 if (status == LYJSON_ARRAY_CLOSED) {
1685 /* empty array, fine... */
1686 break;
1687 }
1688
Michal Vaskod027f382023-02-10 09:13:25 +01001689 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001690 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001691 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001692 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001693 }
Michal Vaskod027f382023-02-10 09:13:25 +01001694 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1695
Michal Vasko8cc3f662022-03-29 11:25:51 +02001696 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Radek Krejci1798aae2020-07-14 13:26:06 +02001697
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001698 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001699 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1700 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko09e04632023-03-22 14:34:10 +01001701 } while (status == LYJSON_ARRAY_NEXT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001702
1703 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001704 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001705 case LYS_CONTAINER:
1706 case LYS_NOTIF:
1707 case LYS_ACTION:
1708 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001709 case LYS_ANYDATA:
1710 case LYS_ANYXML:
Michal Vasko32ac9942020-08-12 14:35:12 +02001711 /* process the value/object */
Michal Vaskod027f382023-02-10 09:13:25 +01001712 r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001713 &status, &node);
Michal Vaskod027f382023-02-10 09:13:25 +01001714 if (r == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001715 goto representation_error;
Radek Krejci1798aae2020-07-14 13:26:06 +02001716 }
Michal Vaskod027f382023-02-10 09:13:25 +01001717 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001718
Michal Vasko32ac9942020-08-12 14:35:12 +02001719 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001720 /* rememeber the RPC/action/notification */
1721 lydctx->op_node = node;
1722 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001723 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001724 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001725 }
1726
Michal Vasko32ac9942020-08-12 14:35:12 +02001727 /* finally connect the parsed node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001728 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Michal Vaskoe0665742021-02-11 11:08:44 +01001729
1730 /* rememeber a successfully parsed node */
Michal Vasko4e26adc2022-03-30 11:01:16 +02001731 if (parsed && node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001732 ly_set_add(parsed, node, 1, NULL);
1733 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001734
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001735 if (!parse_subtree) {
1736 /* move after the item(s) */
Michal Vaskod027f382023-02-10 09:13:25 +01001737 r = lyjson_ctx_next(lydctx->jsonctx, &status);
1738 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001739 }
1740
Radek Krejci5813c362021-01-05 10:51:57 +01001741 /* success */
1742 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001743
1744representation_error:
Michal Vaskobfebf662023-02-17 09:07:22 +01001745 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as name/%s.",
1746 expected, lys_nodetype2str(snode->nodetype), snode->name, lyjson_token2str(status));
Michal Vaskod027f382023-02-10 09:13:25 +01001747 rc = LY_EVALID;
1748 if (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) {
1749 /* try to skip the invalid data */
Michal Vasko560f9de2023-02-10 09:56:01 +01001750 if ((r = lydjson_data_skip(lydctx->jsonctx))) {
1751 rc = r;
1752 }
Michal Vaskod027f382023-02-10 09:13:25 +01001753 }
Radek Krejci5813c362021-01-05 10:51:57 +01001754
1755cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001756 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001757 lyd_free_tree(node);
Michal Vaskod027f382023-02-10 09:13:25 +01001758 return rc;
Radek Krejci1798aae2020-07-14 13:26:06 +02001759}
1760
1761/**
1762 * @brief Common start of JSON parser processing different types of the input data.
1763 *
1764 * @param[in] ctx libyang context
1765 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001766 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1767 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001768 * @param[out] lydctx_p Data parser context to finish validation.
1769 * @return LY_ERR value.
1770 */
1771static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001772lyd_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 +01001773 struct lyd_json_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001774{
1775 LY_ERR ret = LY_SUCCESS;
1776 struct lyd_json_ctx *lydctx;
Michal Vasko09e04632023-03-22 14:34:10 +01001777 enum LYJSON_PARSER_STATUS status;
Radek Krejci1798aae2020-07-14 13:26:06 +02001778
Radek Krejcicd5f6222020-11-12 08:54:13 +01001779 assert(lydctx_p);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001780
Radek Krejci1798aae2020-07-14 13:26:06 +02001781 /* init context */
1782 lydctx = calloc(1, sizeof *lydctx);
1783 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001784 lydctx->parse_opts = parse_opts;
1785 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001786 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001787
Michal Vasko09e04632023-03-22 14:34:10 +01001788 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
1789 status = lyjson_ctx_status(lydctx->jsonctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001790
Michal Vasko09e04632023-03-22 14:34:10 +01001791 /* parse_opts & LYD_PARSE_SUBTREE not implemented */
1792 if (status != LYJSON_OBJECT) {
Radek Krejcicd5f6222020-11-12 08:54:13 +01001793 /* expecting top-level object */
Michal Vasko09e04632023-03-22 14:34:10 +01001794 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001795 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001796 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001797 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001798 }
Michal Vasko09e04632023-03-22 14:34:10 +01001799
1800 *lydctx_p = lydctx;
1801 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001802}
1803
1804LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001805lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001806 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 +01001807 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001808{
Michal Vaskod027f382023-02-10 09:13:25 +01001809 LY_ERR r, rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001810 struct lyd_json_ctx *lydctx = NULL;
1811 enum LYJSON_PARSER_STATUS status;
1812
Michal Vasko09e04632023-03-22 14:34:10 +01001813 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx);
1814 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001815
Michal Vaskoe0665742021-02-11 11:08:44 +01001816 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001817 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001818
1819 /* find the operation node if it exists already */
1820 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1821
Radek Krejci1798aae2020-07-14 13:26:06 +02001822 /* read subtree(s) */
Michal Vasko09e04632023-03-22 14:34:10 +01001823 do {
Michal Vaskod027f382023-02-10 09:13:25 +01001824 r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1825 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001826
Michal Vasko09e04632023-03-22 14:34:10 +01001827 status = lyjson_ctx_status(lydctx->jsonctx);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001828
1829 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001830 break;
1831 }
Michal Vasko09e04632023-03-22 14:34:10 +01001832 } while (status == LYJSON_OBJECT_NEXT);
Michal Vasko6a5e4692023-03-01 14:41:39 +01001833 assert((status == LYJSON_END) || (status == LYJSON_OBJECT_CLOSED));
Michal Vaskoe0665742021-02-11 11:08:44 +01001834
1835 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
Michal Vasko09e04632023-03-22 14:34:10 +01001836 (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_OBJECT_CLOSED)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001837 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001838 r = LY_EVALID;
1839 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001840 }
1841 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1842 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001843 r = LY_EVALID;
1844 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001845 }
1846
1847 /* finish linking metadata */
Michal Vaskod027f382023-02-10 09:13:25 +01001848 r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1849 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001850
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001851 if (parse_opts & LYD_PARSE_SUBTREE) {
1852 /* check for a sibling object */
1853 assert(subtree_sibling);
1854 if (lydctx->jsonctx->in->current[0] == ',') {
1855 *subtree_sibling = 1;
1856
1857 /* move to the next object */
1858 ly_in_skip(lydctx->jsonctx->in, 1);
1859 } else {
1860 *subtree_sibling = 0;
1861 }
1862 }
1863
Radek Krejci1798aae2020-07-14 13:26:06 +02001864cleanup:
1865 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001866 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001867 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001868
Michal Vaskod027f382023-02-10 09:13:25 +01001869 if (rc && (!lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001870 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001871 } else {
1872 *lydctx_p = (struct lyd_ctx *)lydctx;
Michal Vaskof8ebf132022-11-21 14:06:48 +01001873
1874 /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1875 lyjson_ctx_free(lydctx->jsonctx);
1876 lydctx->jsonctx = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001877 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001878 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001879}
Michal Vasko820efe82023-05-12 15:47:43 +02001880
1881/**
1882 * @brief Parse a specific JSON object into an opaque node.
1883 *
1884 * @param[in] jsonctx JSON parser context.
1885 * @param[in] name Name of the object.
1886 * @param[in] module Module name of the object, NULL if none expected.
1887 * @param[in] value Whether a value is expected.
1888 * @param[out] evnp Parsed envelope (opaque node).
1889 * @return LY_SUCCESS on success.
1890 * @return LY_ENOT if the specified object did not match.
1891 * @return LY_ERR value on error.
1892 */
1893static LY_ERR
1894lydjson_envelope(struct lyjson_ctx *jsonctx, const char *name, const char *module, ly_bool value, struct lyd_node **envp)
1895{
1896 LY_ERR rc = LY_SUCCESS, r;
1897 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx);
1898 const char *nam, *prefix, *val = NULL;
1899 size_t nam_len, prefix_len, val_len = 0;
1900 ly_bool is_meta;
1901
1902 assert(status == LYJSON_OBJECT);
1903
1904 *envp = NULL;
1905
1906 r = lyjson_ctx_next(jsonctx, &status);
1907 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1908 if (status == LYJSON_OBJECT_CLOSED) {
1909 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Empty JSON object.");
1910 rc = LY_EVALID;
1911 goto cleanup;
1912 }
1913
1914 /* process the name */
1915 assert(status == LYJSON_OBJECT_NAME);
1916 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &nam, &nam_len, &prefix, &prefix_len, &is_meta);
1917 if (is_meta) {
1918 LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected metadata.");
1919 rc = LY_EVALID;
1920 goto cleanup;
1921 } else if (module && ly_strncmp(module, prefix, prefix_len)) {
1922 LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected module \"%.*s\" instead of \"%s\".", (int)prefix_len, prefix, module);
1923 rc = LY_EVALID;
1924 goto cleanup;
1925 } else if (ly_strncmp(name, nam, nam_len)) {
1926 LOGVAL(jsonctx->ctx, LYVE_DATA, "Unexpected object \"%.*s\" instead of \"%s\".", (int)nam_len, nam, name);
1927 rc = LY_EVALID;
1928 goto cleanup;
1929 }
1930
1931 r = lyjson_ctx_next(jsonctx, &status);
1932 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1933
1934 if (value) {
1935 if (status != LYJSON_STRING) {
1936 LOGVAL(jsonctx->ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_STRING),
1937 lyjson_token2str(status));
1938 rc = LY_EVALID;
1939 goto cleanup;
1940 }
1941
1942 val = jsonctx->value;
1943 val_len = jsonctx->value_len;
1944
1945 r = lyjson_ctx_next(jsonctx, &status);
1946 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1947 }
1948
1949 /* create node */
1950 rc = lyd_create_opaq(jsonctx->ctx, name, strlen(name), prefix, prefix_len, prefix, prefix_len, val, val_len,
1951 NULL, LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, envp);
1952 LY_CHECK_GOTO(rc, cleanup);
1953
1954cleanup:
1955 if (rc) {
1956 lyd_free_tree(*envp);
1957 *envp = NULL;
1958 }
1959 return rc;
1960}
1961
1962/**
1963 * @brief Parse all expected non-data JSON objects of a RESTCONF notification message.
1964 *
1965 * @param[in] jsonctx JSON parser context.
1966 * @param[out] evnp Parsed envelope(s) (opaque node).
1967 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1968 * @param[out] close_elem Number of parsed opened objects that need to be closed.
1969 * @return LY_SUCCESS on success.
1970 * @return LY_ERR value on error.
1971 */
1972static LY_ERR
1973lydjson_env_restconf_notif(struct lyjson_ctx *jsonctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1974{
1975 LY_ERR rc = LY_SUCCESS, r;
1976 struct lyd_node *child;
1977
1978 assert(envp && !*envp);
1979
1980 /* parse "notification" */
1981 r = lydjson_envelope(jsonctx, "notification", "ietf-restconf", 0, envp);
1982 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1983
1984 /* parse "eventTime" */
1985 r = lydjson_envelope(jsonctx, "eventTime", NULL, 1, &child);
1986 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1987
1988 /* insert */
1989 lyd_insert_node(*envp, NULL, child, 0);
1990
1991 /* validate value */
1992 r = lyd_parser_notif_eventtime_validate(child);
1993 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1994
1995 /* RESTCONF notification */
1996 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_NOTIF;
1997 *close_elem = 1;
1998
1999cleanup:
2000 if (rc) {
2001 lyd_free_tree(*envp);
2002 *envp = NULL;
2003 }
2004 return rc;
2005}
2006
2007LY_ERR
2008lyd_parse_json_restconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
2009 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
2010 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
2011{
2012 LY_ERR rc = LY_SUCCESS, r;
2013 struct lyd_json_ctx *lydctx;
2014 enum LYJSON_PARSER_STATUS status;
2015 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vasko820efe82023-05-12 15:47:43 +02002016
2017 assert(ctx && in && lydctx_p);
2018 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
2019 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
2020
2021 assert((data_type == LYD_TYPE_RPC_RESTCONF) || (data_type == LYD_TYPE_NOTIF_RESTCONF) ||
2022 (data_type == LYD_TYPE_REPLY_RESTCONF));
2023 assert(!(parse_opts & LYD_PARSE_SUBTREE));
2024
2025 /* init context */
2026 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx);
2027 LY_CHECK_GOTO(rc, cleanup);
2028 lydctx->ext = ext;
2029
2030 switch (data_type) {
2031 case LYD_TYPE_RPC_RESTCONF:
2032 assert(parent);
2033
2034 /* parse "input" */
2035 rc = lydjson_envelope(lydctx->jsonctx, "input", lyd_owner_module(parent)->name, 0, envp);
2036 LY_CHECK_GOTO(rc, cleanup);
2037
2038 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
2039 close_elem = 1;
2040 break;
2041 case LYD_TYPE_NOTIF_RESTCONF:
2042 assert(!parent);
2043 rc = lydjson_env_restconf_notif(lydctx->jsonctx, envp, &int_opts, &close_elem);
2044 LY_CHECK_GOTO(rc, cleanup);
2045 break;
2046 case LYD_TYPE_REPLY_RESTCONF:
2047 assert(parent);
2048
2049 /* parse "output" */
2050 rc = lydjson_envelope(lydctx->jsonctx, "output", lyd_owner_module(parent)->name, 0, envp);
2051 LY_CHECK_GOTO(rc, cleanup);
2052
2053 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
2054 close_elem = 1;
2055 break;
2056 default:
2057 LOGINT(ctx);
2058 rc = LY_EINT;
2059 goto cleanup;
2060 }
2061
2062 lydctx->int_opts = int_opts;
2063
2064 /* find the operation node if it exists already */
2065 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
2066
2067 /* read subtree(s) */
2068 do {
2069 r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
2070 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko820efe82023-05-12 15:47:43 +02002071
2072 status = lyjson_ctx_status(lydctx->jsonctx);
2073
2074 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
2075 break;
2076 }
2077 } while (status == LYJSON_OBJECT_NEXT);
2078 assert((status == LYJSON_END) || (status == LYJSON_OBJECT_CLOSED));
2079
2080 /* close all opened elements */
2081 for (i = 0; i < close_elem; ++i) {
2082 if (status != LYJSON_OBJECT_CLOSED) {
2083 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_OBJECT_CLOSED),
2084 lyjson_token2str(status));
2085 rc = LY_EVALID;
2086 goto cleanup;
2087 }
2088
2089 r = lyjson_ctx_next(lydctx->jsonctx, &status);
2090 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2091 }
2092
2093 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
2094 (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_OBJECT_CLOSED)) {
2095 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
2096 r = LY_EVALID;
2097 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
2098 }
2099 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
2100 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
2101 r = LY_EVALID;
2102 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
2103 }
2104
2105 /* finish linking metadata */
2106 r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
2107 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
2108
Michal Vasko820efe82023-05-12 15:47:43 +02002109cleanup:
2110 /* there should be no unres stored if validation should be skipped */
2111 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
2112 !lydctx->node_when.count));
2113
2114 if (rc) {
2115 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
2116 } else {
2117 *lydctx_p = (struct lyd_ctx *)lydctx;
2118
2119 /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */
2120 lyjson_ctx_free(lydctx->jsonctx);
2121 lydctx->jsonctx = NULL;
2122 }
2123 return rc;
2124}