blob: c967063fad0f9d55ec699d954b2bc62af7e9bde9 [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoca0b23b2022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-02-10 15:48:39 +010079 /* give the dynamic value */
aPiecekd2c39372021-06-16 14:03:12 +020080 *dst = (char *)jsonctx->value;
81
Michal Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-02-10 15:48:39 +0100205 /* backup current input */
206 in_bck = *lydctx->jsonctx->in;
Radek Krejci1798aae2020-07-14 13:26:06 +0200207
Michal Vaskoc08b9d22022-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 Vaskoa621c532022-02-14 08:49:51 +0100217 if (in_start.current == in_start.start) {
218 /* invalid JSON */
219 return LY_ENOT;
220 }
Michal Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-02-10 15:48:39 +0100251 /* data was not from this module, continue */
Radek Krejci1798aae2020-07-14 13:26:06 +0200252 }
253
Michal Vaskoc08b9d22022-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;
Michal Vasko69abd362022-02-24 16:06:16 +0100268 uint32_t sublevels, prev_depth;
Radek Krejci1798aae2020-07-14 13:26:06 +0200269
270 status = lyjson_ctx_status(jsonctx, 0);
Michal Vasko69abd362022-02-24 16:06:16 +0100271 if (status == LYJSON_OBJECT) {
272 sublevels = 1;
273 } else {
274 sublevels = 0;
275 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200276
277 /* skip after the content */
278 do {
Michal Vasko69abd362022-02-24 16:06:16 +0100279 prev_depth = jsonctx->depth;
Radek Krejci1798aae2020-07-14 13:26:06 +0200280 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200281
Michal Vasko69abd362022-02-24 16:06:16 +0100282 if ((current == LYJSON_OBJECT) && (prev_depth < jsonctx->depth)) {
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200283 /* lyjson_ctx_next() can return LYSJON_OBJECT in two cases, either when
284 * a new object is encountered, or when it finishes parsing a value from a
285 * previous key-value pair. In the latter case the sublevel shouldn't increase.
Michal Vasko69abd362022-02-24 16:06:16 +0100286 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200287 sublevels++;
Michal Vasko69abd362022-02-24 16:06:16 +0100288 } else if (current == LYJSON_OBJECT_CLOSED) {
Juraj Vijtiuk46eb15e2021-09-06 15:38:36 +0200289 sublevels--;
Radek Krejci1798aae2020-07-14 13:26:06 +0200290 }
Michal Vaskoc2da3072022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vasko8b832092022-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 Vasko8b832092022-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 Vaskoaac267d2022-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 Vaskoaac267d2022-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 Vaskof9bed932022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-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 Vaskoc08b9d22022-02-10 15:48:39 +01001239 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001240 } else {
Michal Vaskoc08b9d22022-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 Vaskoc08b9d22022-02-10 15:48:39 +01001244 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001245}
1246
1247/**
Michal Vasko69abd362022-02-24 16:06:16 +01001248 * @brief Parse a single anydata/anyxml 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] snode Schema node corresponding to the member currently being processed in the context.
1253 * @param[in,out] status JSON parser status, is updated.
1254 * @param[out] node Parsed data (or opaque) node.
1255 * @return LY_SUCCESS if a node was successfully parsed,
1256 * @return LY_ENOT in case of invalid JSON encoding,
1257 * @return LY_ERR on other errors.
1258 */
1259static LY_ERR
1260lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, enum LYJSON_PARSER_STATUS *status,
1261 struct lyd_node **node)
1262{
1263 LY_ERR r;
1264 uint32_t prev_parse_opts, prev_int_opts;
1265 struct ly_in in_start;
1266 char *val;
1267 struct lyd_node *tree = NULL;
1268
1269 assert(snode->nodetype & LYD_NODE_ANY);
1270
1271 /* status check according to allowed JSON types */
1272 if (snode->nodetype == LYS_ANYXML) {
1273 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY) &&
1274 (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) &&
1275 (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
1276 } else {
1277 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY), LY_ENOT);
1278 }
1279
1280 if ((snode->nodetype == LYS_ANYDATA) && (*status == LYJSON_ARRAY)) {
1281 /* only special anydata [null] allowed, 2 more moves are needed */
1282 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1283 LY_CHECK_RET(*status != LYJSON_NULL, LY_ENOT);
1284 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1285 LY_CHECK_RET(*status != LYJSON_ARRAY_CLOSED, LY_ENOT);
1286
1287 return lyd_create_any(snode, "[null]", LYD_ANYDATA_JSON, 0, node);
1288 }
1289
1290 /* create any node */
1291 switch (*status) {
1292 case LYJSON_OBJECT:
1293 /* parse any data tree with correct options, first backup the current options and then make the parser
1294 * process data as opaq nodes */
1295 prev_parse_opts = lydctx->parse_opts;
1296 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1297 lydctx->parse_opts |= LYD_PARSE_OPAQ;
1298 prev_int_opts = lydctx->int_opts;
1299 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
1300
1301 /* process the anydata content */
1302 while (*status != LYJSON_OBJECT_CLOSED) {
1303 LY_CHECK_RET(lydjson_subtree_r(lydctx, NULL, &tree, NULL));
1304 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1305 }
1306
1307 /* restore parser options */
1308 lydctx->parse_opts = prev_parse_opts;
1309 lydctx->int_opts = prev_int_opts;
1310
1311 /* finish linking metadata */
1312 LY_CHECK_RET(lydjson_metadata_finish(lydctx, &tree));
1313
1314 LY_CHECK_RET(lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node));
1315 break;
1316 case LYJSON_ARRAY:
1317 /* skip until the array end */
1318 in_start = *lydctx->jsonctx->in;
1319 LY_CHECK_RET(lydjson_data_skip(lydctx->jsonctx));
1320
1321 /* make a copy of the whole array and store it */
1322 if (asprintf(&val, "[%.*s", (int)(lydctx->jsonctx->in->current - in_start.current), in_start.current) == -1) {
1323 LOGMEM(lydctx->jsonctx->ctx);
1324 return LY_EMEM;
1325 }
1326 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1327 if (r) {
1328 free(val);
1329 return r;
1330 }
1331 break;
1332 case LYJSON_STRING:
1333 /* string value */
1334 if (lydctx->jsonctx->dynamic) {
1335 LY_CHECK_RET(lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node));
1336 lydctx->jsonctx->dynamic = 0;
1337 } else {
1338 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1339 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1340
1341 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
1342 if (r) {
1343 free(val);
1344 return r;
1345 }
1346 }
1347 break;
1348 case LYJSON_NUMBER:
1349 case LYJSON_FALSE:
1350 case LYJSON_TRUE:
1351 /* JSON value */
1352 assert(!lydctx->jsonctx->dynamic);
1353 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1354 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1355
1356 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1357 if (r) {
1358 free(val);
1359 return r;
1360 }
1361 break;
1362 case LYJSON_NULL:
1363 /* no value */
1364 LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node));
1365 break;
1366 case LYJSON_OBJECT_EMPTY:
1367 /* empty object */
1368 LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node));
1369 break;
1370 default:
1371 LOGINT_RET(lydctx->jsonctx->ctx);
1372 }
1373
1374 return LY_SUCCESS;
1375}
1376
1377/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001378 * @brief Parse a single instance of a node.
1379 *
1380 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1381 * as before calling, despite it is necessary to process input data for checking.
1382 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1383 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1384 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1385 * @param[in] name Parsed JSON node name.
1386 * @param[in] name_len Lenght of @p name.
1387 * @param[in] prefix Parsed JSON node prefix.
1388 * @param[in] prefix_len Length of @p prefix.
1389 * @param[in,out] status JSON parser status, is updated.
1390 * @param[out] node Parsed data (or opaque) node.
1391 * @return LY_SUCCESS if a node was successfully parsed,
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001392 * @return LY_ENOT in case of invalid JSON encoding,
Michal Vasko32ac9942020-08-12 14:35:12 +02001393 * @return LY_ERR on other errors.
1394 */
1395static LY_ERR
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001396lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001397 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1398 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001399{
1400 LY_ERR ret;
Michal Vasko69abd362022-02-24 16:06:16 +01001401 uint32_t type_hints = 0;
Michal Vasko32ac9942020-08-12 14:35:12 +02001402
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001403 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001404 if (ret == LY_SUCCESS) {
1405 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1406 if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskoca0b23b2022-01-06 11:40:10 +01001407 LY_CHECK_RET((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001408 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
Michal Vaskoca0b23b2022-01-06 11:40:10 +01001409
Michal Vasko32ac9942020-08-12 14:35:12 +02001410 /* create terminal node */
1411 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001412 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001413 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001414 LY_CHECK_RET(ret);
1415
1416 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001417 if (*status == LYJSON_ARRAY) {
1418 /* only [null], 2 more moves are needed */
1419 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1420 assert(*status == LYJSON_NULL);
1421 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1422 assert(*status == LYJSON_ARRAY_CLOSED);
1423 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001424 } else if (snode->nodetype & LYD_NODE_INNER) {
1425 /* create inner node */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001426 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko32ac9942020-08-12 14:35:12 +02001427
Michal Vasko69abd362022-02-24 16:06:16 +01001428 LY_CHECK_RET(lyd_create_inner(snode, node));
Michal Vasko32ac9942020-08-12 14:35:12 +02001429
Radek Krejciddace2c2021-01-08 11:30:56 +01001430 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001431
Michal Vasko32ac9942020-08-12 14:35:12 +02001432 /* process children */
Michal Vaskoca0b23b2022-01-06 11:40:10 +01001433 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001434 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001435 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001436 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1437 }
1438
1439 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001440 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001441 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001442
1443 if (snode->nodetype == LYS_LIST) {
1444 /* check all keys exist */
1445 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001446 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001447 }
1448
Michal Vaskoe0665742021-02-11 11:08:44 +01001449 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001450 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001451 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001452 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001453
1454 /* add any missing default children */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001455 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001456 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001457 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001458 }
1459
Radek Krejciddace2c2021-01-08 11:30:56 +01001460 LOG_LOCBACK(1, 1, 0, 0);
Michal Vasko69abd362022-02-24 16:06:16 +01001461 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001462 /* create any node */
Michal Vasko69abd362022-02-24 16:06:16 +01001463 LY_CHECK_RET(lydjson_parse_any(lydctx, snode, status, node));
Michal Vasko32ac9942020-08-12 14:35:12 +02001464 }
Michal Vaskocaf608d2021-07-23 13:51:23 +02001465
1466 /* add/correct flags */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001467 lyd_parse_set_data_flags(*node, &lydctx->node_when, &(*node)->meta, lydctx->parse_opts);
Michal Vasko32ac9942020-08-12 14:35:12 +02001468 } else if (ret == LY_ENOT) {
1469 /* parse it again as an opaq node */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001470 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001471 LY_CHECK_RET(ret);
1472
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001473 if (snode->nodetype == LYS_LIST) {
1474 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1475 } else if (snode->nodetype == LYS_LEAFLIST) {
1476 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001477 }
1478 }
1479
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001480 return LY_SUCCESS;
Michal Vasko32ac9942020-08-12 14:35:12 +02001481}
1482
1483/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001484 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001485 *
1486 * @param[in] lydctx JSON data parser context.
1487 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1488 * @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 +01001489 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001490 * @return LY_ERR value.
1491 */
1492static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001493lydjson_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 +02001494{
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001495 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci1798aae2020-07-14 13:26:06 +02001496 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
aPiecek49e2a3a2021-05-13 10:36:46 +02001497 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001498 size_t name_len, prefix_len = 0;
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001499 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001500 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001501 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001502 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001503 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001504
1505 assert(parent || first_p);
1506 assert(status == LYJSON_OBJECT);
1507
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001508 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1509 /* all descendants should be parsed */
1510 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1511
Radek Krejci1798aae2020-07-14 13:26:06 +02001512 /* process the node name */
aPiecekd2c39372021-06-16 14:03:12 +02001513 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001514 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001515
Michal Vaskocabe0702020-08-12 10:14:36 +02001516 if (!is_meta || name_len || prefix_len) {
1517 /* get the schema node */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001518 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode);
1519 if (r == LY_ENOT) {
1520 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001521 goto cleanup;
1522 }
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001523 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001524
Michal Vaskocabe0702020-08-12 10:14:36 +02001525 if (!snode) {
1526 /* we will not be parsing it as metadata */
1527 is_meta = 0;
1528 }
1529 }
1530
1531 if (is_meta) {
1532 /* parse as metadata */
1533 if (!name_len && !prefix_len) {
1534 /* parent's metadata without a name - use the schema from the parent */
1535 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001536 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001537 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001538 ret = LY_EVALID;
1539 goto cleanup;
1540 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001541 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001542 snode = attr_node->schema;
1543 }
1544 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001545 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001546 LY_CHECK_GOTO(ret, cleanup);
1547 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001548 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001549 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001550
aPiecekb7b29e62021-05-11 10:02:43 +02001551 /* opaq node cannot have an empty string as the name. */
1552 if (name_len == 0) {
1553 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string.");
1554 ret = LY_EVALID;
1555 goto cleanup;
1556 }
1557
aPiecek6cee5d02021-05-12 10:32:00 +02001558 /* move to the second item in the name/X pair and parse opaq */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001559 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 +02001560 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001561 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001562 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001563
1564 /* move to the second item in the name/X pair */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001565 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001566
1567 /* first check the expected representation according to the nodetype and then continue with the content */
1568 switch (snode->nodetype) {
1569 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001570 case LYS_LIST:
1571 if (snode->nodetype == LYS_LEAFLIST) {
1572 expected = "name/array of values";
1573 } else {
1574 expected = "name/array of objects";
1575 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001576
1577 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1578
1579 /* move into array */
1580 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1581 LY_CHECK_GOTO(ret, cleanup);
1582
Michal Vasko32ac9942020-08-12 14:35:12 +02001583 /* process all the values/objects */
1584 do {
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001585 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001586
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001587 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len,
1588 &status, &node);
1589 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001590 goto representation_error;
1591 } else if (ret) {
1592 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001593 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001594
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001595 /* move after the item(s) */
1596 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1597 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001598
1599 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001600 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001601 case LYS_CONTAINER:
1602 case LYS_NOTIF:
1603 case LYS_ACTION:
1604 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001605 case LYS_ANYDATA:
1606 case LYS_ANYXML:
Michal Vaskoca0b23b2022-01-06 11:40:10 +01001607 if (snode->nodetype & (LYS_LEAF | LYS_ANYXML)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001608 if (status == LYJSON_ARRAY) {
1609 expected = "name/[null]";
1610 } else {
1611 expected = "name/value";
1612 }
1613 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001614 expected = "name/object";
1615 }
1616
Michal Vasko32ac9942020-08-12 14:35:12 +02001617 /* process the value/object */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001618 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, name, name_len, prefix, prefix_len, &status, &node);
1619 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001620 goto representation_error;
1621 } else if (ret) {
1622 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001623 }
1624
Michal Vasko32ac9942020-08-12 14:35:12 +02001625 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001626 /* rememeber the RPC/action/notification */
1627 lydctx->op_node = node;
1628 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001629 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001630 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001631 }
1632
Michal Vasko32ac9942020-08-12 14:35:12 +02001633 /* finally connect the parsed node */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001634 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001635
1636 /* rememeber a successfully parsed node */
1637 if (parsed) {
1638 ly_set_add(parsed, node, 1, NULL);
1639 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001640
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001641 if (!parse_subtree) {
1642 /* move after the item(s) */
1643 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1644 }
1645
Radek Krejci5813c362021-01-05 10:51:57 +01001646 /* success */
1647 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001648
1649representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001650 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001651 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 +02001652 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001653 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001654 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001655
1656cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001657 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001658 lyd_free_tree(node);
1659 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001660}
1661
1662/**
1663 * @brief Common start of JSON parser processing different types of the input data.
1664 *
1665 * @param[in] ctx libyang context
1666 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001667 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1668 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001669 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001670 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001671 * @return LY_ERR value.
1672 */
1673static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001674lyd_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 +01001675 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001676{
1677 LY_ERR ret = LY_SUCCESS;
1678 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001679 size_t i;
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001680 ly_bool subtree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001681
Radek Krejcicd5f6222020-11-12 08:54:13 +01001682 assert(lydctx_p);
1683 assert(status);
1684
Radek Krejci1798aae2020-07-14 13:26:06 +02001685 /* init context */
1686 lydctx = calloc(1, sizeof *lydctx);
1687 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001688 lydctx->parse_opts = parse_opts;
1689 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001690 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001691
Radek Krejci284f31f2020-09-18 15:40:29 +02001692 /* starting top-level */
1693 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001694 if (in->current[i] == '\n') {
1695 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001696 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001697 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001698 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001699
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001700 subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0;
1701 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001702 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001703
Radek Krejcibb27df32020-11-13 15:39:16 +01001704 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001705 *lydctx_p = lydctx;
1706 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001707 } else {
1708 /* expecting top-level object */
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001709 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001710 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001711 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001712 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001713 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001714}
1715
1716LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001717lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1718 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoaac267d2022-01-17 13:34:48 +01001719 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001720{
Michal Vaskoe0665742021-02-11 11:08:44 +01001721 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001722 struct lyd_json_ctx *lydctx = NULL;
1723 enum LYJSON_PARSER_STATUS status;
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001724 uint32_t int_opts = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001725
Michal Vaskoe0665742021-02-11 11:08:44 +01001726 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1727 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001728
Radek Krejcicd5f6222020-11-12 08:54:13 +01001729 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001730
Michal Vaskoe0665742021-02-11 11:08:44 +01001731 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001732 case LYD_TYPE_DATA_YANG:
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001733 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
1734 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1735 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001736 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001737 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001738 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1739 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001740 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001741 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1742 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001743 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001744 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1745 break;
1746 default:
1747 LOGINT(ctx);
1748 rc = LY_EINT;
1749 goto cleanup;
1750 }
1751 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001752 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001753
1754 /* find the operation node if it exists already */
1755 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1756
Radek Krejci1798aae2020-07-14 13:26:06 +02001757 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001758 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1759 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1760 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001761
1762 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001763
1764 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1765 break;
1766 }
1767 }
1768
1769 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1770 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1771 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1772 rc = LY_EVALID;
1773 goto cleanup;
1774 }
1775 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1776 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1777 rc = LY_EVALID;
1778 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001779 }
1780
1781 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001782 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1783 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001784
Michal Vaskoc08b9d22022-02-10 15:48:39 +01001785 if (parse_opts & LYD_PARSE_SUBTREE) {
1786 /* check for a sibling object */
1787 assert(subtree_sibling);
1788 if (lydctx->jsonctx->in->current[0] == ',') {
1789 *subtree_sibling = 1;
1790
1791 /* move to the next object */
1792 ly_in_skip(lydctx->jsonctx->in, 1);
1793 } else {
1794 *subtree_sibling = 0;
1795 }
1796 }
1797
Radek Krejci1798aae2020-07-14 13:26:06 +02001798cleanup:
1799 /* there should be no unresolved types stored */
Michal Vasko40b80592021-12-09 09:25:33 +01001800 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001801 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001802
Michal Vaskoe0665742021-02-11 11:08:44 +01001803 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001804 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001805 } else {
1806 *lydctx_p = (struct lyd_ctx *)lydctx;
1807 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001808 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001809}