blob: c9fc57307cf45e4036d681fa54fcfb48605f68d5 [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko61ad1ff2022-02-10 15:48:39 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko90932a92020-02-12 14:33:03 +01005 * @brief JSON data parser for libyang
6 *
Michal Vasko61ad1ff2022-02-10 15:48:39 +01007 * Copyright (c) 2020 - 2022 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010017
Radek Krejci1798aae2020-07-14 13:26:06 +020018#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010020#include <stdlib.h>
21#include <string.h>
22
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "common.h"
Michal Vaskobbdadda2022-01-06 11:40:10 +010024#include "compat.h"
Michal Vasko90932a92020-02-12 14:33:03 +010025#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010026#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020027#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020028#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010029#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010030#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020031#include "parser_internal.h"
Michal Vasko61ad1ff2022-02-10 15:48:39 +010032#include "plugins_exts.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "set.h"
34#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020035#include "tree_data.h"
36#include "tree_data_internal.h"
37#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010038#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020039#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010040
41/**
Radek Krejci1798aae2020-07-14 13:26:06 +020042 * @brief Free the JSON data parser context.
43 *
44 * JSON implementation of lyd_ctx_free_clb().
45 */
46static void
47lyd_json_ctx_free(struct lyd_ctx *lydctx)
48{
49 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
50
51 if (lydctx) {
52 lyd_ctx_free(lydctx);
53 lyjson_ctx_free(ctx->jsonctx);
54 free(ctx);
55 }
56}
57
58/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +010059 * @brief Pass the responsibility for releasing the dynamic values to @p dst.
aPiecekd2c39372021-06-16 14:03:12 +020060 *
61 * @param[in] jsonctx JSON context which contains the dynamic value.
62 * @param[in,out] dst Pointer to which the responsibility will be submited.
Michal Vasko61ad1ff2022-02-10 15:48:39 +010063 * If the pointer is already pointing to some allocated memory, it is released beforehand.
aPiecekd2c39372021-06-16 14:03:12 +020064 */
65static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010066lyjson_ctx_give_dynamic_value(struct lyjson_ctx *jsonctx, char **dst)
aPiecekd2c39372021-06-16 14:03:12 +020067{
68 assert(jsonctx && dst);
69
70 if (!jsonctx->dynamic) {
71 return;
72 }
73
74 if (dst) {
75 free(*dst);
76 }
77 *dst = NULL;
78
Michal Vasko61ad1ff2022-02-10 15:48:39 +010079 /* give the dynamic value */
aPiecekd2c39372021-06-16 14:03:12 +020080 *dst = (char *)jsonctx->value;
81
Michal Vasko61ad1ff2022-02-10 15:48:39 +010082 /* responsibility for the release is now passed to dst */
aPiecekd2c39372021-06-16 14:03:12 +020083 jsonctx->dynamic = 0;
84}
85
86/**
Radek Krejci1798aae2020-07-14 13:26:06 +020087 * @brief Parse JSON member-name as [\@][prefix:][name]
88 *
Michal Vaskocabe0702020-08-12 10:14:36 +020089 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020090 * prefix - name of the module of the data node
91 * name - name of the data node
92 *
93 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
94 *
95 * @param[in] value String to parse
96 * @param[in] value_len Length of the @p str.
97 * @param[out] name_p Pointer to the beginning of the parsed name.
98 * @param[out] name_len_p Pointer to the length of the parsed name.
99 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
100 * @param[out] prefix_len_p Pointer to the length of the parsed prefix. If the member-name does not contain prefix, result is 0.
Michal Vaskocabe0702020-08-12 10:14:36 +0200101 * @param[out] is_meta_p Pointer to the metadata flag, set to 1 if the member-name contains \@, 0 otherwise.
Radek Krejci1798aae2020-07-14 13:26:06 +0200102 */
103static void
Michal Vaskocabe0702020-08-12 10:14:36 +0200104lydjson_parse_name(const char *value, size_t value_len, const char **name_p, size_t *name_len_p, const char **prefix_p,
Radek Krejci857189e2020-09-01 13:26:36 +0200105 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200106{
107 const char *name, *prefix = NULL;
108 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200109 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200110
111 name = memchr(value, ':', value_len);
112 if (name != NULL) {
113 prefix = value;
114 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200115 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200116 prefix++;
117 }
118 prefix_len = name - prefix;
119 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200120 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200121 } else {
122 name = value;
123 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200124 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200125 name++;
126 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200127 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100128 }
129
Radek Krejci1798aae2020-07-14 13:26:06 +0200130 *name_p = name;
131 *name_len_p = name_len;
132 *prefix_p = prefix;
133 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200134 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200135}
136
137/**
138 * @brief Get correct prefix (module_name) inside the @p node.
139 *
140 * @param[in] node Data node to get inherited prefix.
141 * @param[in] local_prefix Local prefix to replace the inherited prefix.
142 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
143 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
144 * and no context @p node to get inherited prefix.
145 * @param[out] prefix_len_p Pointer to the length of the resulting @p prefix_p string. Note that the result can be 0 in case
146 * of no local prefix and no context @p node to get inherited prefix.
147 * @return LY_ERR value.
148 */
149static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200150lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, size_t local_prefix_len, const char **prefix_p,
Radek Krejci0f969882020-08-21 16:56:47 +0200151 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200152{
153 struct lyd_node_opaq *onode;
154 const char *module_name = NULL;
155
156 assert(prefix_p && prefix_len_p);
157
158 if (local_prefix_len) {
159 *prefix_p = local_prefix;
160 *prefix_len_p = local_prefix_len;
161 return LY_SUCCESS;
162 }
163
164 *prefix_p = NULL;
165 while (node) {
166 if (node->schema) {
167 *prefix_p = node->schema->module->name;
168 break;
169 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200170 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100171 if (onode->name.module_name) {
172 *prefix_p = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200173 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100174 } else if (onode->name.prefix) {
175 *prefix_p = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200176 break;
177 }
Michal Vasko9e685082021-01-29 14:49:09 +0100178 node = lyd_parent(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200179 }
180 *prefix_len_p = ly_strlen(module_name);
181
182 return LY_SUCCESS;
183}
184
185/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100186 * @brief Try to parse data with a parent based on an extension instance.
Radek Krejci1798aae2020-07-14 13:26:06 +0200187 *
188 * @param[in] lydctx JSON data parser context.
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100189 * @param[in,out] parent Parent node where the children are inserted.
190 * @return LY_SUCCESS on success;
191 * @return LY_ENOT if no extension instance parsed the data;
192 * @return LY_ERR on error.
Radek Krejci1798aae2020-07-14 13:26:06 +0200193 */
194static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100195lydjson_nested_ext(struct lyd_json_ctx *lydctx, struct lyd_node *parent)
Radek Krejci1798aae2020-07-14 13:26:06 +0200196{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100197 LY_ERR r;
198 struct ly_in in_bck, in_start, in_ext;
199 LY_ARRAY_COUNT_TYPE u;
200 struct lysc_ext_instance *nested_exts = NULL;
201 lyplg_ext_data_parse_clb ext_parse_cb;
202 struct lyd_ctx_ext_val *ext_val;
203 uint32_t quot_count;
Radek Krejci1798aae2020-07-14 13:26:06 +0200204
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100205 /* backup current input */
206 in_bck = *lydctx->jsonctx->in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200207
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100208 /* go back in the input for extension parsing */
209 in_start = *lydctx->jsonctx->in;
210 assert(lyjson_ctx_status(lydctx->jsonctx, 0) == LYJSON_OBJECT);
211 quot_count = 0;
212 do {
213 --in_start.current;
214 if ((in_start.current[0] == '\"') && (in_start.current[-1] != '\\')) {
215 ++quot_count;
Radek Krejci1798aae2020-07-14 13:26:06 +0200216 }
Michal Vasko45e3a9a2022-02-14 08:49:51 +0100217 if (in_start.current == in_start.start) {
218 /* invalid JSON */
219 return LY_ENOT;
220 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100221 } while (quot_count < 2);
222
223 /* check if there are any nested extension instances */
224 if (parent && parent->schema) {
225 nested_exts = parent->schema->exts;
Radek Krejci1798aae2020-07-14 13:26:06 +0200226 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100227 LY_ARRAY_FOR(nested_exts, u) {
228 /* prepare the input and try to parse this extension data */
229 in_ext = in_start;
230 ext_parse_cb = nested_exts[u].def->plugin->parse;
231 r = ext_parse_cb(&in_ext, LYD_JSON, &nested_exts[u], parent, lydctx->parse_opts | LYD_PARSE_ONLY);
232 if (!r) {
233 /* data successfully parsed, remember for validation */
234 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
235 ext_val = malloc(sizeof *ext_val);
236 LY_CHECK_ERR_RET(!ext_val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
237 ext_val->ext = &nested_exts[u];
238 ext_val->sibling = lyd_child_no_keys(parent);
239 LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200240 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100241
242 /* adjust the jsonctx accordingly */
243 *lydctx->jsonctx->in = in_ext;
244 LYJSON_STATUS_PUSH_RET(lydctx->jsonctx, LYJSON_OBJECT_CLOSED);
245 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
246 return LY_SUCCESS;
247 } else if (r != LY_ENOT) {
248 /* fatal error */
249 return r;
Radek Krejci1798aae2020-07-14 13:26:06 +0200250 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100251 /* data was not from this module, continue */
Radek Krejci1798aae2020-07-14 13:26:06 +0200252 }
253
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100254 /* no extensions or none matched, restore input */
255 *lydctx->jsonctx->in = in_bck;
256 return LY_ENOT;
Radek Krejci1798aae2020-07-14 13:26:06 +0200257}
258
259/**
260 * @brief Skip the currently open JSON object/array
261 * @param[in] jsonctx JSON context with the input data to skip.
262 * @return LY_ERR value.
263 */
264static LY_ERR
265lydjson_data_skip(struct lyjson_ctx *jsonctx)
266{
267 enum LYJSON_PARSER_STATUS status, current;
268 size_t sublevels = 1;
269
270 status = lyjson_ctx_status(jsonctx, 0);
271
272 /* skip after the content */
273 do {
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200274 uint32_t prev_depth = jsonctx->depth;
275
Radek Krejci1798aae2020-07-14 13:26:06 +0200276 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200277
Radek Krejci1798aae2020-07-14 13:26:06 +0200278 if (current == status) {
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200279 /* lyjson_ctx_next() can return LYSJON_OBJECT in two cases, either when
280 * a new object is encountered, or when it finishes parsing a value from a
281 * previous key-value pair. In the latter case the sublevel shouldn't increase.
282 */
283 if ((status == LYJSON_OBJECT) && (prev_depth == jsonctx->depth)) {
284 continue;
285 }
286
Radek Krejci1798aae2020-07-14 13:26:06 +0200287 sublevels++;
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200288 } else if ((status == LYJSON_OBJECT) && (current == LYJSON_OBJECT_CLOSED)) {
Juraj Vijtiuk46eb15e2021-09-06 15:38:36 +0200289 sublevels--;
Radek Krejci1798aae2020-07-14 13:26:06 +0200290 }
Michal Vaskoef964672022-01-03 11:13:25 +0100291 } while ((current != status + 1) || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200292
293 return LY_SUCCESS;
294}
295
296/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297 * @brief Get schema node corresponding to the input parameters.
298 *
299 * @param[in] lydctx JSON data parser context.
300 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
301 * @param[in] prefix Requested node's prefix (module name).
302 * @param[in] prefix_len Length of the @p prefix.
303 * @param[in] name Requested node's name.
304 * @param[in] name_len Length of the @p name.
305 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
306 * @param[out] snode_p Found schema node corresponding to the input parameters. If NULL, parse as an opaque node.
307 * @return LY_SUCCES on success.
308 * @return LY_ENOT if the whole object was parsed (skipped or as an extension).
309 * @return LY_ERR on error.
310 */
311static LY_ERR
312lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
313 size_t name_len, struct lyd_node *parent, const struct lysc_node **snode_p)
314{
315 LY_ERR ret = LY_SUCCESS, r;
316 struct lys_module *mod = NULL;
317 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
318
319 /* init return value */
320 *snode_p = NULL;
321
322 LOG_LOCSET(NULL, parent, NULL, NULL);
323
324 /* get the element module */
325 if (prefix_len) {
326 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
327 } else if (parent) {
328 if (parent->schema) {
329 mod = parent->schema->module;
330 }
331 } else {
332 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
333 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
334 ret = LY_EVALID;
335 goto cleanup;
336 }
337 if (!mod) {
338 /* check for extension data */
339 r = lydjson_nested_ext(lydctx, parent);
340 if (!r) {
341 /* successfully parsed */
342 ret = LY_ENOT;
343 goto cleanup;
344 } else if (r != LY_ENOT) {
345 /* error */
346 ret = r;
347 goto cleanup;
348 }
349
350 /* unknown module */
351 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
352 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
353 ret = LY_EVALID;
354 goto cleanup;
355 }
356 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
357 /* skip element with children */
358 ret = lydjson_data_skip(lydctx->jsonctx);
359 LY_CHECK_GOTO(ret, cleanup);
360
361 ret = LY_ENOT;
362 goto cleanup;
363 }
364 }
365
366 /* get the schema node */
367 if (mod && (!parent || parent->schema)) {
368 if (!parent && lydctx->ext) {
369 *snode_p = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
370 } else {
371 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
372 }
373 if (!*snode_p) {
374 /* check for extension data */
375 r = lydjson_nested_ext(lydctx, parent);
376 if (!r) {
377 /* successfully parsed */
378 ret = LY_ENOT;
379 goto cleanup;
380 } else if (r != LY_ENOT) {
381 /* error */
382 ret = r;
383 goto cleanup;
384 }
385
386 /* unknown data node */
387 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
388 if (parent) {
389 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
390 (int)name_len, name, LYD_NAME(parent));
391 } else if (lydctx->ext) {
392 if (lydctx->ext->argument) {
393 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
394 "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
395 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
396 } else {
397 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
398 (int)name_len, name, lydctx->ext->def->name);
399 }
400 } else {
401 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
402 (int)name_len, name, mod->name);
403 }
404 ret = LY_EVALID;
405 goto cleanup;
406 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
407 /* skip element with children */
408 ret = lydjson_data_skip(lydctx->jsonctx);
409 LY_CHECK_GOTO(ret, cleanup);
410
411 ret = LY_ENOT;
412 goto cleanup;
413 }
414 } else {
415 /* check that schema node is valid and can be used */
416 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p);
417 }
418 }
419
420cleanup:
421 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
422 return ret;
423}
424
425/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200426 * @brief Check that the input data are parseable as the @p list.
427 *
428 * Checks for all the list's keys. Function does not revert the context state.
429 *
430 * @param[in] jsonctx JSON parser context.
431 * @param[in] list List schema node corresponding to the input data object.
432 * @return LY_SUCCESS in case the data are ok for the @p list
433 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
434 */
435static LY_ERR
436lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
437{
438 LY_ERR ret = LY_SUCCESS;
439 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
440 struct ly_set key_set = {0};
441 const struct lysc_node *snode;
442 uint32_t i, status_count;
443
444 assert(list && (list->nodetype == LYS_LIST));
445 assert(status == LYJSON_OBJECT);
446
447 /* get all keys into a set (keys do not have if-features or anything) */
448 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100449 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200450 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200451 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200452 }
453
454 if (status != LYJSON_OBJECT_EMPTY) {
455 status_count = jsonctx->status.count;
456
457 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
458 const char *name, *prefix;
459 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200460 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200461
462 /* match the key */
463 snode = NULL;
464 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
465
466 if (!is_attr && !prefix) {
467 for (i = 0; i < key_set.count; ++i) {
468 snode = (const struct lysc_node *)key_set.objs[i];
469 if (!ly_strncmp(snode->name, name, name_len)) {
470 break;
471 }
472 }
473 /* go into the item to a) process it as a key or b) start skipping it as another list child */
474 ret = lyjson_ctx_next(jsonctx, &status);
475 LY_CHECK_GOTO(ret, cleanup);
476
477 if (snode) {
478 /* we have the key, validate the value */
479 if (status < LYJSON_NUMBER) {
480 /* not a terminal */
481 ret = LY_ENOT;
482 goto cleanup;
483 }
484
Radek Krejci8df109d2021-04-23 12:19:08 +0200485 ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200486 LY_CHECK_GOTO(ret, cleanup);
487
488 /* key with a valid value, remove from the set */
489 ly_set_rm_index(&key_set, i, NULL);
490 }
491 } else {
492 /* start skipping the member we are not interested in */
493 ret = lyjson_ctx_next(jsonctx, &status);
494 LY_CHECK_GOTO(ret, cleanup);
495 }
496 /* move to the next child */
497 while (status_count < jsonctx->status.count) {
498 ret = lyjson_ctx_next(jsonctx, &status);
499 LY_CHECK_GOTO(ret, cleanup);
500 }
501 }
502 }
503
504 if (key_set.count) {
505 /* some keys are missing/did not validate */
506 ret = LY_ENOT;
507 }
508
509cleanup:
510 ly_set_erase(&key_set, NULL);
511 return ret;
512}
513
514/**
515 * @brief Get the hint for the data type parsers according to the current JSON parser context.
516 *
517 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
518 * @param[in,out] status Pointer to the current context status,
519 * in some circumstances the function manipulates with the context so the status is updated.
520 * @param[out] type_hint_p Pointer to the variable to store the result.
521 * @return LY_SUCCESS in case of success.
522 * @return LY_EINVAL in case of invalid context status not referring to a value.
523 */
524static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200525lydjson_value_type_hint(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status_p, uint32_t *type_hint_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200526{
527 *type_hint_p = 0;
528
529 if (*status_p == LYJSON_ARRAY) {
530 /* only [null] */
531 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
532 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
533
534 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
535 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
536
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200537 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200538 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200539 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200540 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200541 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200542 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200543 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200544 } else if (*status_p == LYJSON_NULL) {
545 *type_hint_p = 0;
546 } else {
547 return LY_EINVAL;
548 }
549
550 return LY_SUCCESS;
551}
552
553/**
554 * @brief Check in advance if the input data are parsable according to the provided @p snode.
555 *
556 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
557 * is naturally done when the data are really parsed.
558 *
559 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
560 * as before calling, despite it is necessary to process input data for checking.
561 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
562 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
563 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
564 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
565 * @return LY_EINVAL in case of invalid leaf JSON encoding
566 * and they are expected to be parsed as opaq nodes.
567 */
568static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200569lydjson_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 +0200570{
571 LY_ERR ret = LY_SUCCESS;
572 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
573 enum LYJSON_PARSER_STATUS status;
574
575 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200576
Michal Vasko32ac9942020-08-12 14:35:12 +0200577 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
578 /* can always be parsed as a data node if we have the schema node */
579 return LY_SUCCESS;
580 }
581
Michal Vasko85430702021-07-21 14:29:56 +0200582 /* backup parser */
583 lyjson_ctx_backup(jsonctx);
584 status = lyjson_ctx_status(jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200585
Michal Vasko85430702021-07-21 14:29:56 +0200586 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200587 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
588 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200589 switch (snode->nodetype) {
590 case LYS_LEAFLIST:
591 case LYS_LEAF:
592 /* value may not be valid in which case we parse it as an opaque node */
593 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
594 if (ret) {
595 break;
596 }
597
Radek Krejci8df109d2021-04-23 12:19:08 +0200598 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200599 ret = LY_ENOT;
600 }
601 break;
602 case LYS_LIST:
603 /* lists may not have all its keys */
604 if (lydjson_check_list(jsonctx, snode)) {
605 /* invalid list, parse as opaque if it missing/has invalid some keys */
606 ret = LY_ENOT;
607 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200608 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200609 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200610 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200611 status = lyjson_ctx_status(jsonctx, 0);
612 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
613 }
614
Michal Vasko85430702021-07-21 14:29:56 +0200615 /* restore parser */
616 lyjson_ctx_restore(jsonctx);
617
Radek Krejci1798aae2020-07-14 13:26:06 +0200618 return ret;
619}
620
621/**
622 * @brief Join the forward-referencing metadata with their target data nodes.
623 *
624 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
625 *
626 * @param[in] lydctx JSON data parser context.
627 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
628 * as a starting point to search for the metadata's target data node
629 * @return LY_SUCCESS on success
630 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
631 */
632static LY_ERR
633lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
634{
635 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200636 struct lyd_node *node, *attr, *next, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200637 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200638 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100639 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200640
641 /* finish linking metadata */
642 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200643 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200644 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200645 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200646 const char *name, *prefix;
647 size_t name_len, prefix_len;
648 const struct lysc_node *snode;
649
Michal Vaskoad92b672020-11-12 13:11:31 +0100650 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200651 /* not an opaq metadata node */
652 continue;
653 }
654
Radek Krejciddace2c2021-01-08 11:30:56 +0100655 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100656 log_location_items++;
657
Michal Vaskoad92b672020-11-12 13:11:31 +0100658 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200659 /* metas' names are stored in dictionary, so checking pointers must works */
660 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100661 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200662 instance = 1;
663 } else {
664 instance++;
665 }
666
aPiecek5057c2e2021-05-17 10:28:03 +0200667 /* find the corresponding data node */
668 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200669 if (!node->schema) {
670 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100671 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200672 continue;
673 }
674
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200675 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100676 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
677 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200678 ret = LY_EVALID;
679 goto cleanup;
680 }
681
682 /* match */
683 match++;
684 if (match != instance) {
685 continue;
686 }
687
688 LY_LIST_FOR(meta_container->child, meta_iter) {
689 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200690 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100691
Michal Vaskoad92b672020-11-12 13:11:31 +0100692 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
693 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200694 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100695 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200696 LY_CHECK_GOTO(ret, cleanup);
697 }
698
699 /* done */
700 break;
701 } else {
702 /* this is the second time we are resolving the schema node, so it must succeed,
703 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100704 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
705 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200706 assert(is_attr);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100707 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, lyd_parent(*first_p), &snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200708 assert(ret == LY_SUCCESS);
709
710 if (snode != node->schema) {
711 continue;
712 }
713
714 /* match */
715 match++;
716 if (match != instance) {
717 continue;
718 }
719
720 LY_LIST_FOR(meta_container->child, meta_iter) {
721 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200722 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200723 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200724
Michal Vaskoad92b672020-11-12 13:11:31 +0100725 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200726 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200727 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100728 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100729 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200730 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100731 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200732 if (meta->name.prefix) {
733 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
734 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
735 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
736 meta->name.name);
737 } else {
738 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
739 meta->name.name);
740 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200741 ret = LY_EVALID;
742 goto cleanup;
743 }
744 }
745 /* add/correct flags */
Michal Vaskoddd76592022-01-17 13:34:48 +0100746 lyd_parse_set_data_flags(node, &lydctx->node_when, &node->meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200747
748 /* done */
749 break;
750 }
751 }
752
753 if (match != instance) {
754 /* there is no corresponding data node for the metadata */
755 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100756 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
757 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200758 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200759 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100760 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
761 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200762 }
763 ret = LY_EVALID;
764 } else {
765 /* remove the opaq attr */
766 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200767 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200768 }
769 lyd_free_tree(attr);
770 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100771
Radek Krejciddace2c2021-01-08 11:30:56 +0100772 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100773 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200774 }
775
776cleanup:
777 lydict_remove(lydctx->jsonctx->ctx, prev);
778
Radek Krejciddace2c2021-01-08 11:30:56 +0100779 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200780 return ret;
781}
782
783/**
784 * @brief Parse a metadata member.
785 *
786 * @param[in] lydctx JSON data parser context.
787 * @param[in] snode Schema node of the metadata parent.
788 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
789 * so the data node does not exists. In such a case the metadata is stored in the context for the later
790 * processing by lydjson_metadata_finish().
791 * @return LY_SUCCESS on success
792 * @return Various LY_ERR values in case of failure.
793 */
794static LY_ERR
795lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
796{
797 LY_ERR ret = LY_SUCCESS;
798 enum LYJSON_PARSER_STATUS status;
799 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200800 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200801 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200802 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200803 size_t name_len, prefix_len = 0;
804 struct lys_module *mod;
805 struct lyd_meta *meta = NULL;
806 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200807 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200808 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200809 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200810 uint16_t nodetype;
811
812 assert(snode || node);
813
814 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
815
816 /* move to the second item in the name/X pair */
817 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
818 LY_CHECK_GOTO(ret, cleanup);
819
820 /* check attribute encoding */
821 switch (nodetype) {
822 case LYS_LEAFLIST:
823 expected = "@name/array of objects/nulls";
824
825 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
826
827next_entry:
828 instance++;
829
830 /* move into array / next entry */
831 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
832 LY_CHECK_GOTO(ret, cleanup);
833
834 if (status == LYJSON_ARRAY_CLOSED) {
835 /* we are done, move after the array */
836 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
837 goto cleanup;
838 }
839 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
840
Michal Vasko69730152020-10-09 16:30:07 +0200841 if (!node || (node->schema != prev->schema)) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200842 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 +0200843 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200844 ret = LY_EVALID;
845 goto cleanup;
846 }
847
848 if (status == LYJSON_NULL) {
849 /* continue with the next entry in the leaf-list array */
850 prev = node;
851 node = node->next;
852 goto next_entry;
853 }
854 break;
855 case LYS_LEAF:
856 case LYS_ANYXML:
857 expected = "@name/object";
858
aPiecek5b115fc2021-05-24 14:32:42 +0200859 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200860 break;
861 case LYS_CONTAINER:
862 case LYS_LIST:
863 case LYS_ANYDATA:
864 case LYS_NOTIF:
865 case LYS_ACTION:
866 case LYS_RPC:
867 in_parent = 1;
868 expected = "@/object";
869 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
870 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200871 default:
872 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200873 }
874
875 /* process all the members inside a single metadata object */
876 assert(status == LYJSON_OBJECT);
877
Radek Krejciddace2c2021-01-08 11:30:56 +0100878 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100879
Radek Krejci1798aae2020-07-14 13:26:06 +0200880 while (status != LYJSON_OBJECT_CLOSED) {
aPiecekd2c39372021-06-16 14:03:12 +0200881 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 +0100882 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200883
Michal Vasko69d0ca12021-07-23 10:22:03 +0200884 if (!name_len) {
885 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
886 ret = LY_EVALID;
887 goto cleanup;
888 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100889 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100890 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200891 ret = LY_EVALID;
892 goto cleanup;
893 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100894 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100895 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200896 ret = LY_EVALID;
897 goto cleanup;
898 }
899
900 /* get the element module */
901 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
902 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100903 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100904 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 +0100905 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200906 ret = LY_EVALID;
907 goto cleanup;
908 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100909 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200910 /* skip element with children */
911 ret = lydjson_data_skip(lydctx->jsonctx);
912 LY_CHECK_GOTO(ret, cleanup);
913 status = lyjson_ctx_status(lydctx->jsonctx, 0);
914 /* end of the item */
915 continue;
916 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100917 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200918 }
919
920 /* get the value */
921 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
922 LY_CHECK_GOTO(ret, cleanup);
923
924 if (node->schema) {
925 /* create metadata */
926 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200927 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100928 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200929 LY_CHECK_GOTO(ret, cleanup);
930
931 /* add/correct flags */
Michal Vaskoddd76592022-01-17 13:34:48 +0100932 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200933 } else {
934 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200935 const char *module_name;
936 size_t module_name_len;
937
938 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
939
Radek Krejci1798aae2020-07-14 13:26:06 +0200940 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100941 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
942 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200943 LY_VALUE_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200944 LY_CHECK_GOTO(ret, cleanup);
945 }
946 /* next member */
947 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
948 LY_CHECK_GOTO(ret, cleanup);
aPiecekde3c6922021-05-24 14:42:10 +0200949 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200950 }
951
952 if (nodetype == LYS_LEAFLIST) {
953 /* continue by processing another metadata object for the following
954 * leaf-list instance since they are allways instantiated in JSON array */
955 prev = node;
956 node = node->next;
957 goto next_entry;
958 }
959
Radek Krejci5813c362021-01-05 10:51:57 +0100960 /* success */
961 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200962
963representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100964 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200965 "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 +0100966 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
967 in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200968
Radek Krejci2efc45b2020-12-22 16:25:44 +0100969 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100970
971cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200972 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100973 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100974 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200975}
976
977/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200978 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
979 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200980 *
981 * @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 +0200982 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
983 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
984 * @param[in] last If set, always insert at the end.
Radek Krejci1798aae2020-07-14 13:26:06 +0200985 */
986static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100987lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last)
Radek Krejci1798aae2020-07-14 13:26:06 +0200988{
989 if (*node_p) {
990 /* insert, keep first pointer correct */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100991 lyd_insert_node(parent, first_p, *node_p, last);
Radek Krejci1798aae2020-07-14 13:26:06 +0200992 if (first_p) {
993 if (parent) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100994 *first_p = lyd_child(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200995 } else {
996 while ((*first_p)->prev->next) {
997 *first_p = (*first_p)->prev;
998 }
999 }
1000 }
1001 *node_p = NULL;
1002 }
1003}
1004
aPiecek4bf47212021-05-27 10:55:47 +02001005/**
1006 * @brief Wrapper for ::lyd_create_opaq().
1007 *
1008 * @param[in] lydctx JSON data parser context.
1009 * @param[in] name Name of the opaq node to create.
1010 * @param[in] name_len Length of the @p name string.
1011 * @param[in] prefix Prefix of the opaq node to create.
1012 * @param[in] prefix_len Length of the @p prefx string.
1013 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1014 * but must be set if @p first is not.
1015 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1016 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1017 * @param[out] node_p Pointer to the created opaq node.
1018 * @return LY_ERR value.
1019 */
1020static LY_ERR
1021lydjson_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 +01001022 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +02001023{
1024 LY_ERR ret = LY_SUCCESS;
1025 const char *value = NULL, *module_name;
1026 size_t value_len = 0, module_name_len = 0;
1027 ly_bool dynamic = 0;
1028 uint32_t type_hint = 0;
1029
1030 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
1031 /* prepare for creating opaq node with a value */
1032 value = lydctx->jsonctx->value;
1033 value_len = lydctx->jsonctx->value_len;
1034 dynamic = lydctx->jsonctx->dynamic;
1035 lydctx->jsonctx->dynamic = 0;
1036
1037 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
1038 }
1039
1040 /* create node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001041 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +02001042 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
1043 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
1044 if (dynamic) {
1045 free((char *)value);
1046 }
1047
1048 return ret;
1049}
1050
Michal Vaskoe0665742021-02-11 11:08:44 +01001051static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
1052 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +02001053
1054/**
1055 * @brief Parse opaq node from the input.
1056 *
1057 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
1058 *
1059 * @param[in] lydctx JSON data parser context.
1060 * @param[in] name Name of the opaq node to create.
1061 * @param[in] name_len Length of the @p name string.
1062 * @param[in] prefix Prefix of the opaq node to create.
1063 * @param[in] prefix_len Length of the @p prefx string.
1064 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1065 * but must be set if @p first is not.
1066 * @param[in,out] status_p Pointer to the current status of the parser context,
1067 * since the function manipulates with the context and process the input, the status can be updated.
1068 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1069 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1070 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1071 * @param[out] node_p Pointer to the created opaq node.
1072 * @return LY_ERR value.
1073 */
1074static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001075lydjson_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 +01001076 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1077 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001078{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001079 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 +02001080
Michal Vasko1a85d332021-08-27 10:35:28 +02001081 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1082 /* special array null value */
1083 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1084
1085 /* must be the only item */
1086 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
1087 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1088 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
1089 return LY_EVALID;
1090 }
1091
1092 goto finish;
1093 }
1094
aPiecekdbb8baf2021-05-27 11:01:51 +02001095 while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001096 /* process another instance of the same node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001097
Michal Vasko69730152020-10-09 16:30:07 +02001098 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Michal Vasko1a85d332021-08-27 10:35:28 +02001099 /* array with objects, list */
1100 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
1101
Radek Krejci1798aae2020-07-14 13:26:06 +02001102 /* but first process children of the object in the array */
Michal Vasko1a85d332021-08-27 10:35:28 +02001103 while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001104 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +02001105 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
1106 }
Michal Vasko1a85d332021-08-27 10:35:28 +02001107 } else {
1108 /* array with values, leaf-list */
1109 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001110 }
1111
1112 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001113 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1114 goto finish;
1115 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001116
1117 /* continue with the next instance */
aPiecekdbb8baf2021-05-27 11:01:51 +02001118 assert(node_p);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001119 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001120 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 +02001121 }
1122
1123 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
1124 /* process children */
1125 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
1126 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
1127 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001128 }
1129 }
1130
aPiecekdbb8baf2021-05-27 11:01:51 +02001131finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001132 /* finish linking metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001133 return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001134}
1135
1136/**
aPiecek6cee5d02021-05-12 10:32:00 +02001137 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1138 *
1139 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1140 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1141 * for ::lydjson_parse_opaq().
1142 *
1143 * @param[in] lydctx JSON data parser context.
1144 * @param[in] name Name of the opaq node to create.
1145 * @param[in] name_len Length of the @p name string.
1146 * @param[in] prefix Prefix of the opaq node to create.
1147 * @param[in] prefix_len Length of the @p prefx string.
1148 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1149 * but must be set if @p first is not.
1150 * @param[in,out] status_p Pointer to the current status of the parser context,
1151 * since the function manipulates with the context and process the input, the status can be updated.
1152 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1153 * @param[out] node_p Pointer to the created opaq node.
1154 * @return LY_ERR value.
1155 */
1156static LY_ERR
1157lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001158 const char *prefix, size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001159 struct lyd_node **first_p, struct lyd_node **node_p)
1160{
1161 enum LYJSON_PARSER_STATUS status_inner = 0;
1162
1163 /* move to the second item in the name/X pair */
1164 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1165
1166 if (*status_p == LYJSON_ARRAY) {
1167 /* move into the array */
1168 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1169 } else {
1170 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1171 status_inner = LYJSON_ERROR;
1172 }
1173
1174 if (status_inner == LYJSON_ERROR) {
1175 status_inner = *status_p;
1176 }
1177
1178 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001179 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1180 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001181
1182 return LY_SUCCESS;
1183}
1184
1185/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001186 * @brief Process the attribute container (starting by @)
1187 *
1188 * @param[in] lydctx JSON data parser context.
1189 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1190 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1191 * @param[in] name Name of the opaq node to create.
1192 * @param[in] name_len Length of the @p name string.
1193 * @param[in] prefix Prefix of the opaq node to create.
1194 * @param[in] prefix_len Length of the @p prefx string.
1195 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1196 * but must be set if @p first is not.
1197 * @param[in,out] status_p Pointer to the current status of the parser context,
1198 * since the function manipulates with the context and process the input, the status can be updated.
1199 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1200 * @param[out] node_p Pointer to the created opaq node.
1201 * @return LY_ERR value.
1202 */
1203static LY_ERR
1204lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001205 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1206 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001207{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001208 LY_ERR r;
1209 const char *opaq_name;
1210 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001211
1212 /* parse as an attribute to a node */
1213 if (!attr_node && snode) {
1214 /* try to find the instance */
1215 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1216 if (iter->schema == snode) {
1217 attr_node = iter;
1218 break;
1219 }
1220 }
1221 }
1222 if (!attr_node) {
1223 /* parse just as an opaq node with the name beginning with @,
1224 * later we have to check that it belongs to a standard node
1225 * and it is supposed to be converted to a metadata */
1226 uint32_t prev_opts;
1227
Radek Krejci1798aae2020-07-14 13:26:06 +02001228 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001229 prev_opts = lydctx->parse_opts;
1230 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1231 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001232
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001233 opaq_name = prefix ? prefix - 1 : name - 1;
1234 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1235 r = lydjson_ctx_next_parse_opaq(lydctx, opaq_name, opaq_name_len, NULL, 0, parent, status_p, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001236
1237 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001238 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001239 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001240 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001241 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001242 }
1243
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001244 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001245}
1246
1247/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001248 * @brief Parse a single instance of a node.
1249 *
1250 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1251 * as before calling, despite it is necessary to process input data for checking.
1252 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1253 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1254 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1255 * @param[in] name Parsed JSON node name.
1256 * @param[in] name_len Lenght of @p name.
1257 * @param[in] prefix Parsed JSON node prefix.
1258 * @param[in] prefix_len Length of @p prefix.
1259 * @param[in,out] status JSON parser status, is updated.
1260 * @param[out] node Parsed data (or opaque) node.
1261 * @return LY_SUCCESS if a node was successfully parsed,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001262 * @return LY_ENOT in case of invalid JSON encoding,
Michal Vasko32ac9942020-08-12 14:35:12 +02001263 * @return LY_ERR on other errors.
1264 */
1265static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001266lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001267 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1268 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001269{
1270 LY_ERR ret;
Michal Vaskobbdadda2022-01-06 11:40:10 +01001271 uint32_t type_hints = 0, prev_parse_opts, prev_int_opts;
1272 char *val;
Michal Vasko32ac9942020-08-12 14:35:12 +02001273 struct lyd_node *tree = NULL;
1274
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001275 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001276 if (ret == LY_SUCCESS) {
1277 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1278 if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskobbdadda2022-01-06 11:40:10 +01001279 LY_CHECK_RET((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001280 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
Michal Vaskobbdadda2022-01-06 11:40:10 +01001281
Michal Vasko32ac9942020-08-12 14:35:12 +02001282 /* create terminal node */
1283 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001284 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001285 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001286 LY_CHECK_RET(ret);
1287
1288 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001289 if (*status == LYJSON_ARRAY) {
1290 /* only [null], 2 more moves are needed */
1291 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1292 assert(*status == LYJSON_NULL);
1293 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1294 assert(*status == LYJSON_ARRAY_CLOSED);
1295 }
Michal Vaskobbdadda2022-01-06 11:40:10 +01001296 } else if (snode->nodetype == LYS_ANYXML) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001297 LY_CHECK_RET((*status != LYJSON_STRING) && (*status != LYJSON_NULL), LY_ENOT);
Michal Vaskobbdadda2022-01-06 11:40:10 +01001298
1299 /* create anyxml node */
1300 if (*status == LYJSON_NULL) {
1301 ret = lyd_create_any(snode, NULL, LYD_ANYDATA_STRING, 1, node);
1302 LY_CHECK_RET(ret);
1303 } else if (lydctx->jsonctx->dynamic) {
1304 ret = lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node);
1305 LY_CHECK_RET(ret);
1306 lydctx->jsonctx->dynamic = 0;
1307 } else {
1308 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1309 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1310 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
1311 if (ret) {
1312 free(val);
1313 return ret;
1314 }
1315 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001316 } else if (snode->nodetype & LYD_NODE_INNER) {
1317 /* create inner node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001318 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko32ac9942020-08-12 14:35:12 +02001319
1320 ret = lyd_create_inner(snode, node);
1321 LY_CHECK_RET(ret);
1322
Radek Krejciddace2c2021-01-08 11:30:56 +01001323 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001324
Michal Vasko32ac9942020-08-12 14:35:12 +02001325 /* process children */
Michal Vaskobbdadda2022-01-06 11:40:10 +01001326 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001327 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001328 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001329 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1330 }
1331
1332 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001333 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001334 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001335
1336 if (snode->nodetype == LYS_LIST) {
1337 /* check all keys exist */
1338 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001339 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001340 }
1341
Michal Vaskoe0665742021-02-11 11:08:44 +01001342 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001343 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001344 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001345 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001346
1347 /* add any missing default children */
Michal Vaskoddd76592022-01-17 13:34:48 +01001348 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001349 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001350 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001351 }
1352
Radek Krejciddace2c2021-01-08 11:30:56 +01001353 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskobbdadda2022-01-06 11:40:10 +01001354 } else if (snode->nodetype == LYS_ANYDATA) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001355 /* create any node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001356 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko32ac9942020-08-12 14:35:12 +02001357
1358 /* parse any data tree with correct options */
1359 /* first backup the current options and then make the parser to process data as opaq nodes */
Michal Vasko02ed9d82021-07-15 14:58:04 +02001360 prev_parse_opts = lydctx->parse_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001361 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1362 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001363 prev_int_opts = lydctx->int_opts;
1364 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vasko32ac9942020-08-12 14:35:12 +02001365
1366 /* process the anydata content */
Michal Vaskobbdadda2022-01-06 11:40:10 +01001367 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001368 ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001369 LY_CHECK_RET(ret);
1370 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1371 }
1372
1373 /* restore parser options */
Michal Vasko02ed9d82021-07-15 14:58:04 +02001374 lydctx->parse_opts = prev_parse_opts;
1375 lydctx->int_opts = prev_int_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001376
1377 /* finish linking metadata */
1378 ret = lydjson_metadata_finish(lydctx, &tree);
1379 LY_CHECK_RET(ret);
1380
Michal Vasko366a4a12020-12-04 16:23:57 +01001381 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001382 LY_CHECK_RET(ret);
1383 }
Michal Vaskocaf608d2021-07-23 13:51:23 +02001384
1385 /* add/correct flags */
Michal Vaskoddd76592022-01-17 13:34:48 +01001386 lyd_parse_set_data_flags(*node, &lydctx->node_when, &(*node)->meta, lydctx->parse_opts);
Michal Vasko32ac9942020-08-12 14:35:12 +02001387 } else if (ret == LY_ENOT) {
1388 /* parse it again as an opaq node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001389 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001390 LY_CHECK_RET(ret);
1391
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001392 if (snode->nodetype == LYS_LIST) {
1393 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1394 } else if (snode->nodetype == LYS_LEAFLIST) {
1395 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001396 }
1397 }
1398
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001399 return LY_SUCCESS;
Michal Vasko32ac9942020-08-12 14:35:12 +02001400}
1401
1402/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001403 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001404 *
1405 * @param[in] lydctx JSON data parser context.
1406 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1407 * @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 +01001408 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001409 * @return LY_ERR value.
1410 */
1411static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001412lydjson_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 +02001413{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001414 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci1798aae2020-07-14 13:26:06 +02001415 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
aPiecek49e2a3a2021-05-13 10:36:46 +02001416 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001417 size_t name_len, prefix_len = 0;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001418 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001419 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001420 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001421 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001422 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001423
1424 assert(parent || first_p);
1425 assert(status == LYJSON_OBJECT);
1426
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001427 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1428 /* all descendants should be parsed */
1429 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1430
Radek Krejci1798aae2020-07-14 13:26:06 +02001431 /* process the node name */
aPiecekd2c39372021-06-16 14:03:12 +02001432 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 +01001433 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001434
Michal Vaskocabe0702020-08-12 10:14:36 +02001435 if (!is_meta || name_len || prefix_len) {
1436 /* get the schema node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001437 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1438 if (r == LY_ENOT) {
1439 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001440 goto cleanup;
1441 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001442 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001443
Michal Vaskocabe0702020-08-12 10:14:36 +02001444 if (!snode) {
1445 /* we will not be parsing it as metadata */
1446 is_meta = 0;
1447 }
1448 }
1449
1450 if (is_meta) {
1451 /* parse as metadata */
1452 if (!name_len && !prefix_len) {
1453 /* parent's metadata without a name - use the schema from the parent */
1454 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001455 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001456 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001457 ret = LY_EVALID;
1458 goto cleanup;
1459 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001460 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001461 snode = attr_node->schema;
1462 }
1463 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001464 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001465 LY_CHECK_GOTO(ret, cleanup);
1466 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001467 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001468 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001469
aPiecekb7b29e62021-05-11 10:02:43 +02001470 /* opaq node cannot have an empty string as the name. */
1471 if (name_len == 0) {
1472 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string.");
1473 ret = LY_EVALID;
1474 goto cleanup;
1475 }
1476
aPiecek6cee5d02021-05-12 10:32:00 +02001477 /* move to the second item in the name/X pair and parse opaq */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001478 ret = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001479 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001480 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001481 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001482
1483 /* move to the second item in the name/X pair */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001484 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001485
1486 /* first check the expected representation according to the nodetype and then continue with the content */
1487 switch (snode->nodetype) {
1488 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001489 case LYS_LIST:
1490 if (snode->nodetype == LYS_LEAFLIST) {
1491 expected = "name/array of values";
1492 } else {
1493 expected = "name/array of objects";
1494 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001495
1496 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1497
1498 /* move into array */
1499 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1500 LY_CHECK_GOTO(ret, cleanup);
1501
Michal Vasko32ac9942020-08-12 14:35:12 +02001502 /* process all the values/objects */
1503 do {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001504 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001505
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001506 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
1507 &status, &node);
1508 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001509 goto representation_error;
1510 } else if (ret) {
1511 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001512 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001513
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001514 /* move after the item(s) */
1515 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1516 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001517
1518 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001519 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001520 case LYS_CONTAINER:
1521 case LYS_NOTIF:
1522 case LYS_ACTION:
1523 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001524 case LYS_ANYDATA:
1525 case LYS_ANYXML:
Michal Vaskobbdadda2022-01-06 11:40:10 +01001526 if (snode->nodetype & (LYS_LEAF | LYS_ANYXML)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001527 if (status == LYJSON_ARRAY) {
1528 expected = "name/[null]";
1529 } else {
1530 expected = "name/value";
1531 }
1532 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001533 expected = "name/object";
1534 }
1535
Michal Vasko32ac9942020-08-12 14:35:12 +02001536 /* process the value/object */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001537 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1538 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001539 goto representation_error;
1540 } else if (ret) {
1541 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001542 }
1543
Michal Vasko32ac9942020-08-12 14:35:12 +02001544 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001545 /* rememeber the RPC/action/notification */
1546 lydctx->op_node = node;
1547 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001548 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001549 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001550 }
1551
Michal Vasko32ac9942020-08-12 14:35:12 +02001552 /* finally connect the parsed node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001553 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001554
1555 /* rememeber a successfully parsed node */
1556 if (parsed) {
1557 ly_set_add(parsed, node, 1, NULL);
1558 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001559
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001560 if (!parse_subtree) {
1561 /* move after the item(s) */
1562 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1563 }
1564
Radek Krejci5813c362021-01-05 10:51:57 +01001565 /* success */
1566 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001567
1568representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001569 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001570 LOGVAL(ctx, LYVE_SYNTAX_JSON, "The %s \"%s\" is expected to be represented as JSON %s, but input data contains name/%s.",
Michal Vasko69730152020-10-09 16:30:07 +02001571 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001572 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001573 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001574
1575cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001576 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001577 lyd_free_tree(node);
1578 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001579}
1580
1581/**
1582 * @brief Common start of JSON parser processing different types of the input data.
1583 *
1584 * @param[in] ctx libyang context
1585 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001586 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1587 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001588 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001589 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001590 * @return LY_ERR value.
1591 */
1592static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001593lyd_parse_json_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts,
Radek Krejcicd5f6222020-11-12 08:54:13 +01001594 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001595{
1596 LY_ERR ret = LY_SUCCESS;
1597 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001598 size_t i;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001599 ly_bool subtree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001600
Radek Krejcicd5f6222020-11-12 08:54:13 +01001601 assert(lydctx_p);
1602 assert(status);
1603
Radek Krejci1798aae2020-07-14 13:26:06 +02001604 /* init context */
1605 lydctx = calloc(1, sizeof *lydctx);
1606 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001607 lydctx->parse_opts = parse_opts;
1608 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001609 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001610
Radek Krejci284f31f2020-09-18 15:40:29 +02001611 /* starting top-level */
1612 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001613 if (in->current[i] == '\n') {
1614 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001615 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001616 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001617 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001618
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001619 subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0;
1620 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001621 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001622
Radek Krejcibb27df32020-11-13 15:39:16 +01001623 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001624 *lydctx_p = lydctx;
1625 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001626 } else {
1627 /* expecting top-level object */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001628 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001629 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001630 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001631 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001632 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001633}
1634
1635LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001636lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1637 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoddd76592022-01-17 13:34:48 +01001638 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001639{
Michal Vaskoe0665742021-02-11 11:08:44 +01001640 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001641 struct lyd_json_ctx *lydctx = NULL;
1642 enum LYJSON_PARSER_STATUS status;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001643 uint32_t int_opts = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001644
Michal Vaskoe0665742021-02-11 11:08:44 +01001645 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1646 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001647
Radek Krejcicd5f6222020-11-12 08:54:13 +01001648 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001649
Michal Vaskoe0665742021-02-11 11:08:44 +01001650 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001651 case LYD_TYPE_DATA_YANG:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001652 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
1653 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1654 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001655 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001656 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001657 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1658 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001659 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001660 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1661 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001662 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001663 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1664 break;
1665 default:
1666 LOGINT(ctx);
1667 rc = LY_EINT;
1668 goto cleanup;
1669 }
1670 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001671 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001672
1673 /* find the operation node if it exists already */
1674 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1675
Radek Krejci1798aae2020-07-14 13:26:06 +02001676 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001677 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1678 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1679 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001680
1681 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001682
1683 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1684 break;
1685 }
1686 }
1687
1688 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1689 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1690 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1691 rc = LY_EVALID;
1692 goto cleanup;
1693 }
1694 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1695 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1696 rc = LY_EVALID;
1697 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001698 }
1699
1700 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001701 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1702 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001703
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001704 if (parse_opts & LYD_PARSE_SUBTREE) {
1705 /* check for a sibling object */
1706 assert(subtree_sibling);
1707 if (lydctx->jsonctx->in->current[0] == ',') {
1708 *subtree_sibling = 1;
1709
1710 /* move to the next object */
1711 ly_in_skip(lydctx->jsonctx->in, 1);
1712 } else {
1713 *subtree_sibling = 0;
1714 }
1715 }
1716
Radek Krejci1798aae2020-07-14 13:26:06 +02001717cleanup:
1718 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001719 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001720 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001721
Michal Vaskoe0665742021-02-11 11:08:44 +01001722 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001723 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001724 } else {
1725 *lydctx_p = (struct lyd_ctx *)lydctx;
1726 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001727 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001728}