blob: d14151ec9b86ff85ff260402f43c0b566aed441f [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 Vaskoa4af6252022-05-20 10:38:54 +0200186 * @brief Skip the current JSON object/array.
187 *
Radek Krejci1798aae2020-07-14 13:26:06 +0200188 * @param[in] jsonctx JSON context with the input data to skip.
189 * @return LY_ERR value.
190 */
191static LY_ERR
192lydjson_data_skip(struct lyjson_ctx *jsonctx)
193{
194 enum LYJSON_PARSER_STATUS status, current;
Michal Vaskoa4af6252022-05-20 10:38:54 +0200195 uint32_t orig_depth;
Radek Krejci1798aae2020-07-14 13:26:06 +0200196
197 status = lyjson_ctx_status(jsonctx, 0);
Michal Vaskoa4af6252022-05-20 10:38:54 +0200198 assert((status == LYJSON_OBJECT) || (status == LYJSON_ARRAY));
199 orig_depth = jsonctx->depth;
200
201 /* next */
202 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
203
Michal Vaskoa09e8662022-06-15 07:58:24 +0200204 if ((status == LYJSON_OBJECT) && (current != LYJSON_OBJECT) && (current != LYJSON_ARRAY)) {
Michal Vaskoa4af6252022-05-20 10:38:54 +0200205 /* no nested objects */
206 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
207 return LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +0100208 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200209
210 /* skip after the content */
Michal Vaskoa4af6252022-05-20 10:38:54 +0200211 while ((jsonctx->depth > orig_depth) || (current != status + 1)) {
Michal Vasko4f04d762022-09-05 08:38:26 +0200212 if (current == LYJSON_ARRAY) {
213 /* skip the array separately */
214 LY_CHECK_RET(lydjson_data_skip(jsonctx));
215 current = lyjson_ctx_status(jsonctx, 0);
216 } else {
217 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
218 }
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200219
Michal Vaskoa4af6252022-05-20 10:38:54 +0200220 if (current == LYJSON_END) {
221 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200222 }
Michal Vaskoa4af6252022-05-20 10:38:54 +0200223 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200224
225 return LY_SUCCESS;
226}
227
228/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100229 * @brief Get schema node corresponding to the input parameters.
230 *
231 * @param[in] lydctx JSON data parser context.
232 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
233 * @param[in] prefix Requested node's prefix (module name).
234 * @param[in] prefix_len Length of the @p prefix.
235 * @param[in] name Requested node's name.
236 * @param[in] name_len Length of the @p name.
237 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200238 * @param[out] snode Found schema node corresponding to the input parameters. If NULL, parse as an opaque node.
239 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100240 * @return LY_SUCCES on success.
241 * @return LY_ENOT if the whole object was parsed (skipped or as an extension).
242 * @return LY_ERR on error.
243 */
244static LY_ERR
245lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200246 size_t name_len, struct lyd_node *parent, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100247{
248 LY_ERR ret = LY_SUCCESS, r;
249 struct lys_module *mod = NULL;
250 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
251
Michal Vasko8cc3f662022-03-29 11:25:51 +0200252 *snode = NULL;
253 *ext = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100254
Michal Vasko8cc3f662022-03-29 11:25:51 +0200255 /* get the element module, prefer parent context because of extensions */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100256 if (prefix_len) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200257 mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100258 } else if (parent) {
259 if (parent->schema) {
260 mod = parent->schema->module;
261 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +0100262 } else if (!(lydctx->int_opts & LYD_INTOPT_ANY)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
264 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
265 ret = LY_EVALID;
266 goto cleanup;
267 }
268 if (!mod) {
269 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200270 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
271 if (r != LY_ENOT) {
272 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100273 ret = r;
274 goto cleanup;
275 }
276
277 /* unknown module */
278 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
279 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
280 ret = LY_EVALID;
281 goto cleanup;
282 }
283 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
284 /* skip element with children */
285 ret = lydjson_data_skip(lydctx->jsonctx);
286 LY_CHECK_GOTO(ret, cleanup);
287
288 ret = LY_ENOT;
289 goto cleanup;
290 }
291 }
292
293 /* get the schema node */
294 if (mod && (!parent || parent->schema)) {
295 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200296 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200298 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100299 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200300 if (!*snode) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100301 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200302 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
303 if (r != LY_ENOT) {
304 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100305 ret = r;
306 goto cleanup;
307 }
308
309 /* unknown data node */
310 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
311 if (parent) {
312 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
313 (int)name_len, name, LYD_NAME(parent));
314 } else if (lydctx->ext) {
315 if (lydctx->ext->argument) {
316 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
317 "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
318 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
319 } else {
320 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
321 (int)name_len, name, lydctx->ext->def->name);
322 }
323 } else {
324 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
325 (int)name_len, name, mod->name);
326 }
327 ret = LY_EVALID;
328 goto cleanup;
329 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
330 /* skip element with children */
331 ret = lydjson_data_skip(lydctx->jsonctx);
332 LY_CHECK_GOTO(ret, cleanup);
333
334 ret = LY_ENOT;
335 goto cleanup;
336 }
337 } else {
338 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200339 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100340 }
341 }
342
343cleanup:
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100344 return ret;
345}
346
347/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200348 * @brief Check that the input data are parseable as the @p list.
349 *
350 * Checks for all the list's keys. Function does not revert the context state.
351 *
352 * @param[in] jsonctx JSON parser context.
353 * @param[in] list List schema node corresponding to the input data object.
354 * @return LY_SUCCESS in case the data are ok for the @p list
355 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
356 */
357static LY_ERR
358lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
359{
360 LY_ERR ret = LY_SUCCESS;
361 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
362 struct ly_set key_set = {0};
363 const struct lysc_node *snode;
364 uint32_t i, status_count;
365
366 assert(list && (list->nodetype == LYS_LIST));
367 assert(status == LYJSON_OBJECT);
368
369 /* get all keys into a set (keys do not have if-features or anything) */
370 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100371 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200372 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200373 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200374 }
375
376 if (status != LYJSON_OBJECT_EMPTY) {
377 status_count = jsonctx->status.count;
378
379 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
380 const char *name, *prefix;
381 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200382 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200383
384 /* match the key */
385 snode = NULL;
386 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
387
388 if (!is_attr && !prefix) {
389 for (i = 0; i < key_set.count; ++i) {
390 snode = (const struct lysc_node *)key_set.objs[i];
391 if (!ly_strncmp(snode->name, name, name_len)) {
392 break;
393 }
394 }
395 /* go into the item to a) process it as a key or b) start skipping it as another list child */
396 ret = lyjson_ctx_next(jsonctx, &status);
397 LY_CHECK_GOTO(ret, cleanup);
398
399 if (snode) {
400 /* we have the key, validate the value */
401 if (status < LYJSON_NUMBER) {
402 /* not a terminal */
403 ret = LY_ENOT;
404 goto cleanup;
405 }
406
Radek Krejci8df109d2021-04-23 12:19:08 +0200407 ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200408 LY_CHECK_GOTO(ret, cleanup);
409
410 /* key with a valid value, remove from the set */
411 ly_set_rm_index(&key_set, i, NULL);
412 }
413 } else {
414 /* start skipping the member we are not interested in */
415 ret = lyjson_ctx_next(jsonctx, &status);
416 LY_CHECK_GOTO(ret, cleanup);
417 }
418 /* move to the next child */
419 while (status_count < jsonctx->status.count) {
420 ret = lyjson_ctx_next(jsonctx, &status);
421 LY_CHECK_GOTO(ret, cleanup);
422 }
423 }
424 }
425
426 if (key_set.count) {
427 /* some keys are missing/did not validate */
428 ret = LY_ENOT;
429 }
430
431cleanup:
432 ly_set_erase(&key_set, NULL);
433 return ret;
434}
435
436/**
437 * @brief Get the hint for the data type parsers according to the current JSON parser context.
438 *
439 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
440 * @param[in,out] status Pointer to the current context status,
441 * in some circumstances the function manipulates with the context so the status is updated.
442 * @param[out] type_hint_p Pointer to the variable to store the result.
443 * @return LY_SUCCESS in case of success.
444 * @return LY_EINVAL in case of invalid context status not referring to a value.
445 */
446static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200447lydjson_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 +0200448{
449 *type_hint_p = 0;
450
451 if (*status_p == LYJSON_ARRAY) {
452 /* only [null] */
453 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200454 if (*status_p != LYJSON_NULL) {
Michal Vaskod1336a32022-10-25 15:00:20 +0200455 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON,
456 "Expected JSON name/value or special name/[null], but input data contains name/[%s].",
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200457 lyjson_token2str(*status_p));
458 return LY_EINVAL;
459 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200460
461 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200462 if (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED) {
463 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.",
464 lyjson_token2str(*status_p));
465 return LY_EINVAL;
466 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200467
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200468 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200469 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200470 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200471 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200472 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200473 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200474 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200475 } else if (*status_p == LYJSON_NULL) {
476 *type_hint_p = 0;
477 } else {
Michal Vasko0ebc95f2022-07-11 15:55:38 +0200478 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p));
Radek Krejci1798aae2020-07-14 13:26:06 +0200479 return LY_EINVAL;
480 }
481
482 return LY_SUCCESS;
483}
484
485/**
486 * @brief Check in advance if the input data are parsable according to the provided @p snode.
487 *
488 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
489 * is naturally done when the data are really parsed.
490 *
491 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
492 * as before calling, despite it is necessary to process input data for checking.
493 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
494 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
495 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
496 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
497 * @return LY_EINVAL in case of invalid leaf JSON encoding
498 * and they are expected to be parsed as opaq nodes.
499 */
500static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200501lydjson_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 +0200502{
503 LY_ERR ret = LY_SUCCESS;
504 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
505 enum LYJSON_PARSER_STATUS status;
506
507 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200508
Michal Vasko32ac9942020-08-12 14:35:12 +0200509 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
510 /* can always be parsed as a data node if we have the schema node */
511 return LY_SUCCESS;
512 }
513
Michal Vasko85430702021-07-21 14:29:56 +0200514 /* backup parser */
515 lyjson_ctx_backup(jsonctx);
516 status = lyjson_ctx_status(jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200517
Michal Vasko85430702021-07-21 14:29:56 +0200518 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200519 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
520 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200521 switch (snode->nodetype) {
522 case LYS_LEAFLIST:
523 case LYS_LEAF:
524 /* value may not be valid in which case we parse it as an opaque node */
525 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
526 if (ret) {
527 break;
528 }
529
Radek Krejci8df109d2021-04-23 12:19:08 +0200530 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200531 ret = LY_ENOT;
532 }
533 break;
534 case LYS_LIST:
535 /* lists may not have all its keys */
536 if (lydjson_check_list(jsonctx, snode)) {
537 /* invalid list, parse as opaque if it missing/has invalid some keys */
538 ret = LY_ENOT;
539 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200540 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200541 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200542 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200543 status = lyjson_ctx_status(jsonctx, 0);
544 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
545 }
546
Michal Vasko85430702021-07-21 14:29:56 +0200547 /* restore parser */
548 lyjson_ctx_restore(jsonctx);
549
Radek Krejci1798aae2020-07-14 13:26:06 +0200550 return ret;
551}
552
553/**
554 * @brief Join the forward-referencing metadata with their target data nodes.
555 *
556 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
557 *
558 * @param[in] lydctx JSON data parser context.
559 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
560 * as a starting point to search for the metadata's target data node
561 * @return LY_SUCCESS on success
562 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
563 */
564static LY_ERR
565lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
566{
567 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200568 struct lyd_node *node, *attr, *next, *meta_iter;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200569 struct lysc_ext_instance *ext;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200570 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200571 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100572 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200573
574 /* finish linking metadata */
575 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200576 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200577 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200578 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200579 const char *name, *prefix;
580 size_t name_len, prefix_len;
581 const struct lysc_node *snode;
582
Michal Vaskoad92b672020-11-12 13:11:31 +0100583 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200584 /* not an opaq metadata node */
585 continue;
586 }
587
Radek Krejciddace2c2021-01-08 11:30:56 +0100588 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100589 log_location_items++;
590
Michal Vaskoad92b672020-11-12 13:11:31 +0100591 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200592 /* metas' names are stored in dictionary, so checking pointers must works */
593 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100594 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200595 instance = 1;
596 } else {
597 instance++;
598 }
599
aPiecek5057c2e2021-05-17 10:28:03 +0200600 /* find the corresponding data node */
601 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200602 if (!node->schema) {
603 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100604 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200605 continue;
606 }
607
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200608 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100609 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
610 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200611 ret = LY_EVALID;
612 goto cleanup;
613 }
614
615 /* match */
616 match++;
617 if (match != instance) {
618 continue;
619 }
620
621 LY_LIST_FOR(meta_container->child, meta_iter) {
622 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200623 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100624
Michal Vaskoad92b672020-11-12 13:11:31 +0100625 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
626 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200627 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100628 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200629 LY_CHECK_GOTO(ret, cleanup);
630 }
631
632 /* done */
633 break;
634 } else {
635 /* this is the second time we are resolving the schema node, so it must succeed,
636 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100637 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
638 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200639 assert(is_attr);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200640 lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, lyd_parent(*first_p), &snode, &ext);
Radek Krejci1798aae2020-07-14 13:26:06 +0200641
642 if (snode != node->schema) {
643 continue;
644 }
645
646 /* match */
647 match++;
648 if (match != instance) {
649 continue;
650 }
651
652 LY_LIST_FOR(meta_container->child, meta_iter) {
653 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200654 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200655 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200656
Michal Vaskoad92b672020-11-12 13:11:31 +0100657 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200658 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200659 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100660 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100661 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200662 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100663 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200664 if (meta->name.prefix) {
665 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
666 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
667 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
668 meta->name.name);
669 } else {
670 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
671 meta->name.name);
672 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200673 ret = LY_EVALID;
674 goto cleanup;
675 }
676 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200677
Michal Vasko8cc3f662022-03-29 11:25:51 +0200678 /* add/correct flags */
679 ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, ext);
680 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200681 break;
682 }
683 }
684
685 if (match != instance) {
686 /* there is no corresponding data node for the metadata */
687 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100688 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
689 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200690 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200691 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100692 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
693 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200694 }
695 ret = LY_EVALID;
696 } else {
697 /* remove the opaq attr */
698 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200699 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200700 }
701 lyd_free_tree(attr);
702 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100703
Radek Krejciddace2c2021-01-08 11:30:56 +0100704 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100705 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200706 }
707
708cleanup:
709 lydict_remove(lydctx->jsonctx->ctx, prev);
710
Radek Krejciddace2c2021-01-08 11:30:56 +0100711 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200712 return ret;
713}
714
715/**
716 * @brief Parse a metadata member.
717 *
718 * @param[in] lydctx JSON data parser context.
719 * @param[in] snode Schema node of the metadata parent.
720 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
721 * so the data node does not exists. In such a case the metadata is stored in the context for the later
722 * processing by lydjson_metadata_finish().
723 * @return LY_SUCCESS on success
724 * @return Various LY_ERR values in case of failure.
725 */
726static LY_ERR
727lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
728{
729 LY_ERR ret = LY_SUCCESS;
730 enum LYJSON_PARSER_STATUS status;
731 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200732 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200733 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200734 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200735 size_t name_len, prefix_len = 0;
736 struct lys_module *mod;
737 struct lyd_meta *meta = NULL;
738 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200739 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200740 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200741 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200742 uint16_t nodetype;
743
744 assert(snode || node);
745
746 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
747
748 /* move to the second item in the name/X pair */
749 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
750 LY_CHECK_GOTO(ret, cleanup);
751
752 /* check attribute encoding */
753 switch (nodetype) {
754 case LYS_LEAFLIST:
755 expected = "@name/array of objects/nulls";
756
757 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
758
759next_entry:
760 instance++;
761
762 /* move into array / next entry */
763 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
764 LY_CHECK_GOTO(ret, cleanup);
765
766 if (status == LYJSON_ARRAY_CLOSED) {
767 /* we are done, move after the array */
768 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
769 goto cleanup;
770 }
771 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
772
Michal Vasko69730152020-10-09 16:30:07 +0200773 if (!node || (node->schema != prev->schema)) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200774 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 +0200775 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200776 ret = LY_EVALID;
777 goto cleanup;
778 }
779
780 if (status == LYJSON_NULL) {
781 /* continue with the next entry in the leaf-list array */
782 prev = node;
783 node = node->next;
784 goto next_entry;
785 }
786 break;
787 case LYS_LEAF:
788 case LYS_ANYXML:
789 expected = "@name/object";
790
aPiecek5b115fc2021-05-24 14:32:42 +0200791 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200792 break;
793 case LYS_CONTAINER:
794 case LYS_LIST:
795 case LYS_ANYDATA:
796 case LYS_NOTIF:
797 case LYS_ACTION:
798 case LYS_RPC:
799 in_parent = 1;
800 expected = "@/object";
801 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
802 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200803 default:
804 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200805 }
806
807 /* process all the members inside a single metadata object */
808 assert(status == LYJSON_OBJECT);
809
Radek Krejciddace2c2021-01-08 11:30:56 +0100810 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100811
Radek Krejci1798aae2020-07-14 13:26:06 +0200812 while (status != LYJSON_OBJECT_CLOSED) {
Michal Vaskoebcd9a92022-05-23 08:06:34 +0200813 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
814
aPiecekd2c39372021-06-16 14:03:12 +0200815 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 +0100816 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200817
Michal Vasko69d0ca12021-07-23 10:22:03 +0200818 if (!name_len) {
819 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
820 ret = LY_EVALID;
821 goto cleanup;
822 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100823 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100824 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200825 ret = LY_EVALID;
826 goto cleanup;
827 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100828 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100829 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 ret = LY_EVALID;
831 goto cleanup;
832 }
833
834 /* get the element module */
835 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
836 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100837 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100838 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 +0100839 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200840 ret = LY_EVALID;
841 goto cleanup;
842 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100843 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200844 /* skip element with children */
845 ret = lydjson_data_skip(lydctx->jsonctx);
846 LY_CHECK_GOTO(ret, cleanup);
847 status = lyjson_ctx_status(lydctx->jsonctx, 0);
848 /* end of the item */
849 continue;
850 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100851 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200852 }
853
854 /* get the value */
855 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
856 LY_CHECK_GOTO(ret, cleanup);
857
858 if (node->schema) {
859 /* create metadata */
860 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200861 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 +0100862 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200863 LY_CHECK_GOTO(ret, cleanup);
864
865 /* add/correct flags */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200866 ret = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, NULL);
867 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200868 } else {
869 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200870 const char *module_name;
871 size_t module_name_len;
872
873 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
874
Radek Krejci1798aae2020-07-14 13:26:06 +0200875 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100876 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
877 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200878 LY_VALUE_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200879 LY_CHECK_GOTO(ret, cleanup);
880 }
881 /* next member */
882 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
883 LY_CHECK_GOTO(ret, cleanup);
aPiecekde3c6922021-05-24 14:42:10 +0200884 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200885 }
886
887 if (nodetype == LYS_LEAFLIST) {
888 /* continue by processing another metadata object for the following
889 * leaf-list instance since they are allways instantiated in JSON array */
890 prev = node;
891 node = node->next;
892 goto next_entry;
893 }
894
Radek Krejci5813c362021-01-05 10:51:57 +0100895 /* success */
896 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200897
898representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100899 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200900 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
Michal Vasko5722c1b2022-01-24 07:47:49 +0100901 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
902 in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200903
Radek Krejci2efc45b2020-12-22 16:25:44 +0100904 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100905
906cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200907 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100908 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100909 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200910}
911
912/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200913 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
914 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200915 *
916 * @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 +0200917 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
918 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
919 * @param[in] last If set, always insert at the end.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200920 * @param[in] ext Extension instance of @p node_p, if any.
Radek Krejci1798aae2020-07-14 13:26:06 +0200921 */
922static void
Michal Vasko8cc3f662022-03-29 11:25:51 +0200923lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last,
924 struct lysc_ext_instance *ext)
Radek Krejci1798aae2020-07-14 13:26:06 +0200925{
926 if (*node_p) {
927 /* insert, keep first pointer correct */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200928 if (ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200929 lyplg_ext_insert(parent, *node_p);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200930 } else {
931 lyd_insert_node(parent, first_p, *node_p, last);
932 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200933 if (first_p) {
934 if (parent) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100935 *first_p = lyd_child(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 } else {
937 while ((*first_p)->prev->next) {
938 *first_p = (*first_p)->prev;
939 }
940 }
941 }
942 *node_p = NULL;
943 }
944}
945
aPiecek4bf47212021-05-27 10:55:47 +0200946/**
947 * @brief Wrapper for ::lyd_create_opaq().
948 *
949 * @param[in] lydctx JSON data parser context.
950 * @param[in] name Name of the opaq node to create.
951 * @param[in] name_len Length of the @p name string.
952 * @param[in] prefix Prefix of the opaq node to create.
953 * @param[in] prefix_len Length of the @p prefx string.
954 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
955 * but must be set if @p first is not.
956 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
957 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
958 * @param[out] node_p Pointer to the created opaq node.
959 * @return LY_ERR value.
960 */
961static LY_ERR
962lydjson_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 +0100963 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +0200964{
965 LY_ERR ret = LY_SUCCESS;
966 const char *value = NULL, *module_name;
967 size_t value_len = 0, module_name_len = 0;
968 ly_bool dynamic = 0;
969 uint32_t type_hint = 0;
970
971 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
972 /* prepare for creating opaq node with a value */
973 value = lydctx->jsonctx->value;
974 value_len = lydctx->jsonctx->value_len;
975 dynamic = lydctx->jsonctx->dynamic;
976 lydctx->jsonctx->dynamic = 0;
977
978 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
979 }
980
981 /* create node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100982 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +0200983 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
984 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
985 if (dynamic) {
986 free((char *)value);
987 }
988
989 return ret;
990}
991
Michal Vaskoe0665742021-02-11 11:08:44 +0100992static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
993 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200994
995/**
996 * @brief Parse opaq node from the input.
997 *
998 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
999 *
1000 * @param[in] lydctx JSON data parser context.
1001 * @param[in] name Name of the opaq node to create.
1002 * @param[in] name_len Length of the @p name string.
1003 * @param[in] prefix Prefix of the opaq node to create.
1004 * @param[in] prefix_len Length of the @p prefx string.
1005 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1006 * but must be set if @p first is not.
1007 * @param[in,out] status_p Pointer to the current status of the parser context,
1008 * since the function manipulates with the context and process the input, the status can be updated.
1009 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
1010 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
1011 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1012 * @param[out] node_p Pointer to the created opaq node.
1013 * @return LY_ERR value.
1014 */
1015static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001016lydjson_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 +01001017 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1018 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001019{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001020 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 +02001021
Michal Vasko1a85d332021-08-27 10:35:28 +02001022 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1023 /* special array null value */
1024 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1025
1026 /* must be the only item */
1027 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
1028 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1029 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
1030 return LY_EVALID;
1031 }
1032
1033 goto finish;
1034 }
1035
aPiecekdbb8baf2021-05-27 11:01:51 +02001036 while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001037 /* process another instance of the same node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001038
Michal Vasko69730152020-10-09 16:30:07 +02001039 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Michal Vasko1a85d332021-08-27 10:35:28 +02001040 /* array with objects, list */
1041 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
1042
Radek Krejci1798aae2020-07-14 13:26:06 +02001043 /* but first process children of the object in the array */
Michal Vasko1a85d332021-08-27 10:35:28 +02001044 while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001045 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +02001046 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
1047 }
Michal Vasko1a85d332021-08-27 10:35:28 +02001048 } else {
1049 /* array with values, leaf-list */
1050 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001051 }
1052
1053 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001054 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1055 goto finish;
1056 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001057
1058 /* continue with the next instance */
aPiecekdbb8baf2021-05-27 11:01:51 +02001059 assert(node_p);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001060 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001061 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 +02001062 }
1063
1064 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
1065 /* process children */
1066 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
1067 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
1068 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001069 }
1070 }
1071
aPiecekdbb8baf2021-05-27 11:01:51 +02001072finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001073 /* finish linking metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001074 return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001075}
1076
1077/**
aPiecek6cee5d02021-05-12 10:32:00 +02001078 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1079 *
1080 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1081 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1082 * for ::lydjson_parse_opaq().
1083 *
1084 * @param[in] lydctx JSON data parser context.
1085 * @param[in] name Name of the opaq node to create.
1086 * @param[in] name_len Length of the @p name string.
1087 * @param[in] prefix Prefix of the opaq node to create.
1088 * @param[in] prefix_len Length of the @p prefx string.
1089 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1090 * but must be set if @p first is not.
1091 * @param[in,out] status_p Pointer to the current status of the parser context,
1092 * since the function manipulates with the context and process the input, the status can be updated.
1093 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1094 * @param[out] node_p Pointer to the created opaq node.
1095 * @return LY_ERR value.
1096 */
1097static LY_ERR
1098lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001099 const char *prefix, size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001100 struct lyd_node **first_p, struct lyd_node **node_p)
1101{
1102 enum LYJSON_PARSER_STATUS status_inner = 0;
1103
1104 /* move to the second item in the name/X pair */
1105 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1106
1107 if (*status_p == LYJSON_ARRAY) {
1108 /* move into the array */
1109 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1110 } else {
1111 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1112 status_inner = LYJSON_ERROR;
1113 }
1114
1115 if (status_inner == LYJSON_ERROR) {
1116 status_inner = *status_p;
1117 }
1118
1119 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001120 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1121 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001122
1123 return LY_SUCCESS;
1124}
1125
1126/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001127 * @brief Process the attribute container (starting by @)
1128 *
1129 * @param[in] lydctx JSON data parser context.
1130 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1131 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1132 * @param[in] name Name of the opaq node to create.
1133 * @param[in] name_len Length of the @p name string.
1134 * @param[in] prefix Prefix of the opaq node to create.
1135 * @param[in] prefix_len Length of the @p prefx string.
1136 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1137 * but must be set if @p first is not.
1138 * @param[in,out] status_p Pointer to the current status of the parser context,
1139 * since the function manipulates with the context and process the input, the status can be updated.
1140 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1141 * @param[out] node_p Pointer to the created opaq node.
1142 * @return LY_ERR value.
1143 */
1144static LY_ERR
1145lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001146 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1147 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001148{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001149 LY_ERR r;
1150 const char *opaq_name;
1151 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001152
1153 /* parse as an attribute to a node */
1154 if (!attr_node && snode) {
1155 /* try to find the instance */
1156 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1157 if (iter->schema == snode) {
1158 attr_node = iter;
1159 break;
1160 }
1161 }
1162 }
1163 if (!attr_node) {
1164 /* parse just as an opaq node with the name beginning with @,
1165 * later we have to check that it belongs to a standard node
1166 * and it is supposed to be converted to a metadata */
1167 uint32_t prev_opts;
1168
Radek Krejci1798aae2020-07-14 13:26:06 +02001169 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001170 prev_opts = lydctx->parse_opts;
1171 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1172 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001173
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001174 opaq_name = prefix ? prefix - 1 : name - 1;
1175 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1176 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 +02001177
1178 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001179 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001180 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001181 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001182 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001183 }
1184
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001185 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001186}
1187
1188/**
Michal Vasko76096ec2022-02-24 16:06:16 +01001189 * @brief Parse a single anydata/anyxml node.
1190 *
1191 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1192 * as before calling, despite it is necessary to process input data for checking.
1193 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001194 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko76096ec2022-02-24 16:06:16 +01001195 * @param[in,out] status JSON parser status, is updated.
1196 * @param[out] node Parsed data (or opaque) node.
1197 * @return LY_SUCCESS if a node was successfully parsed,
1198 * @return LY_ENOT in case of invalid JSON encoding,
1199 * @return LY_ERR on other errors.
1200 */
1201static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +02001202lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1203 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko76096ec2022-02-24 16:06:16 +01001204{
1205 LY_ERR r;
1206 uint32_t prev_parse_opts, prev_int_opts;
1207 struct ly_in in_start;
1208 char *val;
1209 struct lyd_node *tree = NULL;
1210
1211 assert(snode->nodetype & LYD_NODE_ANY);
1212
1213 /* status check according to allowed JSON types */
1214 if (snode->nodetype == LYS_ANYXML) {
1215 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY) &&
1216 (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) &&
1217 (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
1218 } else {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001219 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko76096ec2022-02-24 16:06:16 +01001220 }
1221
1222 /* create any node */
1223 switch (*status) {
1224 case LYJSON_OBJECT:
1225 /* parse any data tree with correct options, first backup the current options and then make the parser
1226 * process data as opaq nodes */
1227 prev_parse_opts = lydctx->parse_opts;
1228 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001229 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vasko76096ec2022-02-24 16:06:16 +01001230 prev_int_opts = lydctx->int_opts;
1231 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
1232
1233 /* process the anydata content */
1234 while (*status != LYJSON_OBJECT_CLOSED) {
1235 LY_CHECK_RET(lydjson_subtree_r(lydctx, NULL, &tree, NULL));
1236 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1237 }
1238
1239 /* restore parser options */
1240 lydctx->parse_opts = prev_parse_opts;
1241 lydctx->int_opts = prev_int_opts;
1242
1243 /* finish linking metadata */
1244 LY_CHECK_RET(lydjson_metadata_finish(lydctx, &tree));
1245
1246 LY_CHECK_RET(lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node));
1247 break;
1248 case LYJSON_ARRAY:
1249 /* skip until the array end */
1250 in_start = *lydctx->jsonctx->in;
1251 LY_CHECK_RET(lydjson_data_skip(lydctx->jsonctx));
1252
1253 /* make a copy of the whole array and store it */
1254 if (asprintf(&val, "[%.*s", (int)(lydctx->jsonctx->in->current - in_start.current), in_start.current) == -1) {
1255 LOGMEM(lydctx->jsonctx->ctx);
1256 return LY_EMEM;
1257 }
1258 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1259 if (r) {
1260 free(val);
1261 return r;
1262 }
1263 break;
1264 case LYJSON_STRING:
1265 /* string value */
1266 if (lydctx->jsonctx->dynamic) {
1267 LY_CHECK_RET(lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node));
1268 lydctx->jsonctx->dynamic = 0;
1269 } else {
1270 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1271 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1272
1273 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
1274 if (r) {
1275 free(val);
1276 return r;
1277 }
1278 }
1279 break;
1280 case LYJSON_NUMBER:
1281 case LYJSON_FALSE:
1282 case LYJSON_TRUE:
1283 /* JSON value */
1284 assert(!lydctx->jsonctx->dynamic);
1285 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1286 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1287
1288 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1289 if (r) {
1290 free(val);
1291 return r;
1292 }
1293 break;
1294 case LYJSON_NULL:
1295 /* no value */
1296 LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node));
1297 break;
1298 case LYJSON_OBJECT_EMPTY:
1299 /* empty object */
1300 LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node));
1301 break;
1302 default:
1303 LOGINT_RET(lydctx->jsonctx->ctx);
1304 }
1305
1306 return LY_SUCCESS;
1307}
1308
1309/**
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001310 * @brief Parse a single instance of an inner node.
1311 *
1312 * @param[in] lydctx JSON data parser context.
1313 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1314 * @param[in] ext Extension instance of @p snode, if any.
1315 * @param[in,out] status JSON parser status, is updated.
1316 * @param[out] node Parsed data (or opaque) node.
1317 * @return LY_SUCCESS if a node was successfully parsed,
1318 * @return LY_ENOT in case of invalid JSON encoding,
1319 * @return LY_ERR on other errors.
1320 */
1321static LY_ERR
1322lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1323 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
1324{
1325 LY_ERR ret = LY_SUCCESS;
1326 uint32_t prev_parse_opts = lydctx->parse_opts;
1327
1328 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
1329
1330 /* create inner node */
1331 LY_CHECK_RET(lyd_create_inner(snode, node));
1332
1333 /* use it for logging */
1334 LOG_LOCSET(NULL, *node, NULL, NULL);
1335
1336 if (ext) {
1337 /* only parse these extension data and validate afterwards */
1338 lydctx->parse_opts |= LYD_PARSE_ONLY;
1339 }
1340
1341 /* process children */
1342 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
1343 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
1344 LY_CHECK_GOTO(ret, cleanup);
1345 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1346 }
1347
1348 /* finish linking metadata */
1349 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
1350 LY_CHECK_GOTO(ret, cleanup);
1351
1352 if (snode->nodetype == LYS_LIST) {
1353 /* check all keys exist */
1354 ret = lyd_parse_check_keys(*node);
1355 LY_CHECK_GOTO(ret, cleanup);
1356 }
1357
1358 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1359 /* new node validation, autodelete CANNOT occur, all nodes are new */
1360 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
1361 LY_CHECK_GOTO(ret, cleanup);
1362
1363 /* add any missing default children */
1364 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when,
1365 &lydctx->node_types, &lydctx->ext_node,
1366 (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
1367 LY_CHECK_GOTO(ret, cleanup);
1368 }
1369
1370cleanup:
1371 lydctx->parse_opts = prev_parse_opts;
1372 LOG_LOCBACK(0, 1, 0, 0);
1373 return ret;
1374}
1375
1376/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001377 * @brief Parse a single instance of a node.
1378 *
1379 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1380 * as before calling, despite it is necessary to process input data for checking.
1381 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1382 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1383 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001384 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko32ac9942020-08-12 14:35:12 +02001385 * @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 Vasko61ad1ff2022-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 Vasko61ad1ff2022-02-10 15:48:39 +01001396lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001397 const struct lysc_node *snode, struct lysc_ext_instance *ext, const char *name, size_t name_len,
1398 const char *prefix, size_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001399{
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001400 LY_ERR ret = LY_SUCCESS;
Michal Vasko76096ec2022-02-24 16:06:16 +01001401 uint32_t type_hints = 0;
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001402
1403 LOG_LOCSET(snode, NULL, NULL, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001404
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001405 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001406 if (ret == LY_SUCCESS) {
1407 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1408 if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001409 if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
1410 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) {
1411 ret = LY_ENOT;
1412 goto cleanup;
1413 }
Michal Vaskobbdadda2022-01-06 11:40:10 +01001414
Michal Vasko32ac9942020-08-12 14:35:12 +02001415 /* create terminal node */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001416 LY_CHECK_GOTO(ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
1417 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, type_hints, node), cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001418
1419 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001420 if (*status == LYJSON_ARRAY) {
1421 /* only [null], 2 more moves are needed */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001422 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status), cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001423 assert(*status == LYJSON_NULL);
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001424 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status), cleanup);
Michal Vasko85430702021-07-21 14:29:56 +02001425 assert(*status == LYJSON_ARRAY_CLOSED);
1426 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001427 } else if (snode->nodetype & LYD_NODE_INNER) {
1428 /* create inner node */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001429 LY_CHECK_GOTO(ret = lydjson_parse_instance_inner(lydctx, snode, ext, status, node), cleanup);
Michal Vasko76096ec2022-02-24 16:06:16 +01001430 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001431 /* create any node */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001432 LY_CHECK_GOTO(ret = lydjson_parse_any(lydctx, snode, ext, status, node), cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001433 }
Michal Vaskocaf608d2021-07-23 13:51:23 +02001434
1435 /* add/correct flags */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001436 LY_CHECK_GOTO(ret = lyd_parse_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001437
Michal Vaskoeba23112022-08-26 08:35:41 +02001438 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1439 /* store for ext instance node validation, if needed */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001440 LY_CHECK_GOTO(ret = lyd_validate_node_ext(*node, &lydctx->ext_node), cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001441 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001442 } else if (ret == LY_ENOT) {
1443 /* parse it again as an opaq node */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001444 LY_CHECK_GOTO(ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status,
1445 first_p, node), cleanup);
Michal Vasko32ac9942020-08-12 14:35:12 +02001446
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001447 if (snode->nodetype == LYS_LIST) {
1448 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1449 } else if (snode->nodetype == LYS_LEAFLIST) {
1450 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001451 }
Michal Vaskoa32b6b72022-07-11 15:55:56 +02001452 } else {
1453 /* error */
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001454 goto cleanup;
Michal Vasko32ac9942020-08-12 14:35:12 +02001455 }
1456
Michal Vaskoe0608fa2022-10-25 15:01:24 +02001457cleanup:
1458 LOG_LOCBACK(1, 0, 0, 0);
1459 return ret;
Michal Vasko32ac9942020-08-12 14:35:12 +02001460}
1461
1462/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001463 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001464 *
1465 * @param[in] lydctx JSON data parser context.
1466 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1467 * @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 +01001468 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001469 * @return LY_ERR value.
1470 */
1471static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001472lydjson_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 +02001473{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001474 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci1798aae2020-07-14 13:26:06 +02001475 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
aPiecek49e2a3a2021-05-13 10:36:46 +02001476 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001477 size_t name_len, prefix_len = 0;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001478 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001479 const struct lysc_node *snode = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001480 struct lysc_ext_instance *ext;
Michal Vasko32ac9942020-08-12 14:35:12 +02001481 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001482 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001483 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001484
1485 assert(parent || first_p);
1486 assert(status == LYJSON_OBJECT);
1487
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001488 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1489 /* all descendants should be parsed */
1490 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1491
Radek Krejci1798aae2020-07-14 13:26:06 +02001492 /* process the node name */
aPiecekd2c39372021-06-16 14:03:12 +02001493 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 +01001494 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001495
Michal Vaskocabe0702020-08-12 10:14:36 +02001496 if (!is_meta || name_len || prefix_len) {
1497 /* get the schema node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001498 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001499 if (r == LY_ENOT) {
1500 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001501 goto cleanup;
1502 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001503 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001504
Michal Vaskocabe0702020-08-12 10:14:36 +02001505 if (!snode) {
1506 /* we will not be parsing it as metadata */
1507 is_meta = 0;
1508 }
1509 }
1510
1511 if (is_meta) {
1512 /* parse as metadata */
1513 if (!name_len && !prefix_len) {
1514 /* parent's metadata without a name - use the schema from the parent */
1515 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001516 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001517 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001518 ret = LY_EVALID;
1519 goto cleanup;
1520 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001521 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001522 snode = attr_node->schema;
1523 }
1524 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001525 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001526 LY_CHECK_GOTO(ret, cleanup);
1527 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001528 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001529 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001530
aPiecekb7b29e62021-05-11 10:02:43 +02001531 /* opaq node cannot have an empty string as the name. */
1532 if (name_len == 0) {
1533 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string.");
1534 ret = LY_EVALID;
1535 goto cleanup;
1536 }
1537
aPiecek6cee5d02021-05-12 10:32:00 +02001538 /* move to the second item in the name/X pair and parse opaq */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001539 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 +02001540 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001541 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001542 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001543
1544 /* move to the second item in the name/X pair */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001545 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001546
1547 /* first check the expected representation according to the nodetype and then continue with the content */
1548 switch (snode->nodetype) {
1549 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001550 case LYS_LIST:
1551 if (snode->nodetype == LYS_LEAFLIST) {
1552 expected = "name/array of values";
1553 } else {
1554 expected = "name/array of objects";
1555 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001556
Michal Vasko4e26adc2022-03-30 11:01:16 +02001557 if (status == LYJSON_ARRAY_EMPTY) {
1558 /* no instances, skip */
1559 break;
1560 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001561 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1562
1563 /* move into array */
1564 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1565 LY_CHECK_GOTO(ret, cleanup);
1566
Michal Vasko32ac9942020-08-12 14:35:12 +02001567 /* process all the values/objects */
1568 do {
Michal Vasko8cc3f662022-03-29 11:25:51 +02001569 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001570 &status, &node);
1571 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001572 goto representation_error;
1573 } else if (ret) {
1574 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001575 }
Michal Vasko8cc3f662022-03-29 11:25:51 +02001576 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Radek Krejci1798aae2020-07-14 13:26:06 +02001577
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001578 /* move after the item(s) */
1579 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1580 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001581
1582 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001583 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001584 case LYS_CONTAINER:
1585 case LYS_NOTIF:
1586 case LYS_ACTION:
1587 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001588 case LYS_ANYDATA:
1589 case LYS_ANYXML:
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001590 if ((snode->nodetype == LYS_LEAF) || (snode->nodetype == LYS_ANYXML)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001591 if (status == LYJSON_ARRAY) {
1592 expected = "name/[null]";
1593 } else {
1594 expected = "name/value";
1595 }
1596 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001597 expected = "name/object";
1598 }
1599
Michal Vasko32ac9942020-08-12 14:35:12 +02001600 /* process the value/object */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001601 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
1602 &status, &node);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001603 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001604 goto representation_error;
1605 } else if (ret) {
1606 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001607 }
1608
Michal Vasko32ac9942020-08-12 14:35:12 +02001609 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001610 /* rememeber the RPC/action/notification */
1611 lydctx->op_node = node;
1612 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001613 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001614 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001615 }
1616
Michal Vasko32ac9942020-08-12 14:35:12 +02001617 /* finally connect the parsed node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001618 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Michal Vaskoe0665742021-02-11 11:08:44 +01001619
1620 /* rememeber a successfully parsed node */
Michal Vasko4e26adc2022-03-30 11:01:16 +02001621 if (parsed && node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001622 ly_set_add(parsed, node, 1, NULL);
1623 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001624
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001625 if (!parse_subtree) {
1626 /* move after the item(s) */
1627 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1628 }
1629
Radek Krejci5813c362021-01-05 10:51:57 +01001630 /* success */
1631 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001632
1633representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001634 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 +02001635 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejci1798aae2020-07-14 13:26:06 +02001636 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001637
1638cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001639 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001640 lyd_free_tree(node);
1641 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001642}
1643
1644/**
1645 * @brief Common start of JSON parser processing different types of the input data.
1646 *
1647 * @param[in] ctx libyang context
1648 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001649 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1650 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001651 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001652 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001653 * @return LY_ERR value.
1654 */
1655static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001656lyd_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 +01001657 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001658{
1659 LY_ERR ret = LY_SUCCESS;
1660 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001661 size_t i;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001662 ly_bool subtree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001663
Radek Krejcicd5f6222020-11-12 08:54:13 +01001664 assert(lydctx_p);
1665 assert(status);
1666
Radek Krejci1798aae2020-07-14 13:26:06 +02001667 /* init context */
1668 lydctx = calloc(1, sizeof *lydctx);
1669 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001670 lydctx->parse_opts = parse_opts;
1671 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001672 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001673
Radek Krejci284f31f2020-09-18 15:40:29 +02001674 /* starting top-level */
1675 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001676 if (in->current[i] == '\n') {
1677 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001678 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001679 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001680 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001681
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001682 subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0;
1683 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001684 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001685
Radek Krejcibb27df32020-11-13 15:39:16 +01001686 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001687 *lydctx_p = lydctx;
1688 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001689 } else {
1690 /* expecting top-level object */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001691 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001692 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001693 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001694 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001695 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001696}
1697
1698LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001699lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001700 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
Michal Vaskoddd76592022-01-17 13:34:48 +01001701 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001702{
Michal Vaskoe0665742021-02-11 11:08:44 +01001703 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001704 struct lyd_json_ctx *lydctx = NULL;
1705 enum LYJSON_PARSER_STATUS status;
1706
Michal Vaskoe0665742021-02-11 11:08:44 +01001707 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1708 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001709
Radek Krejcicd5f6222020-11-12 08:54:13 +01001710 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001711
Michal Vaskoe0665742021-02-11 11:08:44 +01001712 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001713 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001714
1715 /* find the operation node if it exists already */
1716 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1717
Radek Krejci1798aae2020-07-14 13:26:06 +02001718 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001719 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1720 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1721 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001722
1723 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001724
1725 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1726 break;
1727 }
1728 }
1729
1730 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1731 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1732 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1733 rc = LY_EVALID;
1734 goto cleanup;
1735 }
1736 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1737 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1738 rc = LY_EVALID;
1739 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001740 }
1741
1742 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001743 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1744 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001745
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001746 if (parse_opts & LYD_PARSE_SUBTREE) {
1747 /* check for a sibling object */
1748 assert(subtree_sibling);
1749 if (lydctx->jsonctx->in->current[0] == ',') {
1750 *subtree_sibling = 1;
1751
1752 /* move to the next object */
1753 ly_in_skip(lydctx->jsonctx->in, 1);
1754 } else {
1755 *subtree_sibling = 0;
1756 }
1757 }
1758
Radek Krejci1798aae2020-07-14 13:26:06 +02001759cleanup:
1760 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001761 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001762 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001763
Michal Vaskoe0665742021-02-11 11:08:44 +01001764 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001765 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001766 } else {
1767 *lydctx_p = (struct lyd_ctx *)lydctx;
Michal Vaskof8ebf132022-11-21 14:06:48 +01001768
1769 /* the JSON context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1770 lyjson_ctx_free(lydctx->jsonctx);
1771 lydctx->jsonctx = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001772 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001773 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001774}