blob: 49fd941b2f111aa66c5b16c62f212662f634cae1 [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 Vasko61ad1ff2022-02-10 15:48:39 +0100217 } while (quot_count < 2);
218
219 /* check if there are any nested extension instances */
220 if (parent && parent->schema) {
221 nested_exts = parent->schema->exts;
Radek Krejci1798aae2020-07-14 13:26:06 +0200222 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100223 LY_ARRAY_FOR(nested_exts, u) {
224 /* prepare the input and try to parse this extension data */
225 in_ext = in_start;
226 ext_parse_cb = nested_exts[u].def->plugin->parse;
227 r = ext_parse_cb(&in_ext, LYD_JSON, &nested_exts[u], parent, lydctx->parse_opts | LYD_PARSE_ONLY);
228 if (!r) {
229 /* data successfully parsed, remember for validation */
230 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
231 ext_val = malloc(sizeof *ext_val);
232 LY_CHECK_ERR_RET(!ext_val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
233 ext_val->ext = &nested_exts[u];
234 ext_val->sibling = lyd_child_no_keys(parent);
235 LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +0200236 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100237
238 /* adjust the jsonctx accordingly */
239 *lydctx->jsonctx->in = in_ext;
240 LYJSON_STATUS_PUSH_RET(lydctx->jsonctx, LYJSON_OBJECT_CLOSED);
241 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
242 return LY_SUCCESS;
243 } else if (r != LY_ENOT) {
244 /* fatal error */
245 return r;
Radek Krejci1798aae2020-07-14 13:26:06 +0200246 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100247 /* data was not from this module, continue */
Radek Krejci1798aae2020-07-14 13:26:06 +0200248 }
249
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100250 /* no extensions or none matched, restore input */
251 *lydctx->jsonctx->in = in_bck;
252 return LY_ENOT;
Radek Krejci1798aae2020-07-14 13:26:06 +0200253}
254
255/**
256 * @brief Skip the currently open JSON object/array
257 * @param[in] jsonctx JSON context with the input data to skip.
258 * @return LY_ERR value.
259 */
260static LY_ERR
261lydjson_data_skip(struct lyjson_ctx *jsonctx)
262{
263 enum LYJSON_PARSER_STATUS status, current;
264 size_t sublevels = 1;
265
266 status = lyjson_ctx_status(jsonctx, 0);
267
268 /* skip after the content */
269 do {
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200270 uint32_t prev_depth = jsonctx->depth;
271
Radek Krejci1798aae2020-07-14 13:26:06 +0200272 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200273
Radek Krejci1798aae2020-07-14 13:26:06 +0200274 if (current == status) {
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200275 /* lyjson_ctx_next() can return LYSJON_OBJECT in two cases, either when
276 * a new object is encountered, or when it finishes parsing a value from a
277 * previous key-value pair. In the latter case the sublevel shouldn't increase.
278 */
279 if ((status == LYJSON_OBJECT) && (prev_depth == jsonctx->depth)) {
280 continue;
281 }
282
Radek Krejci1798aae2020-07-14 13:26:06 +0200283 sublevels++;
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200284 } else if ((status == LYJSON_OBJECT) && (current == LYJSON_OBJECT_CLOSED)) {
Juraj Vijtiuk46eb15e2021-09-06 15:38:36 +0200285 sublevels--;
Radek Krejci1798aae2020-07-14 13:26:06 +0200286 }
Michal Vaskoef964672022-01-03 11:13:25 +0100287 } while ((current != status + 1) || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200288
289 return LY_SUCCESS;
290}
291
292/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100293 * @brief Get schema node corresponding to the input parameters.
294 *
295 * @param[in] lydctx JSON data parser context.
296 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
297 * @param[in] prefix Requested node's prefix (module name).
298 * @param[in] prefix_len Length of the @p prefix.
299 * @param[in] name Requested node's name.
300 * @param[in] name_len Length of the @p name.
301 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
302 * @param[out] snode_p Found schema node corresponding to the input parameters. If NULL, parse as an opaque node.
303 * @return LY_SUCCES on success.
304 * @return LY_ENOT if the whole object was parsed (skipped or as an extension).
305 * @return LY_ERR on error.
306 */
307static LY_ERR
308lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
309 size_t name_len, struct lyd_node *parent, const struct lysc_node **snode_p)
310{
311 LY_ERR ret = LY_SUCCESS, r;
312 struct lys_module *mod = NULL;
313 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
314
315 /* init return value */
316 *snode_p = NULL;
317
318 LOG_LOCSET(NULL, parent, NULL, NULL);
319
320 /* get the element module */
321 if (prefix_len) {
322 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
323 } else if (parent) {
324 if (parent->schema) {
325 mod = parent->schema->module;
326 }
327 } else {
328 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
329 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
330 ret = LY_EVALID;
331 goto cleanup;
332 }
333 if (!mod) {
334 /* check for extension data */
335 r = lydjson_nested_ext(lydctx, parent);
336 if (!r) {
337 /* successfully parsed */
338 ret = LY_ENOT;
339 goto cleanup;
340 } else if (r != LY_ENOT) {
341 /* error */
342 ret = r;
343 goto cleanup;
344 }
345
346 /* unknown module */
347 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
348 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
349 ret = LY_EVALID;
350 goto cleanup;
351 }
352 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
353 /* skip element with children */
354 ret = lydjson_data_skip(lydctx->jsonctx);
355 LY_CHECK_GOTO(ret, cleanup);
356
357 ret = LY_ENOT;
358 goto cleanup;
359 }
360 }
361
362 /* get the schema node */
363 if (mod && (!parent || parent->schema)) {
364 if (!parent && lydctx->ext) {
365 *snode_p = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
366 } else {
367 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
368 }
369 if (!*snode_p) {
370 /* check for extension data */
371 r = lydjson_nested_ext(lydctx, parent);
372 if (!r) {
373 /* successfully parsed */
374 ret = LY_ENOT;
375 goto cleanup;
376 } else if (r != LY_ENOT) {
377 /* error */
378 ret = r;
379 goto cleanup;
380 }
381
382 /* unknown data node */
383 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
384 if (parent) {
385 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
386 (int)name_len, name, LYD_NAME(parent));
387 } else if (lydctx->ext) {
388 if (lydctx->ext->argument) {
389 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
390 "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
391 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
392 } else {
393 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
394 (int)name_len, name, lydctx->ext->def->name);
395 }
396 } else {
397 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
398 (int)name_len, name, mod->name);
399 }
400 ret = LY_EVALID;
401 goto cleanup;
402 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
403 /* skip element with children */
404 ret = lydjson_data_skip(lydctx->jsonctx);
405 LY_CHECK_GOTO(ret, cleanup);
406
407 ret = LY_ENOT;
408 goto cleanup;
409 }
410 } else {
411 /* check that schema node is valid and can be used */
412 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p);
413 }
414 }
415
416cleanup:
417 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
418 return ret;
419}
420
421/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200422 * @brief Check that the input data are parseable as the @p list.
423 *
424 * Checks for all the list's keys. Function does not revert the context state.
425 *
426 * @param[in] jsonctx JSON parser context.
427 * @param[in] list List schema node corresponding to the input data object.
428 * @return LY_SUCCESS in case the data are ok for the @p list
429 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
430 */
431static LY_ERR
432lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
433{
434 LY_ERR ret = LY_SUCCESS;
435 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
436 struct ly_set key_set = {0};
437 const struct lysc_node *snode;
438 uint32_t i, status_count;
439
440 assert(list && (list->nodetype == LYS_LIST));
441 assert(status == LYJSON_OBJECT);
442
443 /* get all keys into a set (keys do not have if-features or anything) */
444 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100445 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200446 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200447 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200448 }
449
450 if (status != LYJSON_OBJECT_EMPTY) {
451 status_count = jsonctx->status.count;
452
453 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
454 const char *name, *prefix;
455 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200456 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200457
458 /* match the key */
459 snode = NULL;
460 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
461
462 if (!is_attr && !prefix) {
463 for (i = 0; i < key_set.count; ++i) {
464 snode = (const struct lysc_node *)key_set.objs[i];
465 if (!ly_strncmp(snode->name, name, name_len)) {
466 break;
467 }
468 }
469 /* go into the item to a) process it as a key or b) start skipping it as another list child */
470 ret = lyjson_ctx_next(jsonctx, &status);
471 LY_CHECK_GOTO(ret, cleanup);
472
473 if (snode) {
474 /* we have the key, validate the value */
475 if (status < LYJSON_NUMBER) {
476 /* not a terminal */
477 ret = LY_ENOT;
478 goto cleanup;
479 }
480
Radek Krejci8df109d2021-04-23 12:19:08 +0200481 ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200482 LY_CHECK_GOTO(ret, cleanup);
483
484 /* key with a valid value, remove from the set */
485 ly_set_rm_index(&key_set, i, NULL);
486 }
487 } else {
488 /* start skipping the member we are not interested in */
489 ret = lyjson_ctx_next(jsonctx, &status);
490 LY_CHECK_GOTO(ret, cleanup);
491 }
492 /* move to the next child */
493 while (status_count < jsonctx->status.count) {
494 ret = lyjson_ctx_next(jsonctx, &status);
495 LY_CHECK_GOTO(ret, cleanup);
496 }
497 }
498 }
499
500 if (key_set.count) {
501 /* some keys are missing/did not validate */
502 ret = LY_ENOT;
503 }
504
505cleanup:
506 ly_set_erase(&key_set, NULL);
507 return ret;
508}
509
510/**
511 * @brief Get the hint for the data type parsers according to the current JSON parser context.
512 *
513 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
514 * @param[in,out] status Pointer to the current context status,
515 * in some circumstances the function manipulates with the context so the status is updated.
516 * @param[out] type_hint_p Pointer to the variable to store the result.
517 * @return LY_SUCCESS in case of success.
518 * @return LY_EINVAL in case of invalid context status not referring to a value.
519 */
520static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200521lydjson_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 +0200522{
523 *type_hint_p = 0;
524
525 if (*status_p == LYJSON_ARRAY) {
526 /* only [null] */
527 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
528 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
529
530 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
531 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
532
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200533 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200534 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200535 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200536 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200537 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200538 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200539 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200540 } else if (*status_p == LYJSON_NULL) {
541 *type_hint_p = 0;
542 } else {
543 return LY_EINVAL;
544 }
545
546 return LY_SUCCESS;
547}
548
549/**
550 * @brief Check in advance if the input data are parsable according to the provided @p snode.
551 *
552 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
553 * is naturally done when the data are really parsed.
554 *
555 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
556 * as before calling, despite it is necessary to process input data for checking.
557 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
558 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
559 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
560 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
561 * @return LY_EINVAL in case of invalid leaf JSON encoding
562 * and they are expected to be parsed as opaq nodes.
563 */
564static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200565lydjson_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 +0200566{
567 LY_ERR ret = LY_SUCCESS;
568 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
569 enum LYJSON_PARSER_STATUS status;
570
571 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200572
Michal Vasko32ac9942020-08-12 14:35:12 +0200573 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
574 /* can always be parsed as a data node if we have the schema node */
575 return LY_SUCCESS;
576 }
577
Michal Vasko85430702021-07-21 14:29:56 +0200578 /* backup parser */
579 lyjson_ctx_backup(jsonctx);
580 status = lyjson_ctx_status(jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200581
Michal Vasko85430702021-07-21 14:29:56 +0200582 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200583 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
584 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200585 switch (snode->nodetype) {
586 case LYS_LEAFLIST:
587 case LYS_LEAF:
588 /* value may not be valid in which case we parse it as an opaque node */
589 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
590 if (ret) {
591 break;
592 }
593
Radek Krejci8df109d2021-04-23 12:19:08 +0200594 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200595 ret = LY_ENOT;
596 }
597 break;
598 case LYS_LIST:
599 /* lists may not have all its keys */
600 if (lydjson_check_list(jsonctx, snode)) {
601 /* invalid list, parse as opaque if it missing/has invalid some keys */
602 ret = LY_ENOT;
603 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200604 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200605 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200606 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200607 status = lyjson_ctx_status(jsonctx, 0);
608 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
609 }
610
Michal Vasko85430702021-07-21 14:29:56 +0200611 /* restore parser */
612 lyjson_ctx_restore(jsonctx);
613
Radek Krejci1798aae2020-07-14 13:26:06 +0200614 return ret;
615}
616
617/**
618 * @brief Join the forward-referencing metadata with their target data nodes.
619 *
620 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
621 *
622 * @param[in] lydctx JSON data parser context.
623 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
624 * as a starting point to search for the metadata's target data node
625 * @return LY_SUCCESS on success
626 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
627 */
628static LY_ERR
629lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
630{
631 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200632 struct lyd_node *node, *attr, *next, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200633 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200634 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100635 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200636
637 /* finish linking metadata */
638 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200639 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200640 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200641 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200642 const char *name, *prefix;
643 size_t name_len, prefix_len;
644 const struct lysc_node *snode;
645
Michal Vaskoad92b672020-11-12 13:11:31 +0100646 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200647 /* not an opaq metadata node */
648 continue;
649 }
650
Radek Krejciddace2c2021-01-08 11:30:56 +0100651 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100652 log_location_items++;
653
Michal Vaskoad92b672020-11-12 13:11:31 +0100654 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200655 /* metas' names are stored in dictionary, so checking pointers must works */
656 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100657 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200658 instance = 1;
659 } else {
660 instance++;
661 }
662
aPiecek5057c2e2021-05-17 10:28:03 +0200663 /* find the corresponding data node */
664 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200665 if (!node->schema) {
666 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100667 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200668 continue;
669 }
670
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200671 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100672 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
673 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200674 ret = LY_EVALID;
675 goto cleanup;
676 }
677
678 /* match */
679 match++;
680 if (match != instance) {
681 continue;
682 }
683
684 LY_LIST_FOR(meta_container->child, meta_iter) {
685 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200686 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100687
Michal Vaskoad92b672020-11-12 13:11:31 +0100688 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
689 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200690 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100691 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200692 LY_CHECK_GOTO(ret, cleanup);
693 }
694
695 /* done */
696 break;
697 } else {
698 /* this is the second time we are resolving the schema node, so it must succeed,
699 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100700 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
701 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200702 assert(is_attr);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100703 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 +0200704 assert(ret == LY_SUCCESS);
705
706 if (snode != node->schema) {
707 continue;
708 }
709
710 /* match */
711 match++;
712 if (match != instance) {
713 continue;
714 }
715
716 LY_LIST_FOR(meta_container->child, meta_iter) {
717 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200718 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200719 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200720
Michal Vaskoad92b672020-11-12 13:11:31 +0100721 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200722 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200723 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100724 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100725 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200726 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100727 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200728 if (meta->name.prefix) {
729 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
730 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
731 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
732 meta->name.name);
733 } else {
734 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
735 meta->name.name);
736 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200737 ret = LY_EVALID;
738 goto cleanup;
739 }
740 }
741 /* add/correct flags */
Michal Vaskoddd76592022-01-17 13:34:48 +0100742 lyd_parse_set_data_flags(node, &lydctx->node_when, &node->meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200743
744 /* done */
745 break;
746 }
747 }
748
749 if (match != instance) {
750 /* there is no corresponding data node for the metadata */
751 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100752 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
753 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200754 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200755 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100756 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
757 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200758 }
759 ret = LY_EVALID;
760 } else {
761 /* remove the opaq attr */
762 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200763 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200764 }
765 lyd_free_tree(attr);
766 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100767
Radek Krejciddace2c2021-01-08 11:30:56 +0100768 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100769 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200770 }
771
772cleanup:
773 lydict_remove(lydctx->jsonctx->ctx, prev);
774
Radek Krejciddace2c2021-01-08 11:30:56 +0100775 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200776 return ret;
777}
778
779/**
780 * @brief Parse a metadata member.
781 *
782 * @param[in] lydctx JSON data parser context.
783 * @param[in] snode Schema node of the metadata parent.
784 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
785 * so the data node does not exists. In such a case the metadata is stored in the context for the later
786 * processing by lydjson_metadata_finish().
787 * @return LY_SUCCESS on success
788 * @return Various LY_ERR values in case of failure.
789 */
790static LY_ERR
791lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
792{
793 LY_ERR ret = LY_SUCCESS;
794 enum LYJSON_PARSER_STATUS status;
795 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200796 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200797 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200798 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200799 size_t name_len, prefix_len = 0;
800 struct lys_module *mod;
801 struct lyd_meta *meta = NULL;
802 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200803 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200804 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200805 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200806 uint16_t nodetype;
807
808 assert(snode || node);
809
810 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
811
812 /* move to the second item in the name/X pair */
813 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
814 LY_CHECK_GOTO(ret, cleanup);
815
816 /* check attribute encoding */
817 switch (nodetype) {
818 case LYS_LEAFLIST:
819 expected = "@name/array of objects/nulls";
820
821 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
822
823next_entry:
824 instance++;
825
826 /* move into array / next entry */
827 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
828 LY_CHECK_GOTO(ret, cleanup);
829
830 if (status == LYJSON_ARRAY_CLOSED) {
831 /* we are done, move after the array */
832 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
833 goto cleanup;
834 }
835 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
836
Michal Vasko69730152020-10-09 16:30:07 +0200837 if (!node || (node->schema != prev->schema)) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200838 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 +0200839 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200840 ret = LY_EVALID;
841 goto cleanup;
842 }
843
844 if (status == LYJSON_NULL) {
845 /* continue with the next entry in the leaf-list array */
846 prev = node;
847 node = node->next;
848 goto next_entry;
849 }
850 break;
851 case LYS_LEAF:
852 case LYS_ANYXML:
853 expected = "@name/object";
854
aPiecek5b115fc2021-05-24 14:32:42 +0200855 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200856 break;
857 case LYS_CONTAINER:
858 case LYS_LIST:
859 case LYS_ANYDATA:
860 case LYS_NOTIF:
861 case LYS_ACTION:
862 case LYS_RPC:
863 in_parent = 1;
864 expected = "@/object";
865 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
866 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200867 default:
868 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200869 }
870
871 /* process all the members inside a single metadata object */
872 assert(status == LYJSON_OBJECT);
873
Radek Krejciddace2c2021-01-08 11:30:56 +0100874 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100875
Radek Krejci1798aae2020-07-14 13:26:06 +0200876 while (status != LYJSON_OBJECT_CLOSED) {
aPiecekd2c39372021-06-16 14:03:12 +0200877 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 +0100878 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200879
Michal Vasko69d0ca12021-07-23 10:22:03 +0200880 if (!name_len) {
881 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
882 ret = LY_EVALID;
883 goto cleanup;
884 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100885 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100886 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200887 ret = LY_EVALID;
888 goto cleanup;
889 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100890 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100891 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200892 ret = LY_EVALID;
893 goto cleanup;
894 }
895
896 /* get the element module */
897 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
898 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100899 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100900 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 +0100901 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200902 ret = LY_EVALID;
903 goto cleanup;
904 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100905 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200906 /* skip element with children */
907 ret = lydjson_data_skip(lydctx->jsonctx);
908 LY_CHECK_GOTO(ret, cleanup);
909 status = lyjson_ctx_status(lydctx->jsonctx, 0);
910 /* end of the item */
911 continue;
912 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100913 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200914 }
915
916 /* get the value */
917 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
918 LY_CHECK_GOTO(ret, cleanup);
919
920 if (node->schema) {
921 /* create metadata */
922 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200923 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 +0100924 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200925 LY_CHECK_GOTO(ret, cleanup);
926
927 /* add/correct flags */
Michal Vaskoddd76592022-01-17 13:34:48 +0100928 lyd_parse_set_data_flags(node, &lydctx->node_when, &meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200929 } else {
930 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200931 const char *module_name;
932 size_t module_name_len;
933
934 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
935
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100937 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
938 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200939 LY_VALUE_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200940 LY_CHECK_GOTO(ret, cleanup);
941 }
942 /* next member */
943 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
944 LY_CHECK_GOTO(ret, cleanup);
aPiecekde3c6922021-05-24 14:42:10 +0200945 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200946 }
947
948 if (nodetype == LYS_LEAFLIST) {
949 /* continue by processing another metadata object for the following
950 * leaf-list instance since they are allways instantiated in JSON array */
951 prev = node;
952 node = node->next;
953 goto next_entry;
954 }
955
Radek Krejci5813c362021-01-05 10:51:57 +0100956 /* success */
957 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200958
959representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100960 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200961 "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 +0100962 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
963 in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200964
Radek Krejci2efc45b2020-12-22 16:25:44 +0100965 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100966
967cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200968 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100969 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100970 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200971}
972
973/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200974 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
975 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200976 *
977 * @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 +0200978 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
979 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
980 * @param[in] last If set, always insert at the end.
Radek Krejci1798aae2020-07-14 13:26:06 +0200981 */
982static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100983lydjson_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 +0200984{
985 if (*node_p) {
986 /* insert, keep first pointer correct */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100987 lyd_insert_node(parent, first_p, *node_p, last);
Radek Krejci1798aae2020-07-14 13:26:06 +0200988 if (first_p) {
989 if (parent) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100990 *first_p = lyd_child(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200991 } else {
992 while ((*first_p)->prev->next) {
993 *first_p = (*first_p)->prev;
994 }
995 }
996 }
997 *node_p = NULL;
998 }
999}
1000
aPiecek4bf47212021-05-27 10:55:47 +02001001/**
1002 * @brief Wrapper for ::lyd_create_opaq().
1003 *
1004 * @param[in] lydctx JSON data parser context.
1005 * @param[in] name Name of the opaq node to create.
1006 * @param[in] name_len Length of the @p name string.
1007 * @param[in] prefix Prefix of the opaq node to create.
1008 * @param[in] prefix_len Length of the @p prefx string.
1009 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1010 * but must be set if @p first is not.
1011 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1012 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1013 * @param[out] node_p Pointer to the created opaq node.
1014 * @return LY_ERR value.
1015 */
1016static LY_ERR
1017lydjson_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 +01001018 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +02001019{
1020 LY_ERR ret = LY_SUCCESS;
1021 const char *value = NULL, *module_name;
1022 size_t value_len = 0, module_name_len = 0;
1023 ly_bool dynamic = 0;
1024 uint32_t type_hint = 0;
1025
1026 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
1027 /* prepare for creating opaq node with a value */
1028 value = lydctx->jsonctx->value;
1029 value_len = lydctx->jsonctx->value_len;
1030 dynamic = lydctx->jsonctx->dynamic;
1031 lydctx->jsonctx->dynamic = 0;
1032
1033 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
1034 }
1035
1036 /* create node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001037 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +02001038 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
1039 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
1040 if (dynamic) {
1041 free((char *)value);
1042 }
1043
1044 return ret;
1045}
1046
Michal Vaskoe0665742021-02-11 11:08:44 +01001047static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
1048 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +02001049
1050/**
1051 * @brief Parse opaq node from the input.
1052 *
1053 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
1054 *
1055 * @param[in] lydctx JSON data parser context.
1056 * @param[in] name Name of the opaq node to create.
1057 * @param[in] name_len Length of the @p name string.
1058 * @param[in] prefix Prefix of the opaq node to create.
1059 * @param[in] prefix_len Length of the @p prefx string.
1060 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1061 * but must be set if @p first is not.
1062 * @param[in,out] status_p Pointer to the current status of the parser context,
1063 * since the function manipulates with the context and process the input, the status can be updated.
1064 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1065 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1066 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1067 * @param[out] node_p Pointer to the created opaq node.
1068 * @return LY_ERR value.
1069 */
1070static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001071lydjson_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 +01001072 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1073 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001074{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001075 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 +02001076
Michal Vasko1a85d332021-08-27 10:35:28 +02001077 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1078 /* special array null value */
1079 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1080
1081 /* must be the only item */
1082 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
1083 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1084 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
1085 return LY_EVALID;
1086 }
1087
1088 goto finish;
1089 }
1090
aPiecekdbb8baf2021-05-27 11:01:51 +02001091 while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001092 /* process another instance of the same node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001093
Michal Vasko69730152020-10-09 16:30:07 +02001094 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Michal Vasko1a85d332021-08-27 10:35:28 +02001095 /* array with objects, list */
1096 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
1097
Radek Krejci1798aae2020-07-14 13:26:06 +02001098 /* but first process children of the object in the array */
Michal Vasko1a85d332021-08-27 10:35:28 +02001099 while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001100 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +02001101 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
1102 }
Michal Vasko1a85d332021-08-27 10:35:28 +02001103 } else {
1104 /* array with values, leaf-list */
1105 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001106 }
1107
1108 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001109 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1110 goto finish;
1111 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001112
1113 /* continue with the next instance */
aPiecekdbb8baf2021-05-27 11:01:51 +02001114 assert(node_p);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001115 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001116 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 +02001117 }
1118
1119 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
1120 /* process children */
1121 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
1122 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
1123 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001124 }
1125 }
1126
aPiecekdbb8baf2021-05-27 11:01:51 +02001127finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001128 /* finish linking metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001129 return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001130}
1131
1132/**
aPiecek6cee5d02021-05-12 10:32:00 +02001133 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1134 *
1135 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1136 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1137 * for ::lydjson_parse_opaq().
1138 *
1139 * @param[in] lydctx JSON data parser context.
1140 * @param[in] name Name of the opaq node to create.
1141 * @param[in] name_len Length of the @p name string.
1142 * @param[in] prefix Prefix of the opaq node to create.
1143 * @param[in] prefix_len Length of the @p prefx string.
1144 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1145 * but must be set if @p first is not.
1146 * @param[in,out] status_p Pointer to the current status of the parser context,
1147 * since the function manipulates with the context and process the input, the status can be updated.
1148 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1149 * @param[out] node_p Pointer to the created opaq node.
1150 * @return LY_ERR value.
1151 */
1152static LY_ERR
1153lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001154 const char *prefix, size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001155 struct lyd_node **first_p, struct lyd_node **node_p)
1156{
1157 enum LYJSON_PARSER_STATUS status_inner = 0;
1158
1159 /* move to the second item in the name/X pair */
1160 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1161
1162 if (*status_p == LYJSON_ARRAY) {
1163 /* move into the array */
1164 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1165 } else {
1166 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1167 status_inner = LYJSON_ERROR;
1168 }
1169
1170 if (status_inner == LYJSON_ERROR) {
1171 status_inner = *status_p;
1172 }
1173
1174 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001175 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1176 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001177
1178 return LY_SUCCESS;
1179}
1180
1181/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001182 * @brief Process the attribute container (starting by @)
1183 *
1184 * @param[in] lydctx JSON data parser context.
1185 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1186 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1187 * @param[in] name Name of the opaq node to create.
1188 * @param[in] name_len Length of the @p name string.
1189 * @param[in] prefix Prefix of the opaq node to create.
1190 * @param[in] prefix_len Length of the @p prefx string.
1191 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1192 * but must be set if @p first is not.
1193 * @param[in,out] status_p Pointer to the current status of the parser context,
1194 * since the function manipulates with the context and process the input, the status can be updated.
1195 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1196 * @param[out] node_p Pointer to the created opaq node.
1197 * @return LY_ERR value.
1198 */
1199static LY_ERR
1200lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001201 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1202 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001203{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001204 LY_ERR r;
1205 const char *opaq_name;
1206 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001207
1208 /* parse as an attribute to a node */
1209 if (!attr_node && snode) {
1210 /* try to find the instance */
1211 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1212 if (iter->schema == snode) {
1213 attr_node = iter;
1214 break;
1215 }
1216 }
1217 }
1218 if (!attr_node) {
1219 /* parse just as an opaq node with the name beginning with @,
1220 * later we have to check that it belongs to a standard node
1221 * and it is supposed to be converted to a metadata */
1222 uint32_t prev_opts;
1223
Radek Krejci1798aae2020-07-14 13:26:06 +02001224 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001225 prev_opts = lydctx->parse_opts;
1226 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1227 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001228
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001229 opaq_name = prefix ? prefix - 1 : name - 1;
1230 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1231 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 +02001232
1233 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001234 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001235 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001236 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001237 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001238 }
1239
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001240 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001241}
1242
1243/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001244 * @brief Parse a single instance of a node.
1245 *
1246 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1247 * as before calling, despite it is necessary to process input data for checking.
1248 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1249 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1250 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1251 * @param[in] name Parsed JSON node name.
1252 * @param[in] name_len Lenght of @p name.
1253 * @param[in] prefix Parsed JSON node prefix.
1254 * @param[in] prefix_len Length of @p prefix.
1255 * @param[in,out] status JSON parser status, is updated.
1256 * @param[out] node Parsed data (or opaque) node.
1257 * @return LY_SUCCESS if a node was successfully parsed,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001258 * @return LY_ENOT in case of invalid JSON encoding,
Michal Vasko32ac9942020-08-12 14:35:12 +02001259 * @return LY_ERR on other errors.
1260 */
1261static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001262lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001263 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1264 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001265{
1266 LY_ERR ret;
Michal Vaskobbdadda2022-01-06 11:40:10 +01001267 uint32_t type_hints = 0, prev_parse_opts, prev_int_opts;
1268 char *val;
Michal Vasko32ac9942020-08-12 14:35:12 +02001269 struct lyd_node *tree = NULL;
1270
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001271 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001272 if (ret == LY_SUCCESS) {
1273 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1274 if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskobbdadda2022-01-06 11:40:10 +01001275 LY_CHECK_RET((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001276 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
Michal Vaskobbdadda2022-01-06 11:40:10 +01001277
Michal Vasko32ac9942020-08-12 14:35:12 +02001278 /* create terminal node */
1279 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001280 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001281 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001282 LY_CHECK_RET(ret);
1283
1284 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001285 if (*status == LYJSON_ARRAY) {
1286 /* only [null], 2 more moves are needed */
1287 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1288 assert(*status == LYJSON_NULL);
1289 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1290 assert(*status == LYJSON_ARRAY_CLOSED);
1291 }
Michal Vaskobbdadda2022-01-06 11:40:10 +01001292 } else if (snode->nodetype == LYS_ANYXML) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001293 LY_CHECK_RET((*status != LYJSON_STRING) && (*status != LYJSON_NULL), LY_ENOT);
Michal Vaskobbdadda2022-01-06 11:40:10 +01001294
1295 /* create anyxml node */
1296 if (*status == LYJSON_NULL) {
1297 ret = lyd_create_any(snode, NULL, LYD_ANYDATA_STRING, 1, node);
1298 LY_CHECK_RET(ret);
1299 } else if (lydctx->jsonctx->dynamic) {
1300 ret = lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node);
1301 LY_CHECK_RET(ret);
1302 lydctx->jsonctx->dynamic = 0;
1303 } else {
1304 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1305 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1306 ret = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
1307 if (ret) {
1308 free(val);
1309 return ret;
1310 }
1311 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001312 } else if (snode->nodetype & LYD_NODE_INNER) {
1313 /* create inner node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001314 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko32ac9942020-08-12 14:35:12 +02001315
1316 ret = lyd_create_inner(snode, node);
1317 LY_CHECK_RET(ret);
1318
Radek Krejciddace2c2021-01-08 11:30:56 +01001319 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001320
Michal Vasko32ac9942020-08-12 14:35:12 +02001321 /* process children */
Michal Vaskobbdadda2022-01-06 11:40:10 +01001322 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001323 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001324 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001325 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1326 }
1327
1328 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001329 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001330 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001331
1332 if (snode->nodetype == LYS_LIST) {
1333 /* check all keys exist */
1334 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001335 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001336 }
1337
Michal Vaskoe0665742021-02-11 11:08:44 +01001338 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001339 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001340 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001341 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001342
1343 /* add any missing default children */
Michal Vaskoddd76592022-01-17 13:34:48 +01001344 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001345 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001346 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001347 }
1348
Radek Krejciddace2c2021-01-08 11:30:56 +01001349 LOG_LOCBACK(1, 1, 0, 0);
Michal Vaskobbdadda2022-01-06 11:40:10 +01001350 } else if (snode->nodetype == LYS_ANYDATA) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001351 /* create any node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001352 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko32ac9942020-08-12 14:35:12 +02001353
1354 /* parse any data tree with correct options */
1355 /* first backup the current options and then make the parser to process data as opaq nodes */
Michal Vasko02ed9d82021-07-15 14:58:04 +02001356 prev_parse_opts = lydctx->parse_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001357 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1358 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001359 prev_int_opts = lydctx->int_opts;
1360 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vasko32ac9942020-08-12 14:35:12 +02001361
1362 /* process the anydata content */
Michal Vaskobbdadda2022-01-06 11:40:10 +01001363 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001364 ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001365 LY_CHECK_RET(ret);
1366 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1367 }
1368
1369 /* restore parser options */
Michal Vasko02ed9d82021-07-15 14:58:04 +02001370 lydctx->parse_opts = prev_parse_opts;
1371 lydctx->int_opts = prev_int_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001372
1373 /* finish linking metadata */
1374 ret = lydjson_metadata_finish(lydctx, &tree);
1375 LY_CHECK_RET(ret);
1376
Michal Vasko366a4a12020-12-04 16:23:57 +01001377 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001378 LY_CHECK_RET(ret);
1379 }
Michal Vaskocaf608d2021-07-23 13:51:23 +02001380
1381 /* add/correct flags */
Michal Vaskoddd76592022-01-17 13:34:48 +01001382 lyd_parse_set_data_flags(*node, &lydctx->node_when, &(*node)->meta, lydctx->parse_opts);
Michal Vasko32ac9942020-08-12 14:35:12 +02001383 } else if (ret == LY_ENOT) {
1384 /* parse it again as an opaq node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001385 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001386 LY_CHECK_RET(ret);
1387
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001388 if (snode->nodetype == LYS_LIST) {
1389 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1390 } else if (snode->nodetype == LYS_LEAFLIST) {
1391 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001392 }
1393 }
1394
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001395 return LY_SUCCESS;
Michal Vasko32ac9942020-08-12 14:35:12 +02001396}
1397
1398/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001399 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001400 *
1401 * @param[in] lydctx JSON data parser context.
1402 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1403 * @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 +01001404 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001405 * @return LY_ERR value.
1406 */
1407static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001408lydjson_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 +02001409{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001410 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci1798aae2020-07-14 13:26:06 +02001411 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
aPiecek49e2a3a2021-05-13 10:36:46 +02001412 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001413 size_t name_len, prefix_len = 0;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001414 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001415 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001416 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001417 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001418 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001419
1420 assert(parent || first_p);
1421 assert(status == LYJSON_OBJECT);
1422
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001423 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1424 /* all descendants should be parsed */
1425 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1426
Radek Krejci1798aae2020-07-14 13:26:06 +02001427 /* process the node name */
aPiecekd2c39372021-06-16 14:03:12 +02001428 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 +01001429 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001430
Michal Vaskocabe0702020-08-12 10:14:36 +02001431 if (!is_meta || name_len || prefix_len) {
1432 /* get the schema node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001433 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1434 if (r == LY_ENOT) {
1435 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001436 goto cleanup;
1437 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001438 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001439
Michal Vaskocabe0702020-08-12 10:14:36 +02001440 if (!snode) {
1441 /* we will not be parsing it as metadata */
1442 is_meta = 0;
1443 }
1444 }
1445
1446 if (is_meta) {
1447 /* parse as metadata */
1448 if (!name_len && !prefix_len) {
1449 /* parent's metadata without a name - use the schema from the parent */
1450 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001451 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001452 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001453 ret = LY_EVALID;
1454 goto cleanup;
1455 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001456 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001457 snode = attr_node->schema;
1458 }
1459 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001460 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001461 LY_CHECK_GOTO(ret, cleanup);
1462 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001463 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001464 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001465
aPiecekb7b29e62021-05-11 10:02:43 +02001466 /* opaq node cannot have an empty string as the name. */
1467 if (name_len == 0) {
1468 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string.");
1469 ret = LY_EVALID;
1470 goto cleanup;
1471 }
1472
aPiecek6cee5d02021-05-12 10:32:00 +02001473 /* move to the second item in the name/X pair and parse opaq */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001474 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 +02001475 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001476 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001477 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001478
1479 /* move to the second item in the name/X pair */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001480 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001481
1482 /* first check the expected representation according to the nodetype and then continue with the content */
1483 switch (snode->nodetype) {
1484 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001485 case LYS_LIST:
1486 if (snode->nodetype == LYS_LEAFLIST) {
1487 expected = "name/array of values";
1488 } else {
1489 expected = "name/array of objects";
1490 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001491
1492 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1493
1494 /* move into array */
1495 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1496 LY_CHECK_GOTO(ret, cleanup);
1497
Michal Vasko32ac9942020-08-12 14:35:12 +02001498 /* process all the values/objects */
1499 do {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001500 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001501
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001502 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
1503 &status, &node);
1504 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001505 goto representation_error;
1506 } else if (ret) {
1507 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001508 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001509
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001510 /* move after the item(s) */
1511 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1512 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001513
1514 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001515 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001516 case LYS_CONTAINER:
1517 case LYS_NOTIF:
1518 case LYS_ACTION:
1519 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001520 case LYS_ANYDATA:
1521 case LYS_ANYXML:
Michal Vaskobbdadda2022-01-06 11:40:10 +01001522 if (snode->nodetype & (LYS_LEAF | LYS_ANYXML)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001523 if (status == LYJSON_ARRAY) {
1524 expected = "name/[null]";
1525 } else {
1526 expected = "name/value";
1527 }
1528 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001529 expected = "name/object";
1530 }
1531
Michal Vasko32ac9942020-08-12 14:35:12 +02001532 /* process the value/object */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001533 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1534 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001535 goto representation_error;
1536 } else if (ret) {
1537 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001538 }
1539
Michal Vasko32ac9942020-08-12 14:35:12 +02001540 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001541 /* rememeber the RPC/action/notification */
1542 lydctx->op_node = node;
1543 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001544 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001545 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001546 }
1547
Michal Vasko32ac9942020-08-12 14:35:12 +02001548 /* finally connect the parsed node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001549 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001550
1551 /* rememeber a successfully parsed node */
1552 if (parsed) {
1553 ly_set_add(parsed, node, 1, NULL);
1554 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001555
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001556 if (!parse_subtree) {
1557 /* move after the item(s) */
1558 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1559 }
1560
Radek Krejci5813c362021-01-05 10:51:57 +01001561 /* success */
1562 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001563
1564representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001565 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001566 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 +02001567 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001568 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001569 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001570
1571cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001572 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001573 lyd_free_tree(node);
1574 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001575}
1576
1577/**
1578 * @brief Common start of JSON parser processing different types of the input data.
1579 *
1580 * @param[in] ctx libyang context
1581 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001582 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1583 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001584 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001585 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001586 * @return LY_ERR value.
1587 */
1588static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001589lyd_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 +01001590 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001591{
1592 LY_ERR ret = LY_SUCCESS;
1593 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001594 size_t i;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001595 ly_bool subtree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001596
Radek Krejcicd5f6222020-11-12 08:54:13 +01001597 assert(lydctx_p);
1598 assert(status);
1599
Radek Krejci1798aae2020-07-14 13:26:06 +02001600 /* init context */
1601 lydctx = calloc(1, sizeof *lydctx);
1602 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001603 lydctx->parse_opts = parse_opts;
1604 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001605 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001606
Radek Krejci284f31f2020-09-18 15:40:29 +02001607 /* starting top-level */
1608 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001609 if (in->current[i] == '\n') {
1610 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001611 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001612 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001613 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001614
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001615 subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0;
1616 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001617 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001618
Radek Krejcibb27df32020-11-13 15:39:16 +01001619 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001620 *lydctx_p = lydctx;
1621 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001622 } else {
1623 /* expecting top-level object */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001624 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001625 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001626 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001627 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001628 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001629}
1630
1631LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001632lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1633 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 +01001634 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001635{
Michal Vaskoe0665742021-02-11 11:08:44 +01001636 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001637 struct lyd_json_ctx *lydctx = NULL;
1638 enum LYJSON_PARSER_STATUS status;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001639 uint32_t int_opts = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001640
Michal Vaskoe0665742021-02-11 11:08:44 +01001641 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1642 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001643
Radek Krejcicd5f6222020-11-12 08:54:13 +01001644 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001645
Michal Vaskoe0665742021-02-11 11:08:44 +01001646 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001647 case LYD_TYPE_DATA_YANG:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001648 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
1649 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1650 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001651 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001652 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001653 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1654 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001655 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001656 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1657 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001658 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001659 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1660 break;
1661 default:
1662 LOGINT(ctx);
1663 rc = LY_EINT;
1664 goto cleanup;
1665 }
1666 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001667 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001668
1669 /* find the operation node if it exists already */
1670 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1671
Radek Krejci1798aae2020-07-14 13:26:06 +02001672 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001673 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1674 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1675 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001676
1677 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001678
1679 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1680 break;
1681 }
1682 }
1683
1684 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1685 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1686 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1687 rc = LY_EVALID;
1688 goto cleanup;
1689 }
1690 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1691 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1692 rc = LY_EVALID;
1693 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001694 }
1695
1696 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001697 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1698 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001699
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001700 if (parse_opts & LYD_PARSE_SUBTREE) {
1701 /* check for a sibling object */
1702 assert(subtree_sibling);
1703 if (lydctx->jsonctx->in->current[0] == ',') {
1704 *subtree_sibling = 1;
1705
1706 /* move to the next object */
1707 ly_in_skip(lydctx->jsonctx->in, 1);
1708 } else {
1709 *subtree_sibling = 0;
1710 }
1711 }
1712
Radek Krejci1798aae2020-07-14 13:26:06 +02001713cleanup:
1714 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001715 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001716 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001717
Michal Vaskoe0665742021-02-11 11:08:44 +01001718 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001719 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001720 } else {
1721 *lydctx_p = (struct lyd_ctx *)lydctx;
1722 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001723 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001724}