blob: d54d94ae898f2ca7f7a84ae578c8f94795d89c9d [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)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200212 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200213
Michal Vaskoa4af6252022-05-20 10:38:54 +0200214 if (current == LYJSON_END) {
215 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200216 }
Michal Vaskoa4af6252022-05-20 10:38:54 +0200217 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200218
219 return LY_SUCCESS;
220}
221
222/**
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100223 * @brief Get schema node corresponding to the input parameters.
224 *
225 * @param[in] lydctx JSON data parser context.
226 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
227 * @param[in] prefix Requested node's prefix (module name).
228 * @param[in] prefix_len Length of the @p prefix.
229 * @param[in] name Requested node's name.
230 * @param[in] name_len Length of the @p name.
231 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200232 * @param[out] snode Found schema node corresponding to the input parameters. If NULL, parse as an opaque node.
233 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100234 * @return LY_SUCCES on success.
235 * @return LY_ENOT if the whole object was parsed (skipped or as an extension).
236 * @return LY_ERR on error.
237 */
238static LY_ERR
239lydjson_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 +0200240 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 +0100241{
242 LY_ERR ret = LY_SUCCESS, r;
243 struct lys_module *mod = NULL;
244 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
245
Michal Vasko8cc3f662022-03-29 11:25:51 +0200246 *snode = NULL;
247 *ext = NULL;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100248
249 LOG_LOCSET(NULL, parent, NULL, NULL);
250
Michal Vasko8cc3f662022-03-29 11:25:51 +0200251 /* get the element module, prefer parent context because of extensions */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100252 if (prefix_len) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200253 mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100254 } else if (parent) {
255 if (parent->schema) {
256 mod = parent->schema->module;
257 }
258 } else {
259 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
260 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
261 ret = LY_EVALID;
262 goto cleanup;
263 }
264 if (!mod) {
265 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200266 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
267 if (r != LY_ENOT) {
268 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100269 ret = r;
270 goto cleanup;
271 }
272
273 /* unknown module */
274 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
275 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
276 ret = LY_EVALID;
277 goto cleanup;
278 }
279 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
280 /* skip element with children */
281 ret = lydjson_data_skip(lydctx->jsonctx);
282 LY_CHECK_GOTO(ret, cleanup);
283
284 ret = LY_ENOT;
285 goto cleanup;
286 }
287 }
288
289 /* get the schema node */
290 if (mod && (!parent || parent->schema)) {
291 if (!parent && lydctx->ext) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200292 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100293 } else {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200294 *snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100295 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200296 if (!*snode) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200298 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_JSON, NULL, name, name_len, snode, ext);
299 if (r != LY_ENOT) {
300 /* success or error */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100301 ret = r;
302 goto cleanup;
303 }
304
305 /* unknown data node */
306 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
307 if (parent) {
308 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
309 (int)name_len, name, LYD_NAME(parent));
310 } else if (lydctx->ext) {
311 if (lydctx->ext->argument) {
312 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
313 "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
314 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
315 } else {
316 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
317 (int)name_len, name, lydctx->ext->def->name);
318 }
319 } else {
320 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
321 (int)name_len, name, mod->name);
322 }
323 ret = LY_EVALID;
324 goto cleanup;
325 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
326 /* skip element with children */
327 ret = lydjson_data_skip(lydctx->jsonctx);
328 LY_CHECK_GOTO(ret, cleanup);
329
330 ret = LY_ENOT;
331 goto cleanup;
332 }
333 } else {
334 /* check that schema node is valid and can be used */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200335 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100336 }
337 }
338
339cleanup:
340 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
341 return ret;
342}
343
344/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200345 * @brief Check that the input data are parseable as the @p list.
346 *
347 * Checks for all the list's keys. Function does not revert the context state.
348 *
349 * @param[in] jsonctx JSON parser context.
350 * @param[in] list List schema node corresponding to the input data object.
351 * @return LY_SUCCESS in case the data are ok for the @p list
352 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
353 */
354static LY_ERR
355lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
356{
357 LY_ERR ret = LY_SUCCESS;
358 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
359 struct ly_set key_set = {0};
360 const struct lysc_node *snode;
361 uint32_t i, status_count;
362
363 assert(list && (list->nodetype == LYS_LIST));
364 assert(status == LYJSON_OBJECT);
365
366 /* get all keys into a set (keys do not have if-features or anything) */
367 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100368 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200369 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200370 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200371 }
372
373 if (status != LYJSON_OBJECT_EMPTY) {
374 status_count = jsonctx->status.count;
375
376 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
377 const char *name, *prefix;
378 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200379 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200380
381 /* match the key */
382 snode = NULL;
383 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
384
385 if (!is_attr && !prefix) {
386 for (i = 0; i < key_set.count; ++i) {
387 snode = (const struct lysc_node *)key_set.objs[i];
388 if (!ly_strncmp(snode->name, name, name_len)) {
389 break;
390 }
391 }
392 /* go into the item to a) process it as a key or b) start skipping it as another list child */
393 ret = lyjson_ctx_next(jsonctx, &status);
394 LY_CHECK_GOTO(ret, cleanup);
395
396 if (snode) {
397 /* we have the key, validate the value */
398 if (status < LYJSON_NUMBER) {
399 /* not a terminal */
400 ret = LY_ENOT;
401 goto cleanup;
402 }
403
Radek Krejci8df109d2021-04-23 12:19:08 +0200404 ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200405 LY_CHECK_GOTO(ret, cleanup);
406
407 /* key with a valid value, remove from the set */
408 ly_set_rm_index(&key_set, i, NULL);
409 }
410 } else {
411 /* start skipping the member we are not interested in */
412 ret = lyjson_ctx_next(jsonctx, &status);
413 LY_CHECK_GOTO(ret, cleanup);
414 }
415 /* move to the next child */
416 while (status_count < jsonctx->status.count) {
417 ret = lyjson_ctx_next(jsonctx, &status);
418 LY_CHECK_GOTO(ret, cleanup);
419 }
420 }
421 }
422
423 if (key_set.count) {
424 /* some keys are missing/did not validate */
425 ret = LY_ENOT;
426 }
427
428cleanup:
429 ly_set_erase(&key_set, NULL);
430 return ret;
431}
432
433/**
434 * @brief Get the hint for the data type parsers according to the current JSON parser context.
435 *
436 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
437 * @param[in,out] status Pointer to the current context status,
438 * in some circumstances the function manipulates with the context so the status is updated.
439 * @param[out] type_hint_p Pointer to the variable to store the result.
440 * @return LY_SUCCESS in case of success.
441 * @return LY_EINVAL in case of invalid context status not referring to a value.
442 */
443static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200444lydjson_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 +0200445{
446 *type_hint_p = 0;
447
448 if (*status_p == LYJSON_ARRAY) {
449 /* only [null] */
450 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
451 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
452
453 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
454 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
455
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200456 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200457 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200458 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200459 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200460 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200461 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200462 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 } else if (*status_p == LYJSON_NULL) {
464 *type_hint_p = 0;
465 } else {
466 return LY_EINVAL;
467 }
468
469 return LY_SUCCESS;
470}
471
472/**
473 * @brief Check in advance if the input data are parsable according to the provided @p snode.
474 *
475 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
476 * is naturally done when the data are really parsed.
477 *
478 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
479 * as before calling, despite it is necessary to process input data for checking.
480 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
481 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
482 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
483 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
484 * @return LY_EINVAL in case of invalid leaf JSON encoding
485 * and they are expected to be parsed as opaq nodes.
486 */
487static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200488lydjson_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 +0200489{
490 LY_ERR ret = LY_SUCCESS;
491 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
492 enum LYJSON_PARSER_STATUS status;
493
494 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200495
Michal Vasko32ac9942020-08-12 14:35:12 +0200496 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
497 /* can always be parsed as a data node if we have the schema node */
498 return LY_SUCCESS;
499 }
500
Michal Vasko85430702021-07-21 14:29:56 +0200501 /* backup parser */
502 lyjson_ctx_backup(jsonctx);
503 status = lyjson_ctx_status(jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200504
Michal Vasko85430702021-07-21 14:29:56 +0200505 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200506 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
507 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200508 switch (snode->nodetype) {
509 case LYS_LEAFLIST:
510 case LYS_LEAF:
511 /* value may not be valid in which case we parse it as an opaque node */
512 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
513 if (ret) {
514 break;
515 }
516
Radek Krejci8df109d2021-04-23 12:19:08 +0200517 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200518 ret = LY_ENOT;
519 }
520 break;
521 case LYS_LIST:
522 /* lists may not have all its keys */
523 if (lydjson_check_list(jsonctx, snode)) {
524 /* invalid list, parse as opaque if it missing/has invalid some keys */
525 ret = LY_ENOT;
526 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200527 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200528 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200529 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200530 status = lyjson_ctx_status(jsonctx, 0);
531 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
532 }
533
Michal Vasko85430702021-07-21 14:29:56 +0200534 /* restore parser */
535 lyjson_ctx_restore(jsonctx);
536
Radek Krejci1798aae2020-07-14 13:26:06 +0200537 return ret;
538}
539
540/**
541 * @brief Join the forward-referencing metadata with their target data nodes.
542 *
543 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
544 *
545 * @param[in] lydctx JSON data parser context.
546 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
547 * as a starting point to search for the metadata's target data node
548 * @return LY_SUCCESS on success
549 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
550 */
551static LY_ERR
552lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
553{
554 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200555 struct lyd_node *node, *attr, *next, *meta_iter;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200556 struct lysc_ext_instance *ext;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200557 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200558 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100559 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200560
561 /* finish linking metadata */
562 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200563 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200564 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200565 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200566 const char *name, *prefix;
567 size_t name_len, prefix_len;
568 const struct lysc_node *snode;
569
Michal Vaskoad92b672020-11-12 13:11:31 +0100570 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200571 /* not an opaq metadata node */
572 continue;
573 }
574
Radek Krejciddace2c2021-01-08 11:30:56 +0100575 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100576 log_location_items++;
577
Michal Vaskoad92b672020-11-12 13:11:31 +0100578 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200579 /* metas' names are stored in dictionary, so checking pointers must works */
580 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100581 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200582 instance = 1;
583 } else {
584 instance++;
585 }
586
aPiecek5057c2e2021-05-17 10:28:03 +0200587 /* find the corresponding data node */
588 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200589 if (!node->schema) {
590 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100591 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200592 continue;
593 }
594
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200595 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100596 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
597 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200598 ret = LY_EVALID;
599 goto cleanup;
600 }
601
602 /* match */
603 match++;
604 if (match != instance) {
605 continue;
606 }
607
608 LY_LIST_FOR(meta_container->child, meta_iter) {
609 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200610 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100611
Michal Vaskoad92b672020-11-12 13:11:31 +0100612 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
613 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200614 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100615 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200616 LY_CHECK_GOTO(ret, cleanup);
617 }
618
619 /* done */
620 break;
621 } else {
622 /* this is the second time we are resolving the schema node, so it must succeed,
623 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100624 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
625 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200626 assert(is_attr);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200627 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 +0200628
629 if (snode != node->schema) {
630 continue;
631 }
632
633 /* match */
634 match++;
635 if (match != instance) {
636 continue;
637 }
638
639 LY_LIST_FOR(meta_container->child, meta_iter) {
640 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200641 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200642 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200643
Michal Vaskoad92b672020-11-12 13:11:31 +0100644 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200645 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200646 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100647 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vaskoddd76592022-01-17 13:34:48 +0100648 NULL, LY_VALUE_JSON, NULL, meta->hints, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200649 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100650 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200651 if (meta->name.prefix) {
652 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
653 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
654 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
655 meta->name.name);
656 } else {
657 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
658 meta->name.name);
659 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200660 ret = LY_EVALID;
661 goto cleanup;
662 }
663 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200664
Michal Vasko8cc3f662022-03-29 11:25:51 +0200665 /* add/correct flags */
666 ret = lyd_parse_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, ext);
667 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200668 break;
669 }
670 }
671
672 if (match != instance) {
673 /* there is no corresponding data node for the metadata */
674 if (instance > 1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100675 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
676 "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
Michal Vasko54ba8912021-07-23 12:46:23 +0200677 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200678 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100679 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
680 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200681 }
682 ret = LY_EVALID;
683 } else {
684 /* remove the opaq attr */
685 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200686 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200687 }
688 lyd_free_tree(attr);
689 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100690
Radek Krejciddace2c2021-01-08 11:30:56 +0100691 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100692 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200693 }
694
695cleanup:
696 lydict_remove(lydctx->jsonctx->ctx, prev);
697
Radek Krejciddace2c2021-01-08 11:30:56 +0100698 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200699 return ret;
700}
701
702/**
703 * @brief Parse a metadata member.
704 *
705 * @param[in] lydctx JSON data parser context.
706 * @param[in] snode Schema node of the metadata parent.
707 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
708 * so the data node does not exists. In such a case the metadata is stored in the context for the later
709 * processing by lydjson_metadata_finish().
710 * @return LY_SUCCESS on success
711 * @return Various LY_ERR values in case of failure.
712 */
713static LY_ERR
714lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
715{
716 LY_ERR ret = LY_SUCCESS;
717 enum LYJSON_PARSER_STATUS status;
718 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200719 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200720 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200721 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200722 size_t name_len, prefix_len = 0;
723 struct lys_module *mod;
724 struct lyd_meta *meta = NULL;
725 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200726 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200727 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200728 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200729 uint16_t nodetype;
730
731 assert(snode || node);
732
733 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
734
735 /* move to the second item in the name/X pair */
736 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
737 LY_CHECK_GOTO(ret, cleanup);
738
739 /* check attribute encoding */
740 switch (nodetype) {
741 case LYS_LEAFLIST:
742 expected = "@name/array of objects/nulls";
743
744 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
745
746next_entry:
747 instance++;
748
749 /* move into array / next entry */
750 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
751 LY_CHECK_GOTO(ret, cleanup);
752
753 if (status == LYJSON_ARRAY_CLOSED) {
754 /* we are done, move after the array */
755 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
756 goto cleanup;
757 }
758 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
759
Michal Vasko69730152020-10-09 16:30:07 +0200760 if (!node || (node->schema != prev->schema)) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200761 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 +0200762 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200763 ret = LY_EVALID;
764 goto cleanup;
765 }
766
767 if (status == LYJSON_NULL) {
768 /* continue with the next entry in the leaf-list array */
769 prev = node;
770 node = node->next;
771 goto next_entry;
772 }
773 break;
774 case LYS_LEAF:
775 case LYS_ANYXML:
776 expected = "@name/object";
777
aPiecek5b115fc2021-05-24 14:32:42 +0200778 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200779 break;
780 case LYS_CONTAINER:
781 case LYS_LIST:
782 case LYS_ANYDATA:
783 case LYS_NOTIF:
784 case LYS_ACTION:
785 case LYS_RPC:
786 in_parent = 1;
787 expected = "@/object";
788 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
789 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200790 default:
791 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200792 }
793
794 /* process all the members inside a single metadata object */
795 assert(status == LYJSON_OBJECT);
796
Radek Krejciddace2c2021-01-08 11:30:56 +0100797 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100798
Radek Krejci1798aae2020-07-14 13:26:06 +0200799 while (status != LYJSON_OBJECT_CLOSED) {
Michal Vaskoebcd9a92022-05-23 08:06:34 +0200800 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
801
aPiecekd2c39372021-06-16 14:03:12 +0200802 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 +0100803 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200804
Michal Vasko69d0ca12021-07-23 10:22:03 +0200805 if (!name_len) {
806 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
807 ret = LY_EVALID;
808 goto cleanup;
809 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100810 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100811 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200812 ret = LY_EVALID;
813 goto cleanup;
814 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100815 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100816 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200817 ret = LY_EVALID;
818 goto cleanup;
819 }
820
821 /* get the element module */
822 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
823 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100824 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100825 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 +0100826 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200827 ret = LY_EVALID;
828 goto cleanup;
829 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100830 if (node->schema) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200831 /* skip element with children */
832 ret = lydjson_data_skip(lydctx->jsonctx);
833 LY_CHECK_GOTO(ret, cleanup);
834 status = lyjson_ctx_status(lydctx->jsonctx, 0);
835 /* end of the item */
836 continue;
837 }
Michal Vaskoa556f162022-01-17 14:08:30 +0100838 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 }
840
841 /* get the value */
842 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
843 LY_CHECK_GOTO(ret, cleanup);
844
845 if (node->schema) {
846 /* create metadata */
847 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200848 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 +0100849 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema);
Radek Krejci1798aae2020-07-14 13:26:06 +0200850 LY_CHECK_GOTO(ret, cleanup);
851
852 /* add/correct flags */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200853 ret = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, NULL);
854 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200855 } else {
856 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200857 const char *module_name;
858 size_t module_name_len;
859
860 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
861
Radek Krejci1798aae2020-07-14 13:26:06 +0200862 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100863 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
864 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200865 LY_VALUE_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200866 LY_CHECK_GOTO(ret, cleanup);
867 }
868 /* next member */
869 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
870 LY_CHECK_GOTO(ret, cleanup);
aPiecekde3c6922021-05-24 14:42:10 +0200871 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200872 }
873
874 if (nodetype == LYS_LEAFLIST) {
875 /* continue by processing another metadata object for the following
876 * leaf-list instance since they are allways instantiated in JSON array */
877 prev = node;
878 node = node->next;
879 goto next_entry;
880 }
881
Radek Krejci5813c362021-01-05 10:51:57 +0100882 /* success */
883 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200884
885representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100886 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200887 "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 +0100888 lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
889 in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200890
Radek Krejci2efc45b2020-12-22 16:25:44 +0100891 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100892
893cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200894 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100895 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100896 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200897}
898
899/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200900 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
901 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200902 *
903 * @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 +0200904 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
905 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
906 * @param[in] last If set, always insert at the end.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200907 * @param[in] ext Extension instance of @p node_p, if any.
Radek Krejci1798aae2020-07-14 13:26:06 +0200908 */
909static void
Michal Vasko8cc3f662022-03-29 11:25:51 +0200910lydjson_maintain_children(struct lyd_node *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last,
911 struct lysc_ext_instance *ext)
Radek Krejci1798aae2020-07-14 13:26:06 +0200912{
913 if (*node_p) {
914 /* insert, keep first pointer correct */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200915 if (ext) {
916 lyd_insert_ext(parent, *node_p);
917 } else {
918 lyd_insert_node(parent, first_p, *node_p, last);
919 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200920 if (first_p) {
921 if (parent) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100922 *first_p = lyd_child(parent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200923 } else {
924 while ((*first_p)->prev->next) {
925 *first_p = (*first_p)->prev;
926 }
927 }
928 }
929 *node_p = NULL;
930 }
931}
932
aPiecek4bf47212021-05-27 10:55:47 +0200933/**
934 * @brief Wrapper for ::lyd_create_opaq().
935 *
936 * @param[in] lydctx JSON data parser context.
937 * @param[in] name Name of the opaq node to create.
938 * @param[in] name_len Length of the @p name string.
939 * @param[in] prefix Prefix of the opaq node to create.
940 * @param[in] prefix_len Length of the @p prefx string.
941 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
942 * but must be set if @p first is not.
943 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
944 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
945 * @param[out] node_p Pointer to the created opaq node.
946 * @return LY_ERR value.
947 */
948static LY_ERR
949lydjson_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 +0100950 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
aPiecek4bf47212021-05-27 10:55:47 +0200951{
952 LY_ERR ret = LY_SUCCESS;
953 const char *value = NULL, *module_name;
954 size_t value_len = 0, module_name_len = 0;
955 ly_bool dynamic = 0;
956 uint32_t type_hint = 0;
957
958 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
959 /* prepare for creating opaq node with a value */
960 value = lydctx->jsonctx->value;
961 value_len = lydctx->jsonctx->value_len;
962 dynamic = lydctx->jsonctx->dynamic;
963 lydctx->jsonctx->dynamic = 0;
964
965 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
966 }
967
968 /* create node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100969 lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +0200970 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
971 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
972 if (dynamic) {
973 free((char *)value);
974 }
975
976 return ret;
977}
978
Michal Vaskoe0665742021-02-11 11:08:44 +0100979static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
980 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200981
982/**
983 * @brief Parse opaq node from the input.
984 *
985 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
986 *
987 * @param[in] lydctx JSON data parser context.
988 * @param[in] name Name of the opaq node to create.
989 * @param[in] name_len Length of the @p name string.
990 * @param[in] prefix Prefix of the opaq node to create.
991 * @param[in] prefix_len Length of the @p prefx string.
992 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
993 * but must be set if @p first is not.
994 * @param[in,out] status_p Pointer to the current status of the parser context,
995 * since the function manipulates with the context and process the input, the status can be updated.
996 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
997 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
998 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
999 * @param[out] node_p Pointer to the created opaq node.
1000 * @return LY_ERR value.
1001 */
1002static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +02001003lydjson_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 +01001004 struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
1005 struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001006{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001007 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 +02001008
Michal Vasko1a85d332021-08-27 10:35:28 +02001009 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
1010 /* special array null value */
1011 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
1012
1013 /* must be the only item */
1014 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
1015 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
1016 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
1017 return LY_EVALID;
1018 }
1019
1020 goto finish;
1021 }
1022
aPiecekdbb8baf2021-05-27 11:01:51 +02001023 while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001024 /* process another instance of the same node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001025
Michal Vasko69730152020-10-09 16:30:07 +02001026 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Michal Vasko1a85d332021-08-27 10:35:28 +02001027 /* array with objects, list */
1028 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
1029
Radek Krejci1798aae2020-07-14 13:26:06 +02001030 /* but first process children of the object in the array */
Michal Vasko1a85d332021-08-27 10:35:28 +02001031 while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001032 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +02001033 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
1034 }
Michal Vasko1a85d332021-08-27 10:35:28 +02001035 } else {
1036 /* array with values, leaf-list */
1037 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001038 }
1039
1040 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001041 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1042 goto finish;
1043 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001044
1045 /* continue with the next instance */
aPiecekdbb8baf2021-05-27 11:01:51 +02001046 assert(node_p);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001047 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001048 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 +02001049 }
1050
1051 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
1052 /* process children */
1053 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
1054 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
1055 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001056 }
1057 }
1058
aPiecekdbb8baf2021-05-27 11:01:51 +02001059finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001060 /* finish linking metadata */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001061 return lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p));
Radek Krejci1798aae2020-07-14 13:26:06 +02001062}
1063
1064/**
aPiecek6cee5d02021-05-12 10:32:00 +02001065 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1066 *
1067 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1068 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1069 * for ::lydjson_parse_opaq().
1070 *
1071 * @param[in] lydctx JSON data parser context.
1072 * @param[in] name Name of the opaq node to create.
1073 * @param[in] name_len Length of the @p name string.
1074 * @param[in] prefix Prefix of the opaq node to create.
1075 * @param[in] prefix_len Length of the @p prefx string.
1076 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1077 * but must be set if @p first is not.
1078 * @param[in,out] status_p Pointer to the current status of the parser context,
1079 * since the function manipulates with the context and process the input, the status can be updated.
1080 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1081 * @param[out] node_p Pointer to the created opaq node.
1082 * @return LY_ERR value.
1083 */
1084static LY_ERR
1085lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001086 const char *prefix, size_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
aPiecek6cee5d02021-05-12 10:32:00 +02001087 struct lyd_node **first_p, struct lyd_node **node_p)
1088{
1089 enum LYJSON_PARSER_STATUS status_inner = 0;
1090
1091 /* move to the second item in the name/X pair */
1092 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1093
1094 if (*status_p == LYJSON_ARRAY) {
1095 /* move into the array */
1096 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1097 } else {
1098 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1099 status_inner = LYJSON_ERROR;
1100 }
1101
1102 if (status_inner == LYJSON_ERROR) {
1103 status_inner = *status_p;
1104 }
1105
1106 /* parse opaq node from the input */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001107 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
1108 first_p, node_p));
aPiecek6cee5d02021-05-12 10:32:00 +02001109
1110 return LY_SUCCESS;
1111}
1112
1113/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001114 * @brief Process the attribute container (starting by @)
1115 *
1116 * @param[in] lydctx JSON data parser context.
1117 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1118 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1119 * @param[in] name Name of the opaq node to create.
1120 * @param[in] name_len Length of the @p name string.
1121 * @param[in] prefix Prefix of the opaq node to create.
1122 * @param[in] prefix_len Length of the @p prefx string.
1123 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1124 * but must be set if @p first is not.
1125 * @param[in,out] status_p Pointer to the current status of the parser context,
1126 * since the function manipulates with the context and process the input, the status can be updated.
1127 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1128 * @param[out] node_p Pointer to the created opaq node.
1129 * @return LY_ERR value.
1130 */
1131static LY_ERR
1132lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001133 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1134 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001135{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001136 LY_ERR r;
1137 const char *opaq_name;
1138 size_t opaq_name_len;
Radek Krejci1798aae2020-07-14 13:26:06 +02001139
1140 /* parse as an attribute to a node */
1141 if (!attr_node && snode) {
1142 /* try to find the instance */
1143 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1144 if (iter->schema == snode) {
1145 attr_node = iter;
1146 break;
1147 }
1148 }
1149 }
1150 if (!attr_node) {
1151 /* parse just as an opaq node with the name beginning with @,
1152 * later we have to check that it belongs to a standard node
1153 * and it is supposed to be converted to a metadata */
1154 uint32_t prev_opts;
1155
Radek Krejci1798aae2020-07-14 13:26:06 +02001156 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001157 prev_opts = lydctx->parse_opts;
1158 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1159 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001160
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001161 opaq_name = prefix ? prefix - 1 : name - 1;
1162 opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
1163 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 +02001164
1165 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001166 lydctx->parse_opts = prev_opts;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001167 LY_CHECK_RET(r);
Radek Krejci1798aae2020-07-14 13:26:06 +02001168 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001169 LY_CHECK_RET(lydjson_metadata(lydctx, snode, attr_node));
Radek Krejci1798aae2020-07-14 13:26:06 +02001170 }
1171
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001172 return LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001173}
1174
1175/**
Michal Vasko76096ec2022-02-24 16:06:16 +01001176 * @brief Parse a single anydata/anyxml node.
1177 *
1178 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1179 * as before calling, despite it is necessary to process input data for checking.
1180 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001181 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko76096ec2022-02-24 16:06:16 +01001182 * @param[in,out] status JSON parser status, is updated.
1183 * @param[out] node Parsed data (or opaque) node.
1184 * @return LY_SUCCESS if a node was successfully parsed,
1185 * @return LY_ENOT in case of invalid JSON encoding,
1186 * @return LY_ERR on other errors.
1187 */
1188static LY_ERR
Michal Vasko8cc3f662022-03-29 11:25:51 +02001189lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lysc_ext_instance *ext,
1190 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko76096ec2022-02-24 16:06:16 +01001191{
1192 LY_ERR r;
1193 uint32_t prev_parse_opts, prev_int_opts;
1194 struct ly_in in_start;
1195 char *val;
1196 struct lyd_node *tree = NULL;
1197
1198 assert(snode->nodetype & LYD_NODE_ANY);
1199
1200 /* status check according to allowed JSON types */
1201 if (snode->nodetype == LYS_ANYXML) {
1202 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY) &&
1203 (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) && (*status != LYJSON_FALSE) &&
1204 (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
1205 } else {
1206 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY) && (*status != LYJSON_ARRAY), LY_ENOT);
1207 }
1208
1209 if ((snode->nodetype == LYS_ANYDATA) && (*status == LYJSON_ARRAY)) {
1210 /* only special anydata [null] allowed, 2 more moves are needed */
1211 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1212 LY_CHECK_RET(*status != LYJSON_NULL, LY_ENOT);
1213 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1214 LY_CHECK_RET(*status != LYJSON_ARRAY_CLOSED, LY_ENOT);
1215
1216 return lyd_create_any(snode, "[null]", LYD_ANYDATA_JSON, 0, node);
1217 }
1218
1219 /* create any node */
1220 switch (*status) {
1221 case LYJSON_OBJECT:
1222 /* parse any data tree with correct options, first backup the current options and then make the parser
1223 * process data as opaq nodes */
1224 prev_parse_opts = lydctx->parse_opts;
1225 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001226 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
Michal Vasko76096ec2022-02-24 16:06:16 +01001227 prev_int_opts = lydctx->int_opts;
1228 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
1229
1230 /* process the anydata content */
1231 while (*status != LYJSON_OBJECT_CLOSED) {
1232 LY_CHECK_RET(lydjson_subtree_r(lydctx, NULL, &tree, NULL));
1233 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1234 }
1235
1236 /* restore parser options */
1237 lydctx->parse_opts = prev_parse_opts;
1238 lydctx->int_opts = prev_int_opts;
1239
1240 /* finish linking metadata */
1241 LY_CHECK_RET(lydjson_metadata_finish(lydctx, &tree));
1242
1243 LY_CHECK_RET(lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node));
1244 break;
1245 case LYJSON_ARRAY:
1246 /* skip until the array end */
1247 in_start = *lydctx->jsonctx->in;
1248 LY_CHECK_RET(lydjson_data_skip(lydctx->jsonctx));
1249
1250 /* make a copy of the whole array and store it */
1251 if (asprintf(&val, "[%.*s", (int)(lydctx->jsonctx->in->current - in_start.current), in_start.current) == -1) {
1252 LOGMEM(lydctx->jsonctx->ctx);
1253 return LY_EMEM;
1254 }
1255 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1256 if (r) {
1257 free(val);
1258 return r;
1259 }
1260 break;
1261 case LYJSON_STRING:
1262 /* string value */
1263 if (lydctx->jsonctx->dynamic) {
1264 LY_CHECK_RET(lyd_create_any(snode, lydctx->jsonctx->value, LYD_ANYDATA_STRING, 1, node));
1265 lydctx->jsonctx->dynamic = 0;
1266 } else {
1267 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1268 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1269
1270 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
1271 if (r) {
1272 free(val);
1273 return r;
1274 }
1275 }
1276 break;
1277 case LYJSON_NUMBER:
1278 case LYJSON_FALSE:
1279 case LYJSON_TRUE:
1280 /* JSON value */
1281 assert(!lydctx->jsonctx->dynamic);
1282 val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
1283 LY_CHECK_ERR_RET(!val, LOGMEM(lydctx->jsonctx->ctx), LY_EMEM);
1284
1285 r = lyd_create_any(snode, val, LYD_ANYDATA_JSON, 1, node);
1286 if (r) {
1287 free(val);
1288 return r;
1289 }
1290 break;
1291 case LYJSON_NULL:
1292 /* no value */
1293 LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_JSON, 1, node));
1294 break;
1295 case LYJSON_OBJECT_EMPTY:
1296 /* empty object */
1297 LY_CHECK_RET(lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node));
1298 break;
1299 default:
1300 LOGINT_RET(lydctx->jsonctx->ctx);
1301 }
1302
1303 return LY_SUCCESS;
1304}
1305
1306/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001307 * @brief Parse a single instance of a node.
1308 *
1309 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1310 * as before calling, despite it is necessary to process input data for checking.
1311 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1312 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1313 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
Michal Vasko8cc3f662022-03-29 11:25:51 +02001314 * @param[in] ext Extension instance of @p snode, if any.
Michal Vasko32ac9942020-08-12 14:35:12 +02001315 * @param[in] name Parsed JSON node name.
1316 * @param[in] name_len Lenght of @p name.
1317 * @param[in] prefix Parsed JSON node prefix.
1318 * @param[in] prefix_len Length of @p prefix.
1319 * @param[in,out] status JSON parser status, is updated.
1320 * @param[out] node Parsed data (or opaque) node.
1321 * @return LY_SUCCESS if a node was successfully parsed,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001322 * @return LY_ENOT in case of invalid JSON encoding,
Michal Vasko32ac9942020-08-12 14:35:12 +02001323 * @return LY_ERR on other errors.
1324 */
1325static LY_ERR
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001326lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
Michal Vasko8cc3f662022-03-29 11:25:51 +02001327 const struct lysc_node *snode, struct lysc_ext_instance *ext, const char *name, size_t name_len,
1328 const char *prefix, size_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001329{
1330 LY_ERR ret;
Michal Vasko76096ec2022-02-24 16:06:16 +01001331 uint32_t type_hints = 0;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001332 uint32_t prev_parse_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001333
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001334 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001335 if (ret == LY_SUCCESS) {
1336 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1337 if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskobbdadda2022-01-06 11:40:10 +01001338 LY_CHECK_RET((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001339 (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL), LY_ENOT);
Michal Vaskobbdadda2022-01-06 11:40:10 +01001340
Michal Vasko32ac9942020-08-12 14:35:12 +02001341 /* create terminal node */
1342 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001343 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001344 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001345 LY_CHECK_RET(ret);
1346
1347 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001348 if (*status == LYJSON_ARRAY) {
1349 /* only [null], 2 more moves are needed */
1350 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1351 assert(*status == LYJSON_NULL);
1352 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1353 assert(*status == LYJSON_ARRAY_CLOSED);
1354 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001355 } else if (snode->nodetype & LYD_NODE_INNER) {
1356 /* create inner node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001357 LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_OBJECT_EMPTY), LY_ENOT);
Michal Vasko32ac9942020-08-12 14:35:12 +02001358
Michal Vasko76096ec2022-02-24 16:06:16 +01001359 LY_CHECK_RET(lyd_create_inner(snode, node));
Michal Vasko32ac9942020-08-12 14:35:12 +02001360
Radek Krejciddace2c2021-01-08 11:30:56 +01001361 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001362
Michal Vasko8cc3f662022-03-29 11:25:51 +02001363 prev_parse_opts = lydctx->parse_opts;
1364 if (ext) {
1365 /* only parse these extension data and validate afterwards */
1366 lydctx->parse_opts |= LYD_PARSE_ONLY;
1367 }
1368
Michal Vasko32ac9942020-08-12 14:35:12 +02001369 /* process children */
Michal Vaskobbdadda2022-01-06 11:40:10 +01001370 while ((*status != LYJSON_OBJECT_CLOSED) && (*status != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001371 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001372 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001373 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1374 }
1375
Michal Vasko8cc3f662022-03-29 11:25:51 +02001376 /* restore options */
1377 lydctx->parse_opts = prev_parse_opts;
1378
Michal Vasko32ac9942020-08-12 14:35:12 +02001379 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001380 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001381 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001382
1383 if (snode->nodetype == LYS_LIST) {
1384 /* check all keys exist */
1385 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001386 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001387 }
1388
Michal Vaskoe0665742021-02-11 11:08:44 +01001389 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001390 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001391 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001392 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001393
1394 /* add any missing default children */
Michal Vaskoddd76592022-01-17 13:34:48 +01001395 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when,
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001396 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001397 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001398 }
1399
Radek Krejciddace2c2021-01-08 11:30:56 +01001400 LOG_LOCBACK(1, 1, 0, 0);
Michal Vasko76096ec2022-02-24 16:06:16 +01001401 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001402 /* create any node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001403 LY_CHECK_RET(lydjson_parse_any(lydctx, snode, ext, status, node));
Michal Vasko32ac9942020-08-12 14:35:12 +02001404 }
Michal Vaskocaf608d2021-07-23 13:51:23 +02001405
1406 /* add/correct flags */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001407 lyd_parse_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko32ac9942020-08-12 14:35:12 +02001408 } else if (ret == LY_ENOT) {
1409 /* parse it again as an opaq node */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001410 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001411 LY_CHECK_RET(ret);
1412
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001413 if (snode->nodetype == LYS_LIST) {
1414 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1415 } else if (snode->nodetype == LYS_LEAFLIST) {
1416 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001417 }
1418 }
1419
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001420 return LY_SUCCESS;
Michal Vasko32ac9942020-08-12 14:35:12 +02001421}
1422
1423/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001424 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001425 *
1426 * @param[in] lydctx JSON data parser context.
1427 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1428 * @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 +01001429 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001430 * @return LY_ERR value.
1431 */
1432static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001433lydjson_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 +02001434{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001435 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci1798aae2020-07-14 13:26:06 +02001436 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
aPiecek49e2a3a2021-05-13 10:36:46 +02001437 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001438 size_t name_len, prefix_len = 0;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001439 ly_bool is_meta = 0, parse_subtree;
Michal Vaskocabe0702020-08-12 10:14:36 +02001440 const struct lysc_node *snode = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001441 struct lysc_ext_instance *ext;
Michal Vasko32ac9942020-08-12 14:35:12 +02001442 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001443 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001444 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001445
1446 assert(parent || first_p);
1447 assert(status == LYJSON_OBJECT);
1448
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001449 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1450 /* all descendants should be parsed */
1451 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1452
Radek Krejci1798aae2020-07-14 13:26:06 +02001453 /* process the node name */
aPiecekd2c39372021-06-16 14:03:12 +02001454 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 +01001455 lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001456
Michal Vaskocabe0702020-08-12 10:14:36 +02001457 if (!is_meta || name_len || prefix_len) {
1458 /* get the schema node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001459 r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001460 if (r == LY_ENOT) {
1461 /* data parsed */
Radek Krejci1798aae2020-07-14 13:26:06 +02001462 goto cleanup;
1463 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001464 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001465
Michal Vaskocabe0702020-08-12 10:14:36 +02001466 if (!snode) {
1467 /* we will not be parsing it as metadata */
1468 is_meta = 0;
1469 }
1470 }
1471
1472 if (is_meta) {
1473 /* parse as metadata */
1474 if (!name_len && !prefix_len) {
1475 /* parent's metadata without a name - use the schema from the parent */
1476 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001477 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001478 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001479 ret = LY_EVALID;
1480 goto cleanup;
1481 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001482 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001483 snode = attr_node->schema;
1484 }
1485 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001486 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001487 LY_CHECK_GOTO(ret, cleanup);
1488 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001489 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001490 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001491
aPiecekb7b29e62021-05-11 10:02:43 +02001492 /* opaq node cannot have an empty string as the name. */
1493 if (name_len == 0) {
1494 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string.");
1495 ret = LY_EVALID;
1496 goto cleanup;
1497 }
1498
aPiecek6cee5d02021-05-12 10:32:00 +02001499 /* move to the second item in the name/X pair and parse opaq */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001500 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 +02001501 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001502 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001503 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001504
1505 /* move to the second item in the name/X pair */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001506 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001507
1508 /* first check the expected representation according to the nodetype and then continue with the content */
1509 switch (snode->nodetype) {
1510 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001511 case LYS_LIST:
1512 if (snode->nodetype == LYS_LEAFLIST) {
1513 expected = "name/array of values";
1514 } else {
1515 expected = "name/array of objects";
1516 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001517
Michal Vasko4e26adc2022-03-30 11:01:16 +02001518 if (status == LYJSON_ARRAY_EMPTY) {
1519 /* no instances, skip */
1520 break;
1521 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001522 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1523
1524 /* move into array */
1525 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1526 LY_CHECK_GOTO(ret, cleanup);
1527
Michal Vasko32ac9942020-08-12 14:35:12 +02001528 /* process all the values/objects */
1529 do {
Michal Vasko8cc3f662022-03-29 11:25:51 +02001530 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001531 &status, &node);
1532 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001533 goto representation_error;
1534 } else if (ret) {
1535 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001536 }
Michal Vasko8cc3f662022-03-29 11:25:51 +02001537 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Radek Krejci1798aae2020-07-14 13:26:06 +02001538
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001539 /* move after the item(s) */
1540 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1541 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001542
1543 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001544 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001545 case LYS_CONTAINER:
1546 case LYS_NOTIF:
1547 case LYS_ACTION:
1548 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001549 case LYS_ANYDATA:
1550 case LYS_ANYXML:
Michal Vaskobbdadda2022-01-06 11:40:10 +01001551 if (snode->nodetype & (LYS_LEAF | LYS_ANYXML)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001552 if (status == LYJSON_ARRAY) {
1553 expected = "name/[null]";
1554 } else {
1555 expected = "name/value";
1556 }
1557 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001558 expected = "name/object";
1559 }
1560
Michal Vasko32ac9942020-08-12 14:35:12 +02001561 /* process the value/object */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001562 ret = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
1563 &status, &node);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001564 if (ret == LY_ENOT) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001565 goto representation_error;
1566 } else if (ret) {
1567 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001568 }
1569
Michal Vasko32ac9942020-08-12 14:35:12 +02001570 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001571 /* rememeber the RPC/action/notification */
1572 lydctx->op_node = node;
1573 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001574 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001575 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001576 }
1577
Michal Vasko32ac9942020-08-12 14:35:12 +02001578 /* finally connect the parsed node */
Michal Vasko8cc3f662022-03-29 11:25:51 +02001579 lydjson_maintain_children(parent, first_p, &node, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0, ext);
Michal Vaskoe0665742021-02-11 11:08:44 +01001580
1581 /* rememeber a successfully parsed node */
Michal Vasko4e26adc2022-03-30 11:01:16 +02001582 if (parsed && node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001583 ly_set_add(parsed, node, 1, NULL);
1584 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001585
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001586 if (!parse_subtree) {
1587 /* move after the item(s) */
1588 LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
1589 }
1590
Radek Krejci5813c362021-01-05 10:51:57 +01001591 /* success */
1592 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001593
1594representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001595 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001596 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 +02001597 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001598 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001599 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001600
1601cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001602 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001603 lyd_free_tree(node);
1604 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001605}
1606
1607/**
1608 * @brief Common start of JSON parser processing different types of the input data.
1609 *
1610 * @param[in] ctx libyang context
1611 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001612 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1613 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001614 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001615 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001616 * @return LY_ERR value.
1617 */
1618static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001619lyd_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 +01001620 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001621{
1622 LY_ERR ret = LY_SUCCESS;
1623 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001624 size_t i;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001625 ly_bool subtree;
Radek Krejci1798aae2020-07-14 13:26:06 +02001626
Radek Krejcicd5f6222020-11-12 08:54:13 +01001627 assert(lydctx_p);
1628 assert(status);
1629
Radek Krejci1798aae2020-07-14 13:26:06 +02001630 /* init context */
1631 lydctx = calloc(1, sizeof *lydctx);
1632 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001633 lydctx->parse_opts = parse_opts;
1634 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001635 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001636
Radek Krejci284f31f2020-09-18 15:40:29 +02001637 /* starting top-level */
1638 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001639 if (in->current[i] == '\n') {
1640 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001641 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001642 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001643 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001644
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001645 subtree = (parse_opts & LYD_PARSE_SUBTREE) ? 1 : 0;
1646 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, subtree, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001647 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001648
Radek Krejcibb27df32020-11-13 15:39:16 +01001649 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001650 *lydctx_p = lydctx;
1651 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001652 } else {
1653 /* expecting top-level object */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001654 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.", lyjson_token2str(*status));
Radek Krejcicd5f6222020-11-12 08:54:13 +01001655 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001656 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001657 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001658 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001659}
1660
1661LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001662lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1663 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoddd76592022-01-17 13:34:48 +01001664 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001665{
Michal Vaskoe0665742021-02-11 11:08:44 +01001666 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001667 struct lyd_json_ctx *lydctx = NULL;
1668 enum LYJSON_PARSER_STATUS status;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001669 uint32_t int_opts = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001670
Michal Vaskoe0665742021-02-11 11:08:44 +01001671 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1672 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001673
Radek Krejcicd5f6222020-11-12 08:54:13 +01001674 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001675
Michal Vaskoe0665742021-02-11 11:08:44 +01001676 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001677 case LYD_TYPE_DATA_YANG:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001678 if (!(parse_opts & LYD_PARSE_SUBTREE)) {
1679 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1680 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001681 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001682 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001683 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1684 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001685 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001686 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1687 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001688 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001689 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1690 break;
1691 default:
1692 LOGINT(ctx);
1693 rc = LY_EINT;
1694 goto cleanup;
1695 }
1696 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001697 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001698
1699 /* find the operation node if it exists already */
1700 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1701
Radek Krejci1798aae2020-07-14 13:26:06 +02001702 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001703 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1704 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1705 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001706
1707 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001708
1709 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1710 break;
1711 }
1712 }
1713
1714 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1715 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1716 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1717 rc = LY_EVALID;
1718 goto cleanup;
1719 }
1720 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1721 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1722 rc = LY_EVALID;
1723 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001724 }
1725
1726 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001727 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1728 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001729
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001730 if (parse_opts & LYD_PARSE_SUBTREE) {
1731 /* check for a sibling object */
1732 assert(subtree_sibling);
1733 if (lydctx->jsonctx->in->current[0] == ',') {
1734 *subtree_sibling = 1;
1735
1736 /* move to the next object */
1737 ly_in_skip(lydctx->jsonctx->in, 1);
1738 } else {
1739 *subtree_sibling = 0;
1740 }
1741 }
1742
Radek Krejci1798aae2020-07-14 13:26:06 +02001743cleanup:
1744 /* there should be no unresolved types stored */
Michal Vasko58c8b562021-12-09 09:25:33 +01001745 assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001746 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001747
Michal Vaskoe0665742021-02-11 11:08:44 +01001748 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001749 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001750 } else {
1751 *lydctx_p = (struct lyd_ctx *)lydctx;
1752 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001753 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001754}