blob: e2ed9aedb2a173429a11d9ba3d8a677466b0356b [file] [log] [blame]
Michal Vasko90932a92020-02-12 14:33:03 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON data parser for libyang
5 *
Radek Krejci1798aae2020-07-14 13:26:06 +02006 * Copyright (c) 2020 CESNET, z.s.p.o.
Michal Vasko90932a92020-02-12 14:33:03 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Michal Vasko90932a92020-02-12 14:33:03 +010016
Radek Krejci1798aae2020-07-14 13:26:06 +020017#include <assert.h>
Radek Krejci47fab892020-11-05 17:02:41 +010018#include <stdint.h>
Michal Vasko90932a92020-02-12 14:33:03 +010019#include <stdlib.h>
20#include <string.h>
21
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
Michal Vasko90932a92020-02-12 14:33:03 +010023#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "in_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020026#include "json.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "log.h"
Radek Krejci77114102021-03-10 15:21:57 +010028#include "parser_data.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020029#include "parser_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010030#include "set.h"
31#include "tree.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020032#include "tree_data.h"
33#include "tree_data_internal.h"
34#include "tree_schema.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_schema_internal.h"
Radek Krejci1798aae2020-07-14 13:26:06 +020036#include "validation.h"
Michal Vasko90932a92020-02-12 14:33:03 +010037
38/**
Radek Krejci1798aae2020-07-14 13:26:06 +020039 * @brief Free the JSON data parser context.
40 *
41 * JSON implementation of lyd_ctx_free_clb().
42 */
43static void
44lyd_json_ctx_free(struct lyd_ctx *lydctx)
45{
46 struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
47
48 if (lydctx) {
49 lyd_ctx_free(lydctx);
50 lyjson_ctx_free(ctx->jsonctx);
51 free(ctx);
52 }
53}
54
55/**
aPiecekd2c39372021-06-16 14:03:12 +020056 * @brief Submit the responsibility for releasing the dynamic values to @p dst.
57 *
58 * @param[in] jsonctx JSON context which contains the dynamic value.
59 * @param[in,out] dst Pointer to which the responsibility will be submited.
60 * If the pointer is already pointing to some allocated memory,
61 * it is released beforehand.
62 */
63static void
64lyjson_ctx_submit_dynamic_value(struct lyjson_ctx *jsonctx, char **dst)
65{
66 assert(jsonctx && dst);
67
68 if (!jsonctx->dynamic) {
69 return;
70 }
71
72 if (dst) {
73 free(*dst);
74 }
75 *dst = NULL;
76
77 /* Submit the dynamic value. */
78 *dst = (char *)jsonctx->value;
79
80 /* Responsibility for the release is now passed to @p dst. */
81 jsonctx->dynamic = 0;
82}
83
84/**
Radek Krejci1798aae2020-07-14 13:26:06 +020085 * @brief Parse JSON member-name as [\@][prefix:][name]
86 *
Michal Vaskocabe0702020-08-12 10:14:36 +020087 * \@ - metadata flag, maps to 1 in @p is_meta_p
Radek Krejci1798aae2020-07-14 13:26:06 +020088 * prefix - name of the module of the data node
89 * name - name of the data node
90 *
91 * All the output parameter are mandatory. Function only parse the member-name, all the appropriate checks are up to the caller.
92 *
93 * @param[in] value String to parse
94 * @param[in] value_len Length of the @p str.
95 * @param[out] name_p Pointer to the beginning of the parsed name.
96 * @param[out] name_len_p Pointer to the length of the parsed name.
97 * @param[out] prefix_p Pointer to the beginning of the parsed prefix. If the member-name does not contain prefix, result is NULL.
98 * @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 +020099 * @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 +0200100 */
101static void
Michal Vaskocabe0702020-08-12 10:14:36 +0200102lydjson_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 +0200103 size_t *prefix_len_p, ly_bool *is_meta_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200104{
105 const char *name, *prefix = NULL;
106 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200107 ly_bool is_meta = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200108
109 name = memchr(value, ':', value_len);
110 if (name != NULL) {
111 prefix = value;
112 if (*prefix == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200113 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200114 prefix++;
115 }
116 prefix_len = name - prefix;
117 name++;
Michal Vaskocabe0702020-08-12 10:14:36 +0200118 name_len = value_len - (prefix_len + 1) - is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200119 } else {
120 name = value;
121 if (name[0] == '@') {
Michal Vaskocabe0702020-08-12 10:14:36 +0200122 is_meta = 1;
Radek Krejci1798aae2020-07-14 13:26:06 +0200123 name++;
124 }
Michal Vaskocabe0702020-08-12 10:14:36 +0200125 name_len = value_len - is_meta;
Michal Vasko90932a92020-02-12 14:33:03 +0100126 }
127
Radek Krejci1798aae2020-07-14 13:26:06 +0200128 *name_p = name;
129 *name_len_p = name_len;
130 *prefix_p = prefix;
131 *prefix_len_p = prefix_len;
Michal Vaskocabe0702020-08-12 10:14:36 +0200132 *is_meta_p = is_meta;
Radek Krejci1798aae2020-07-14 13:26:06 +0200133}
134
135/**
136 * @brief Get correct prefix (module_name) inside the @p node.
137 *
138 * @param[in] node Data node to get inherited prefix.
139 * @param[in] local_prefix Local prefix to replace the inherited prefix.
140 * @param[in] local_prefix_len Length of the @p local_prefix string. In case of 0, the inherited prefix is taken.
141 * @param[out] prefix_p Pointer to the resulting prefix string, Note that the result can be NULL in case of no local prefix
142 * and no context @p node to get inherited prefix.
143 * @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
144 * of no local prefix and no context @p node to get inherited prefix.
145 * @return LY_ERR value.
146 */
147static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200148lydjson_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 +0200149 size_t *prefix_len_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200150{
151 struct lyd_node_opaq *onode;
152 const char *module_name = NULL;
153
154 assert(prefix_p && prefix_len_p);
155
156 if (local_prefix_len) {
157 *prefix_p = local_prefix;
158 *prefix_len_p = local_prefix_len;
159 return LY_SUCCESS;
160 }
161
162 *prefix_p = NULL;
163 while (node) {
164 if (node->schema) {
165 *prefix_p = node->schema->module->name;
166 break;
167 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200168 onode = (struct lyd_node_opaq *)node;
Michal Vaskoad92b672020-11-12 13:11:31 +0100169 if (onode->name.module_name) {
170 *prefix_p = onode->name.module_name;
Radek Krejci1798aae2020-07-14 13:26:06 +0200171 break;
Michal Vaskoad92b672020-11-12 13:11:31 +0100172 } else if (onode->name.prefix) {
173 *prefix_p = onode->name.prefix;
Radek Krejci1798aae2020-07-14 13:26:06 +0200174 break;
175 }
Michal Vasko9e685082021-01-29 14:49:09 +0100176 node = lyd_parent(node);
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 }
178 *prefix_len_p = ly_strlen(module_name);
179
180 return LY_SUCCESS;
181}
182
183/**
184 * @brief Get schema node corresponding to the input parameters.
185 *
186 * @param[in] lydctx JSON data parser context.
187 * @param[in] is_attr Flag if the reference to the node is an attribute, for logging only.
188 * @param[in] prefix Requested node's prefix (module name).
189 * @param[in] prefix_len Length of the @p prefix.
190 * @param[in] name Requested node's name.
191 * @param[in] name_len Length of the @p name.
Michal Vasko2552ea32020-12-08 15:32:34 +0100192 * @param[in] parent Parent of the node being processed, can be NULL in case of top-level.
Radek Krejci1798aae2020-07-14 13:26:06 +0200193 * @param[out] snode_p Pointer to the found schema node corresponding to the input parameters.
194 * @return LY_SUCCES on success, note that even in this case the returned value of @p snode_p can be NULL, so the data are expected to be parsed as opaq.
195 * @return LY_EVALID on failure, error message is logged
196 * @return LY_ENOT in case the input data are expected to be skipped
197 */
198static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200199lydjson_get_snode(const struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, size_t prefix_len, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +0200200 size_t name_len, const struct lyd_node_inner *parent, const struct lysc_node **snode_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200201{
Radek Krejci2efc45b2020-12-22 16:25:44 +0100202 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200203 struct lys_module *mod = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100204 uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200205
206 /* init return value */
207 *snode_p = NULL;
208
Michal Vasko9e543402021-06-11 13:55:30 +0200209 LOG_LOCSET(NULL, parent ? &parent->node : NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100210
Radek Krejci1798aae2020-07-14 13:26:06 +0200211 /* get the element module */
Michal Vasko2552ea32020-12-08 15:32:34 +0100212 if (prefix_len) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200213 mod = ly_ctx_get_module_implemented2(lydctx->jsonctx->ctx, prefix, prefix_len);
214 } else if (parent) {
215 if (parent->schema) {
216 mod = parent->schema->module;
217 }
218 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100219 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "Top-level JSON object member \"%.*s\" must be namespace-qualified.",
Radek Krejci422afb12021-03-04 16:38:16 +0100220 (int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100221 ret = LY_EVALID;
222 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200223 }
224 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100225 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci422afb12021-03-04 16:38:16 +0100226 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "No module named \"%.*s\" in the context.", (int)prefix_len, prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100227 ret = LY_EVALID;
228 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200229 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100230 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100231 ret = LY_ENOT;
232 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200233 }
234 }
235
Radek Krejci1798aae2020-07-14 13:26:06 +0200236 /* get the schema node */
Michal Vasko5d54ad72021-07-22 15:36:01 +0200237 if (mod && (!parent || parent->schema)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100238 if (!parent && lydctx->ext) {
Radek Krejciba05eab2021-03-10 13:19:29 +0100239 *snode_p = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
Radek Krejcif16e2542021-02-17 15:39:23 +0100240 } else {
241 *snode_p = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, getnext_opts);
242 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200243 if (!*snode_p) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100244 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100245 if (lydctx->ext) {
246 if (lydctx->ext->argument) {
247 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
Radek Krejci422afb12021-03-04 16:38:16 +0100248 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100249 } else {
250 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
Radek Krejci422afb12021-03-04 16:38:16 +0100251 (int)name_len, name, lydctx->ext->def->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100252 }
253 } else {
254 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
Radek Krejci422afb12021-03-04 16:38:16 +0100255 (int)name_len, name, mod->name);
Radek Krejcif16e2542021-02-17 15:39:23 +0100256 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100257 ret = LY_EVALID;
258 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +0100259 } else if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200260 /* skip element with children */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100261 ret = LY_ENOT;
262 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200263 }
264 } else {
265 /* check that schema node is valid and can be used */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100266 ret = lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200267 }
268 }
269
Radek Krejci2efc45b2020-12-22 16:25:44 +0100270cleanup:
Radek Krejciddace2c2021-01-08 11:30:56 +0100271 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100272 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200273}
274
275/**
276 * @brief Skip the currently open JSON object/array
277 * @param[in] jsonctx JSON context with the input data to skip.
278 * @return LY_ERR value.
279 */
280static LY_ERR
281lydjson_data_skip(struct lyjson_ctx *jsonctx)
282{
283 enum LYJSON_PARSER_STATUS status, current;
284 size_t sublevels = 1;
285
286 status = lyjson_ctx_status(jsonctx, 0);
287
288 /* skip after the content */
289 do {
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200290 uint32_t prev_depth = jsonctx->depth;
291
Radek Krejci1798aae2020-07-14 13:26:06 +0200292 LY_CHECK_RET(lyjson_ctx_next(jsonctx, &current));
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200293
Radek Krejci1798aae2020-07-14 13:26:06 +0200294 if (current == status) {
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200295 /* lyjson_ctx_next() can return LYSJON_OBJECT in two cases, either when
296 * a new object is encountered, or when it finishes parsing a value from a
297 * previous key-value pair. In the latter case the sublevel shouldn't increase.
298 */
299 if ((status == LYJSON_OBJECT) && (prev_depth == jsonctx->depth)) {
300 continue;
301 }
302
Radek Krejci1798aae2020-07-14 13:26:06 +0200303 sublevels++;
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200304 } else if ((status == LYJSON_OBJECT) && (current == LYJSON_OBJECT_CLOSED)) {
Juraj Vijtiuk46eb15e2021-09-06 15:38:36 +0200305 sublevels--;
Juraj Vijtiuk2e883682021-09-20 17:00:56 +0200306 } else if ((status == LYJSON_ARRAY) && (current == LYJSON_ARRAY_CLOSED)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200307 sublevels--;
308 }
Radek Krejci79856c42020-11-19 11:44:08 +0100309 } while (current != status + 1 || sublevels);
Radek Krejci1798aae2020-07-14 13:26:06 +0200310 /* open the next sibling */
311 LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
312
313 return LY_SUCCESS;
314}
315
316/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200317 * @brief Check that the input data are parseable as the @p list.
318 *
319 * Checks for all the list's keys. Function does not revert the context state.
320 *
321 * @param[in] jsonctx JSON parser context.
322 * @param[in] list List schema node corresponding to the input data object.
323 * @return LY_SUCCESS in case the data are ok for the @p list
324 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance.
325 */
326static LY_ERR
327lydjson_check_list(struct lyjson_ctx *jsonctx, const struct lysc_node *list)
328{
329 LY_ERR ret = LY_SUCCESS;
330 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx, 0);
331 struct ly_set key_set = {0};
332 const struct lysc_node *snode;
333 uint32_t i, status_count;
334
335 assert(list && (list->nodetype == LYS_LIST));
336 assert(status == LYJSON_OBJECT);
337
338 /* get all keys into a set (keys do not have if-features or anything) */
339 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100340 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200341 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200342 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200343 }
344
345 if (status != LYJSON_OBJECT_EMPTY) {
346 status_count = jsonctx->status.count;
347
348 while (key_set.count && status != LYJSON_OBJECT_CLOSED) {
349 const char *name, *prefix;
350 size_t name_len, prefix_len;
Radek Krejci857189e2020-09-01 13:26:36 +0200351 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200352
353 /* match the key */
354 snode = NULL;
355 lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
356
357 if (!is_attr && !prefix) {
358 for (i = 0; i < key_set.count; ++i) {
359 snode = (const struct lysc_node *)key_set.objs[i];
360 if (!ly_strncmp(snode->name, name, name_len)) {
361 break;
362 }
363 }
364 /* go into the item to a) process it as a key or b) start skipping it as another list child */
365 ret = lyjson_ctx_next(jsonctx, &status);
366 LY_CHECK_GOTO(ret, cleanup);
367
368 if (snode) {
369 /* we have the key, validate the value */
370 if (status < LYJSON_NUMBER) {
371 /* not a terminal */
372 ret = LY_ENOT;
373 goto cleanup;
374 }
375
Radek Krejci8df109d2021-04-23 12:19:08 +0200376 ret = lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +0200377 LY_CHECK_GOTO(ret, cleanup);
378
379 /* key with a valid value, remove from the set */
380 ly_set_rm_index(&key_set, i, NULL);
381 }
382 } else {
383 /* start skipping the member we are not interested in */
384 ret = lyjson_ctx_next(jsonctx, &status);
385 LY_CHECK_GOTO(ret, cleanup);
386 }
387 /* move to the next child */
388 while (status_count < jsonctx->status.count) {
389 ret = lyjson_ctx_next(jsonctx, &status);
390 LY_CHECK_GOTO(ret, cleanup);
391 }
392 }
393 }
394
395 if (key_set.count) {
396 /* some keys are missing/did not validate */
397 ret = LY_ENOT;
398 }
399
400cleanup:
401 ly_set_erase(&key_set, NULL);
402 return ret;
403}
404
405/**
406 * @brief Get the hint for the data type parsers according to the current JSON parser context.
407 *
408 * @param[in] lydctx JSON data parser context. The context is supposed to be on a value.
409 * @param[in,out] status Pointer to the current context status,
410 * in some circumstances the function manipulates with the context so the status is updated.
411 * @param[out] type_hint_p Pointer to the variable to store the result.
412 * @return LY_SUCCESS in case of success.
413 * @return LY_EINVAL in case of invalid context status not referring to a value.
414 */
415static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200416lydjson_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 +0200417{
418 *type_hint_p = 0;
419
420 if (*status_p == LYJSON_ARRAY) {
421 /* only [null] */
422 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
423 LY_CHECK_RET(*status_p != LYJSON_NULL, LY_EINVAL);
424
425 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, NULL));
426 LY_CHECK_RET(lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_ARRAY_CLOSED, LY_EINVAL);
427
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200428 *type_hint_p = LYD_VALHINT_EMPTY;
Radek Krejci1798aae2020-07-14 13:26:06 +0200429 } else if (*status_p == LYJSON_STRING) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200430 *type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
Radek Krejci1798aae2020-07-14 13:26:06 +0200431 } else if (*status_p == LYJSON_NUMBER) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200432 *type_hint_p = LYD_VALHINT_DECNUM;
Michal Vasko69730152020-10-09 16:30:07 +0200433 } else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200434 *type_hint_p = LYD_VALHINT_BOOLEAN;
Radek Krejci1798aae2020-07-14 13:26:06 +0200435 } else if (*status_p == LYJSON_NULL) {
436 *type_hint_p = 0;
437 } else {
438 return LY_EINVAL;
439 }
440
441 return LY_SUCCESS;
442}
443
444/**
445 * @brief Check in advance if the input data are parsable according to the provided @p snode.
446 *
447 * Note that the checks are done only in case the LYD_PARSE_OPAQ is allowed. Otherwise the same checking
448 * is naturally done when the data are really parsed.
449 *
450 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
451 * as before calling, despite it is necessary to process input data for checking.
452 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
453 * @param[out] type_hint_p Pointer to a variable to store detected value type hint in case of leaf or leaf-list.
454 * @return LY_SUCCESS in case the data are ok for the @p snode or the LYD_PARSE_OPAQ is not enabled.
455 * @return LY_ENOT in case the input data are not sufficient to fully parse the list instance
456 * @return LY_EINVAL in case of invalid leaf JSON encoding
457 * and they are expected to be parsed as opaq nodes.
458 */
459static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200460lydjson_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 +0200461{
462 LY_ERR ret = LY_SUCCESS;
463 struct lyjson_ctx *jsonctx = lydctx->jsonctx;
464 enum LYJSON_PARSER_STATUS status;
465
466 assert(snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200467
Michal Vasko32ac9942020-08-12 14:35:12 +0200468 if (!(snode->nodetype & (LYD_NODE_TERM | LYS_LIST))) {
469 /* can always be parsed as a data node if we have the schema node */
470 return LY_SUCCESS;
471 }
472
Michal Vasko85430702021-07-21 14:29:56 +0200473 /* backup parser */
474 lyjson_ctx_backup(jsonctx);
475 status = lyjson_ctx_status(jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200476
Michal Vasko85430702021-07-21 14:29:56 +0200477 if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
Michal Vasko32ac9942020-08-12 14:35:12 +0200478 /* check if the node is parseable. if not, NULL the snode to announce that it is supposed to be parsed
479 * as an opaq node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200480 switch (snode->nodetype) {
481 case LYS_LEAFLIST:
482 case LYS_LEAF:
483 /* value may not be valid in which case we parse it as an opaque node */
484 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
485 if (ret) {
486 break;
487 }
488
Radek Krejci8df109d2021-04-23 12:19:08 +0200489 if (lys_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len, LY_VALUE_JSON, NULL)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200490 ret = LY_ENOT;
491 }
492 break;
493 case LYS_LIST:
494 /* lists may not have all its keys */
495 if (lydjson_check_list(jsonctx, snode)) {
496 /* invalid list, parse as opaque if it missing/has invalid some keys */
497 ret = LY_ENOT;
498 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200499 break;
Radek Krejci1798aae2020-07-14 13:26:06 +0200500 }
Michal Vasko32ac9942020-08-12 14:35:12 +0200501 } else if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200502 status = lyjson_ctx_status(jsonctx, 0);
503 ret = lydjson_value_type_hint(lydctx, &status, type_hint_p);
504 }
505
Michal Vasko85430702021-07-21 14:29:56 +0200506 /* restore parser */
507 lyjson_ctx_restore(jsonctx);
508
Radek Krejci1798aae2020-07-14 13:26:06 +0200509 return ret;
510}
511
512/**
513 * @brief Join the forward-referencing metadata with their target data nodes.
514 *
515 * Note that JSON encoding for YANG data allows forward-referencing metadata only for leafs/leaf-lists.
516 *
517 * @param[in] lydctx JSON data parser context.
518 * @param[in,out] first_p Pointer to the first sibling node variable (top-level or in a particular parent node)
519 * as a starting point to search for the metadata's target data node
520 * @return LY_SUCCESS on success
521 * @return LY_EVALID in case there are some metadata with unresolved target data node instance
522 */
523static LY_ERR
524lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
525{
526 LY_ERR ret = LY_SUCCESS;
aPiecek5057c2e2021-05-17 10:28:03 +0200527 struct lyd_node *node, *attr, *next, *meta_iter;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200528 uint64_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200529 const char *prev = NULL;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100530 uint32_t log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200531
532 /* finish linking metadata */
533 LY_LIST_FOR_SAFE(*first_p, next, attr) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200534 struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200535 uint64_t match = 0;
Radek Krejci857189e2020-09-01 13:26:36 +0200536 ly_bool is_attr;
Radek Krejci1798aae2020-07-14 13:26:06 +0200537 const char *name, *prefix;
538 size_t name_len, prefix_len;
539 const struct lysc_node *snode;
540
Michal Vaskoad92b672020-11-12 13:11:31 +0100541 if (attr->schema || (meta_container->name.name[0] != '@')) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200542 /* not an opaq metadata node */
543 continue;
544 }
545
Radek Krejciddace2c2021-01-08 11:30:56 +0100546 LOG_LOCSET(NULL, attr, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100547 log_location_items++;
548
Michal Vaskoad92b672020-11-12 13:11:31 +0100549 if (prev != meta_container->name.name) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200550 /* metas' names are stored in dictionary, so checking pointers must works */
551 lydict_remove(lydctx->jsonctx->ctx, prev);
Michal Vaskoad92b672020-11-12 13:11:31 +0100552 LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200553 instance = 1;
554 } else {
555 instance++;
556 }
557
aPiecek5057c2e2021-05-17 10:28:03 +0200558 /* find the corresponding data node */
559 LY_LIST_FOR(*first_p, node) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200560 if (!node->schema) {
561 /* opaq node - we are going to put into it just a generic attribute. */
Michal Vaskoad92b672020-11-12 13:11:31 +0100562 if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200563 continue;
564 }
565
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200566 if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100567 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
568 ((struct lyd_node_opaq *)node)->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200569 ret = LY_EVALID;
570 goto cleanup;
571 }
572
573 /* match */
574 match++;
575 if (match != instance) {
576 continue;
577 }
578
579 LY_LIST_FOR(meta_container->child, meta_iter) {
580 /* convert opaq node to a attribute of the opaq node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200581 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejcibb27df32020-11-13 15:39:16 +0100582
Michal Vaskoad92b672020-11-12 13:11:31 +0100583 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
584 meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200585 ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
Michal Vaskoad92b672020-11-12 13:11:31 +0100586 NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200587 LY_CHECK_GOTO(ret, cleanup);
588 }
589
590 /* done */
591 break;
592 } else {
593 /* this is the second time we are resolving the schema node, so it must succeed,
594 * but remember that snode can be still NULL */
Michal Vaskoad92b672020-11-12 13:11:31 +0100595 lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
596 &prefix, &prefix_len, &is_attr);
Radek Krejci1798aae2020-07-14 13:26:06 +0200597 assert(is_attr);
598 ret = lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode);
599 assert(ret == LY_SUCCESS);
600
601 if (snode != node->schema) {
602 continue;
603 }
604
605 /* match */
606 match++;
607 if (match != instance) {
608 continue;
609 }
610
611 LY_LIST_FOR(meta_container->child, meta_iter) {
612 /* convert opaq node to a metadata of the node */
Michal Vasko22df3f02020-08-24 13:29:22 +0200613 struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
Radek Krejci1798aae2020-07-14 13:26:06 +0200614 struct lys_module *mod = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200615
Michal Vaskoad92b672020-11-12 13:11:31 +0100616 mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200617 if (mod) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200618 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod,
Michal Vaskoad92b672020-11-12 13:11:31 +0100619 meta->name.name, strlen(meta->name.name), meta->value, ly_strlen(meta->value),
Michal Vasko02cf2662021-07-22 15:36:29 +0200620 NULL, LY_VALUE_JSON, NULL, meta->hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200621 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +0100622 } else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko4eab5622021-07-22 15:36:45 +0200623 if (meta->name.prefix) {
624 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE,
625 "Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
626 meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
627 meta->name.name);
628 } else {
629 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
630 meta->name.name);
631 }
Radek Krejci1798aae2020-07-14 13:26:06 +0200632 ret = LY_EVALID;
633 goto cleanup;
634 }
635 }
636 /* add/correct flags */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200637 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &node->meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200638
639 /* done */
640 break;
641 }
642 }
643
644 if (match != instance) {
645 /* there is no corresponding data node for the metadata */
646 if (instance > 1) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200647 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
648 instance, meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200649 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100650 LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
651 meta_container->name.name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200652 }
653 ret = LY_EVALID;
654 } else {
655 /* remove the opaq attr */
656 if (attr == (*first_p)) {
aPiecek5057c2e2021-05-17 10:28:03 +0200657 *first_p = attr->next;
Radek Krejci1798aae2020-07-14 13:26:06 +0200658 }
659 lyd_free_tree(attr);
660 }
Radek Krejci2efc45b2020-12-22 16:25:44 +0100661
Radek Krejciddace2c2021-01-08 11:30:56 +0100662 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100663 log_location_items = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200664 }
665
666cleanup:
667 lydict_remove(lydctx->jsonctx->ctx, prev);
668
Radek Krejciddace2c2021-01-08 11:30:56 +0100669 LOG_LOCBACK(0, log_location_items, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200670 return ret;
671}
672
673/**
674 * @brief Parse a metadata member.
675 *
676 * @param[in] lydctx JSON data parser context.
677 * @param[in] snode Schema node of the metadata parent.
678 * @param[in] node Parent node in case the metadata is not forward-referencing (only LYD_NODE_TERM)
679 * so the data node does not exists. In such a case the metadata is stored in the context for the later
680 * processing by lydjson_metadata_finish().
681 * @return LY_SUCCESS on success
682 * @return Various LY_ERR values in case of failure.
683 */
684static LY_ERR
685lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, struct lyd_node *node)
686{
687 LY_ERR ret = LY_SUCCESS;
688 enum LYJSON_PARSER_STATUS status;
689 const char *expected;
Radek Krejci857189e2020-09-01 13:26:36 +0200690 ly_bool in_parent = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200691 const char *name, *prefix = NULL;
aPiecek303fb252021-06-16 11:54:26 +0200692 char *dynamic_prefname = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200693 size_t name_len, prefix_len = 0;
694 struct lys_module *mod;
695 struct lyd_meta *meta = NULL;
696 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
Radek Krejci857189e2020-09-01 13:26:36 +0200697 ly_bool is_attr = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200698 struct lyd_node *prev = node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200699 uint32_t instance = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +0200700 uint16_t nodetype;
701
702 assert(snode || node);
703
704 nodetype = snode ? snode->nodetype : LYS_CONTAINER;
705
706 /* move to the second item in the name/X pair */
707 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
708 LY_CHECK_GOTO(ret, cleanup);
709
710 /* check attribute encoding */
711 switch (nodetype) {
712 case LYS_LEAFLIST:
713 expected = "@name/array of objects/nulls";
714
715 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
716
717next_entry:
718 instance++;
719
720 /* move into array / next entry */
721 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
722 LY_CHECK_GOTO(ret, cleanup);
723
724 if (status == LYJSON_ARRAY_CLOSED) {
725 /* we are done, move after the array */
726 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
727 goto cleanup;
728 }
729 LY_CHECK_GOTO(status != LYJSON_OBJECT && status != LYJSON_NULL, representation_error);
730
Michal Vasko69730152020-10-09 16:30:07 +0200731 if (!node || (node->schema != prev->schema)) {
Michal Vasko54ba8912021-07-23 12:46:23 +0200732 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 +0200733 instance, prev->schema->module->name, prev->schema->name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200734 ret = LY_EVALID;
735 goto cleanup;
736 }
737
738 if (status == LYJSON_NULL) {
739 /* continue with the next entry in the leaf-list array */
740 prev = node;
741 node = node->next;
742 goto next_entry;
743 }
744 break;
745 case LYS_LEAF:
746 case LYS_ANYXML:
747 expected = "@name/object";
748
aPiecek5b115fc2021-05-24 14:32:42 +0200749 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200750 break;
751 case LYS_CONTAINER:
752 case LYS_LIST:
753 case LYS_ANYDATA:
754 case LYS_NOTIF:
755 case LYS_ACTION:
756 case LYS_RPC:
757 in_parent = 1;
758 expected = "@/object";
759 LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
760 break;
Radek Krejci8f5fad22020-09-15 16:50:54 +0200761 default:
762 LOGINT_RET(ctx);
Radek Krejci1798aae2020-07-14 13:26:06 +0200763 }
764
765 /* process all the members inside a single metadata object */
766 assert(status == LYJSON_OBJECT);
767
Radek Krejciddace2c2021-01-08 11:30:56 +0100768 LOG_LOCSET(snode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100769
Radek Krejci1798aae2020-07-14 13:26:06 +0200770 while (status != LYJSON_OBJECT_CLOSED) {
aPiecekd2c39372021-06-16 14:03:12 +0200771 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
772 lyjson_ctx_submit_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
aPiecek303fb252021-06-16 11:54:26 +0200773
Michal Vasko69d0ca12021-07-23 10:22:03 +0200774 if (!name_len) {
775 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
776 ret = LY_EVALID;
777 goto cleanup;
778 } else if (!prefix_len) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100779 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100780 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200781 ret = LY_EVALID;
782 goto cleanup;
783 } else if (is_attr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100784 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
Radek Krejci422afb12021-03-04 16:38:16 +0100785 (int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
Radek Krejci1798aae2020-07-14 13:26:06 +0200786 ret = LY_EVALID;
787 goto cleanup;
788 }
789
790 /* get the element module */
791 mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
792 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100793 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100794 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 +0100795 (int)prefix_len, prefix, (int)name_len, name);
Radek Krejci1798aae2020-07-14 13:26:06 +0200796 ret = LY_EVALID;
797 goto cleanup;
798 }
Michal Vaskoe0665742021-02-11 11:08:44 +0100799 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200800 /* skip element with children */
801 ret = lydjson_data_skip(lydctx->jsonctx);
802 LY_CHECK_GOTO(ret, cleanup);
803 status = lyjson_ctx_status(lydctx->jsonctx, 0);
804 /* end of the item */
805 continue;
806 }
807 }
808
809 /* get the value */
810 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
811 LY_CHECK_GOTO(ret, cleanup);
812
813 if (node->schema) {
814 /* create metadata */
815 meta = NULL;
Michal Vasko22df3f02020-08-24 13:29:22 +0200816 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, &meta, mod, name, name_len, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200817 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +0200818 LYD_HINT_DATA);
Radek Krejci1798aae2020-07-14 13:26:06 +0200819 LY_CHECK_GOTO(ret, cleanup);
820
821 /* add/correct flags */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200822 lyd_parse_set_data_flags(node, &lydctx->node_when, &lydctx->node_exts, &meta, lydctx->parse_opts);
Radek Krejci1798aae2020-07-14 13:26:06 +0200823 } else {
824 /* create attribute */
Radek Krejci1798aae2020-07-14 13:26:06 +0200825 const char *module_name;
826 size_t module_name_len;
827
828 lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
829
Radek Krejci1798aae2020-07-14 13:26:06 +0200830 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100831 ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
832 module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200833 LY_VALUE_JSON, NULL, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +0200834 LY_CHECK_GOTO(ret, cleanup);
835 }
836 /* next member */
837 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
838 LY_CHECK_GOTO(ret, cleanup);
aPiecekde3c6922021-05-24 14:42:10 +0200839 LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_OBJECT_CLOSED), representation_error);
Radek Krejci1798aae2020-07-14 13:26:06 +0200840 }
841
842 if (nodetype == LYS_LEAFLIST) {
843 /* continue by processing another metadata object for the following
844 * leaf-list instance since they are allways instantiated in JSON array */
845 prev = node;
846 node = node->next;
847 goto next_entry;
848 }
849
850 /* move after the metadata */
851 ret = lyjson_ctx_next(lydctx->jsonctx, NULL);
852 LY_CHECK_GOTO(ret, cleanup);
853
Radek Krejci5813c362021-01-05 10:51:57 +0100854 /* success */
855 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +0200856
857representation_error:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100858 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200859 "The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
Michal Vaskoad92b672020-11-12 13:11:31 +0100860 lys_nodetype2str(nodetype), node->schema ? node->schema->name : ((struct lyd_node_opaq *)node)->name.name,
Michal Vasko69730152020-10-09 16:30:07 +0200861 expected, lyjson_token2str(status), in_parent ? "" : "name");
Radek Krejci1798aae2020-07-14 13:26:06 +0200862
Radek Krejci2efc45b2020-12-22 16:25:44 +0100863 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +0100864
865cleanup:
aPiecek303fb252021-06-16 11:54:26 +0200866 free(dynamic_prefname);
Radek Krejciddace2c2021-01-08 11:30:56 +0100867 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci5813c362021-01-05 10:51:57 +0100868 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +0200869}
870
871/**
Michal Vasko6ee6f432021-07-16 09:49:14 +0200872 * @brief Eat the node pointed by @p node_p by inserting it into @p parent and maintain the @p first_p pointing
873 * to the first child node.
Radek Krejci1798aae2020-07-14 13:26:06 +0200874 *
875 * @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 +0200876 * @param[in,out] first_p Pointer to the first sibling node in case of top-level.
877 * @param[in,out] node_p pointer to the new node to insert, after the insert is done, pointer is set to NULL.
878 * @param[in] last If set, always insert at the end.
Radek Krejci1798aae2020-07-14 13:26:06 +0200879 */
880static void
Michal Vasko6ee6f432021-07-16 09:49:14 +0200881lydjson_maintain_children(struct lyd_node_inner *parent, struct lyd_node **first_p, struct lyd_node **node_p, ly_bool last)
Radek Krejci1798aae2020-07-14 13:26:06 +0200882{
883 if (*node_p) {
884 /* insert, keep first pointer correct */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200885 lyd_insert_node(parent ? &parent->node : NULL, first_p, *node_p, last);
Radek Krejci1798aae2020-07-14 13:26:06 +0200886 if (first_p) {
887 if (parent) {
888 *first_p = parent->child;
889 } else {
890 while ((*first_p)->prev->next) {
891 *first_p = (*first_p)->prev;
892 }
893 }
894 }
895 *node_p = NULL;
896 }
897}
898
aPiecek4bf47212021-05-27 10:55:47 +0200899/**
900 * @brief Wrapper for ::lyd_create_opaq().
901 *
902 * @param[in] lydctx JSON data parser context.
903 * @param[in] name Name of the opaq node to create.
904 * @param[in] name_len Length of the @p name string.
905 * @param[in] prefix Prefix of the opaq node to create.
906 * @param[in] prefix_len Length of the @p prefx string.
907 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
908 * but must be set if @p first is not.
909 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
910 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
911 * @param[out] node_p Pointer to the created opaq node.
912 * @return LY_ERR value.
913 */
914static LY_ERR
915lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
916 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
917{
918 LY_ERR ret = LY_SUCCESS;
919 const char *value = NULL, *module_name;
920 size_t value_len = 0, module_name_len = 0;
921 ly_bool dynamic = 0;
922 uint32_t type_hint = 0;
923
924 if ((*status_inner_p != LYJSON_OBJECT) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
925 /* prepare for creating opaq node with a value */
926 value = lydctx->jsonctx->value;
927 value_len = lydctx->jsonctx->value_len;
928 dynamic = lydctx->jsonctx->dynamic;
929 lydctx->jsonctx->dynamic = 0;
930
931 LY_CHECK_RET(lydjson_value_type_hint(lydctx, status_inner_p, &type_hint));
932 }
933
934 /* create node */
Michal Vaskod8e774e2021-06-15 14:58:07 +0200935 lydjson_get_node_prefix(parent ? &parent->node : NULL, prefix, prefix_len, &module_name, &module_name_len);
aPiecek4bf47212021-05-27 10:55:47 +0200936 ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
937 value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
938 if (dynamic) {
939 free((char *)value);
940 }
941
942 return ret;
943}
944
Michal Vaskoe0665742021-02-11 11:08:44 +0100945static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
946 struct ly_set *parsed);
Radek Krejci1798aae2020-07-14 13:26:06 +0200947
948/**
949 * @brief Parse opaq node from the input.
950 *
951 * In case of processing array, the whole array is being processed and the resulting @p node_p is the last item of the array.
952 *
953 * @param[in] lydctx JSON data parser context.
954 * @param[in] name Name of the opaq node to create.
955 * @param[in] name_len Length of the @p name string.
956 * @param[in] prefix Prefix of the opaq node to create.
957 * @param[in] prefix_len Length of the @p prefx string.
958 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
959 * but must be set if @p first is not.
960 * @param[in,out] status_p Pointer to the current status of the parser context,
961 * since the function manipulates with the context and process the input, the status can be updated.
962 * @param[in,out] status_inner_p In case of processing JSON array, this parameter points to a standalone
963 * context status of the array content. Otherwise, it is supposed to be the same as @p status_p.
964 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
965 * @param[out] node_p Pointer to the created opaq node.
966 * @return LY_ERR value.
967 */
968static LY_ERR
Michal Vaskoa5da3292020-08-12 13:10:50 +0200969lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200970 struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
971 enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +0200972{
973 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +0200974
aPiecek4bf47212021-05-27 10:55:47 +0200975 ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p);
Radek Krejcid46e46a2020-09-15 14:22:42 +0200976 LY_CHECK_RET(ret);
Radek Krejci1798aae2020-07-14 13:26:06 +0200977
Michal Vasko1a85d332021-08-27 10:35:28 +0200978 if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
979 /* special array null value */
980 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_VALHINT_EMPTY;
981
982 /* must be the only item */
983 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
984 if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
985 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX, "Array \"null\" member with another member.");
986 return LY_EVALID;
987 }
988
989 goto finish;
990 }
991
aPiecekdbb8baf2021-05-27 11:01:51 +0200992 while ((*status_p == LYJSON_ARRAY) || (*status_p == LYJSON_ARRAY_EMPTY)) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200993 /* process another instance of the same node */
Radek Krejci1798aae2020-07-14 13:26:06 +0200994
Michal Vasko69730152020-10-09 16:30:07 +0200995 if ((*status_inner_p == LYJSON_OBJECT) || (*status_inner_p == LYJSON_OBJECT_EMPTY)) {
Michal Vasko1a85d332021-08-27 10:35:28 +0200996 /* array with objects, list */
997 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LIST;
998
Radek Krejci1798aae2020-07-14 13:26:06 +0200999 /* but first process children of the object in the array */
Michal Vasko1a85d332021-08-27 10:35:28 +02001000 while ((*status_inner_p != LYJSON_OBJECT_CLOSED) && (*status_inner_p != LYJSON_OBJECT_EMPTY)) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001001 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
Radek Krejci1798aae2020-07-14 13:26:06 +02001002 *status_inner_p = lyjson_ctx_status(lydctx->jsonctx, 0);
1003 }
Michal Vasko1a85d332021-08-27 10:35:28 +02001004 } else {
1005 /* array with values, leaf-list */
1006 ((struct lyd_node_opaq *)*node_p)->hints |= LYD_NODEHINT_LEAFLIST;
Radek Krejci1798aae2020-07-14 13:26:06 +02001007 }
1008
1009 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_inner_p));
aPiecekdbb8baf2021-05-27 11:01:51 +02001010 if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
1011 goto finish;
1012 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001013
1014 /* continue with the next instance */
aPiecekdbb8baf2021-05-27 11:01:51 +02001015 assert(node_p);
Michal Vasko6ee6f432021-07-16 09:49:14 +02001016 lydjson_maintain_children(parent, first_p, node_p, lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
aPiecekdbb8baf2021-05-27 11:01:51 +02001017 ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node_p);
1018 LY_CHECK_RET(ret);
1019 }
1020
1021 if ((*status_p == LYJSON_OBJECT) || (*status_p == LYJSON_OBJECT_EMPTY)) {
1022 /* process children */
1023 while (*status_p != LYJSON_OBJECT_CLOSED && *status_p != LYJSON_OBJECT_EMPTY) {
1024 LY_CHECK_RET(lydjson_subtree_r(lydctx, *node_p, lyd_node_child_p(*node_p), NULL));
1025 *status_p = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001026 }
1027 }
1028
aPiecekdbb8baf2021-05-27 11:01:51 +02001029finish:
Radek Krejci1798aae2020-07-14 13:26:06 +02001030 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001031 LY_CHECK_RET(lydjson_metadata_finish(lydctx, lyd_node_child_p(*node_p)));
Radek Krejci1798aae2020-07-14 13:26:06 +02001032
1033 /* move after the item */
1034 return lyjson_ctx_next(lydctx->jsonctx, status_p);
1035}
1036
1037/**
aPiecek6cee5d02021-05-12 10:32:00 +02001038 * @brief Move to the second item in the name/X pair and parse opaq node from the input.
1039 *
1040 * This function is basically the wrapper of the ::lydjson_parse_opaq().
1041 * In addition, it calls the ::json_ctx_next() and prepares the status_inner_p parameter
1042 * for ::lydjson_parse_opaq().
1043 *
1044 * @param[in] lydctx JSON data parser context.
1045 * @param[in] name Name of the opaq node to create.
1046 * @param[in] name_len Length of the @p name string.
1047 * @param[in] prefix Prefix of the opaq node to create.
1048 * @param[in] prefix_len Length of the @p prefx string.
1049 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1050 * but must be set if @p first is not.
1051 * @param[in,out] status_p Pointer to the current status of the parser context,
1052 * since the function manipulates with the context and process the input, the status can be updated.
1053 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1054 * @param[out] node_p Pointer to the created opaq node.
1055 * @return LY_ERR value.
1056 */
1057static LY_ERR
1058lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, size_t name_len,
1059 const char *prefix, size_t prefix_len, struct lyd_node_inner *parent, enum LYJSON_PARSER_STATUS *status_p,
1060 struct lyd_node **first_p, struct lyd_node **node_p)
1061{
1062 enum LYJSON_PARSER_STATUS status_inner = 0;
1063
1064 /* move to the second item in the name/X pair */
1065 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
1066
1067 if (*status_p == LYJSON_ARRAY) {
1068 /* move into the array */
1069 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
1070 } else {
1071 /* just a flag to pass correct parameters into lydjson_parse_opaq() */
1072 status_inner = LYJSON_ERROR;
1073 }
1074
1075 if (status_inner == LYJSON_ERROR) {
1076 status_inner = *status_p;
1077 }
1078
1079 /* parse opaq node from the input */
1080 LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner, first_p, node_p));
1081
1082 return LY_SUCCESS;
1083}
1084
1085/**
Radek Krejci1798aae2020-07-14 13:26:06 +02001086 * @brief Process the attribute container (starting by @)
1087 *
1088 * @param[in] lydctx JSON data parser context.
1089 * @param[in] attr_node The data node referenced by the attribute container, if already known.
1090 * @param[in] snode The schema node of the data node referenced by the attribute container, if known.
1091 * @param[in] name Name of the opaq node to create.
1092 * @param[in] name_len Length of the @p name string.
1093 * @param[in] prefix Prefix of the opaq node to create.
1094 * @param[in] prefix_len Length of the @p prefx string.
1095 * @param[in] parent Data parent of the opaq node to create, can be NULL in case of top level,
1096 * but must be set if @p first is not.
1097 * @param[in,out] status_p Pointer to the current status of the parser context,
1098 * since the function manipulates with the context and process the input, the status can be updated.
1099 * @param[in,out] first_p First top-level/parent sibling, must be set if @p parent is not.
1100 * @param[out] node_p Pointer to the created opaq node.
1101 * @return LY_ERR value.
1102 */
1103static LY_ERR
1104lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
Michal Vaskoe0665742021-02-11 11:08:44 +01001105 const char *name, size_t name_len, const char *prefix, size_t prefix_len, struct lyd_node *parent,
1106 enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001107{
1108 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001109
1110 /* parse as an attribute to a node */
1111 if (!attr_node && snode) {
1112 /* try to find the instance */
1113 for (struct lyd_node *iter = *first_p; iter; iter = iter->next) {
1114 if (iter->schema == snode) {
1115 attr_node = iter;
1116 break;
1117 }
1118 }
1119 }
1120 if (!attr_node) {
1121 /* parse just as an opaq node with the name beginning with @,
1122 * later we have to check that it belongs to a standard node
1123 * and it is supposed to be converted to a metadata */
1124 uint32_t prev_opts;
1125
Radek Krejci1798aae2020-07-14 13:26:06 +02001126 /* backup parser options to parse unknown metadata as opaq nodes and try to resolve them later */
Michal Vaskoe0665742021-02-11 11:08:44 +01001127 prev_opts = lydctx->parse_opts;
1128 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1129 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Radek Krejci1798aae2020-07-14 13:26:06 +02001130
aPiecek6cee5d02021-05-12 10:32:00 +02001131 ret = lydjson_ctx_next_parse_opaq(lydctx, prefix ? prefix - 1 : name - 1, prefix ? prefix_len + name_len + 2 : name_len + 1,
1132 NULL, 0, (struct lyd_node_inner *)parent, status_p, first_p, node_p);
Radek Krejci1798aae2020-07-14 13:26:06 +02001133
1134 /* restore the parser options */
Michal Vaskoe0665742021-02-11 11:08:44 +01001135 lydctx->parse_opts = prev_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001136 } else {
1137 ret = lydjson_metadata(lydctx, snode, attr_node);
1138 }
1139
1140 return ret;
1141}
1142
1143/**
Michal Vasko32ac9942020-08-12 14:35:12 +02001144 * @brief Parse a single instance of a node.
1145 *
1146 * @param[in] lydctx JSON data parser context. When the function returns, the context is in the same state
1147 * as before calling, despite it is necessary to process input data for checking.
1148 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1149 * @param[in,out] first_p Pointer to the variable holding the first top-level sibling, must be set if @p parent is not.
1150 * @param[in] snode Schema node corresponding to the member currently being processed in the context.
1151 * @param[in] name Parsed JSON node name.
1152 * @param[in] name_len Lenght of @p name.
1153 * @param[in] prefix Parsed JSON node prefix.
1154 * @param[in] prefix_len Length of @p prefix.
1155 * @param[in,out] status JSON parser status, is updated.
1156 * @param[out] node Parsed data (or opaque) node.
1157 * @return LY_SUCCESS if a node was successfully parsed,
1158 * @return LY_EINVAL in case of invalid JSON encoding,
1159 * @return LY_ERR on other errors.
1160 */
1161static LY_ERR
1162lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node_inner *parent, struct lyd_node **first_p,
Radek Krejci0f969882020-08-21 16:56:47 +02001163 const struct lysc_node *snode, const char *name, size_t name_len, const char *prefix, size_t prefix_len,
1164 enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
Michal Vasko32ac9942020-08-12 14:35:12 +02001165{
1166 LY_ERR ret;
Radek Krejci2b18bf12020-11-06 11:20:20 +01001167 uint32_t type_hints = 0;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001168 uint32_t prev_parse_opts, prev_int_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001169 struct lyd_node *tree = NULL;
1170
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001171 ret = lydjson_data_check_opaq(lydctx, snode, &type_hints);
Michal Vasko32ac9942020-08-12 14:35:12 +02001172 if (ret == LY_SUCCESS) {
1173 assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1174 if (snode->nodetype & LYD_NODE_TERM) {
1175 /* create terminal node */
1176 ret = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, lydctx->jsonctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001177 lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
Michal Vasko69730152020-10-09 16:30:07 +02001178 type_hints, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001179 LY_CHECK_RET(ret);
1180
1181 /* move JSON parser */
Michal Vasko85430702021-07-21 14:29:56 +02001182 if (*status == LYJSON_ARRAY) {
1183 /* only [null], 2 more moves are needed */
1184 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1185 assert(*status == LYJSON_NULL);
1186 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
1187 assert(*status == LYJSON_ARRAY_CLOSED);
1188 }
1189 LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status));
Michal Vasko32ac9942020-08-12 14:35:12 +02001190 } else if (snode->nodetype & LYD_NODE_INNER) {
1191 /* create inner node */
1192 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1193
1194 ret = lyd_create_inner(snode, node);
1195 LY_CHECK_RET(ret);
1196
Radek Krejciddace2c2021-01-08 11:30:56 +01001197 LOG_LOCSET(snode, *node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001198
Michal Vasko32ac9942020-08-12 14:35:12 +02001199 /* process children */
1200 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001201 ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001202 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001203 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1204 }
1205
1206 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001207 ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
Radek Krejciddace2c2021-01-08 11:30:56 +01001208 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001209
1210 if (snode->nodetype == LYS_LIST) {
1211 /* check all keys exist */
1212 ret = lyd_parse_check_keys(*node);
Radek Krejciddace2c2021-01-08 11:30:56 +01001213 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001214 }
1215
Michal Vaskoe0665742021-02-11 11:08:44 +01001216 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko32ac9942020-08-12 14:35:12 +02001217 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001218 ret = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001219 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001220
1221 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001222 ret = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_exts,
1223 &lydctx->node_types, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +01001224 LY_CHECK_ERR_RET(ret, LOG_LOCBACK(1, 1, 0, 0), ret);
Michal Vasko32ac9942020-08-12 14:35:12 +02001225 }
1226
Radek Krejciddace2c2021-01-08 11:30:56 +01001227 LOG_LOCBACK(1, 1, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001228
Michal Vasko32ac9942020-08-12 14:35:12 +02001229 /* move JSON parser */
1230 ret = lyjson_ctx_next(lydctx->jsonctx, status);
1231 LY_CHECK_RET(ret);
1232 } else if (snode->nodetype & LYD_NODE_ANY) {
1233 /* create any node */
1234 LY_CHECK_RET(*status != LYJSON_OBJECT && *status != LYJSON_OBJECT_EMPTY, LY_EINVAL);
1235
1236 /* parse any data tree with correct options */
1237 /* first backup the current options and then make the parser to process data as opaq nodes */
Michal Vasko02ed9d82021-07-15 14:58:04 +02001238 prev_parse_opts = lydctx->parse_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001239 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
1240 lydctx->parse_opts |= LYD_PARSE_OPAQ;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001241 prev_int_opts = lydctx->int_opts;
1242 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
Michal Vasko32ac9942020-08-12 14:35:12 +02001243
1244 /* process the anydata content */
1245 while (*status != LYJSON_OBJECT_CLOSED && *status != LYJSON_OBJECT_EMPTY) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001246 ret = lydjson_subtree_r(lydctx, NULL, &tree, NULL);
Michal Vasko32ac9942020-08-12 14:35:12 +02001247 LY_CHECK_RET(ret);
1248 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
1249 }
1250
1251 /* restore parser options */
Michal Vasko02ed9d82021-07-15 14:58:04 +02001252 lydctx->parse_opts = prev_parse_opts;
1253 lydctx->int_opts = prev_int_opts;
Michal Vasko32ac9942020-08-12 14:35:12 +02001254
1255 /* finish linking metadata */
1256 ret = lydjson_metadata_finish(lydctx, &tree);
1257 LY_CHECK_RET(ret);
1258
Michal Vasko366a4a12020-12-04 16:23:57 +01001259 ret = lyd_create_any(snode, tree, LYD_ANYDATA_DATATREE, 1, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001260 LY_CHECK_RET(ret);
1261 }
Michal Vaskocaf608d2021-07-23 13:51:23 +02001262
1263 /* add/correct flags */
1264 lyd_parse_set_data_flags(*node, &lydctx->node_when, &lydctx->node_exts, &(*node)->meta, lydctx->parse_opts);
Michal Vasko32ac9942020-08-12 14:35:12 +02001265 } else if (ret == LY_ENOT) {
1266 /* parse it again as an opaq node */
1267 ret = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent,
Michal Vasko69730152020-10-09 16:30:07 +02001268 status, status, first_p, node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001269 LY_CHECK_RET(ret);
1270
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001271 if (snode->nodetype == LYS_LIST) {
1272 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
1273 } else if (snode->nodetype == LYS_LEAFLIST) {
1274 ((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
Michal Vasko32ac9942020-08-12 14:35:12 +02001275 }
1276 }
1277
1278 return ret;
1279}
1280
1281/**
Michal Vaskoa5da3292020-08-12 13:10:50 +02001282 * @brief Parse JSON subtree. All leaf-list and list instances of a node are considered one subtree.
Radek Krejci1798aae2020-07-14 13:26:06 +02001283 *
1284 * @param[in] lydctx JSON data parser context.
1285 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
1286 * @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 +01001287 * @param[in,out] parsed Optional set to add all the parsed siblings into.
Radek Krejci1798aae2020-07-14 13:26:06 +02001288 * @return LY_ERR value.
1289 */
1290static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001291lydjson_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 +02001292{
1293 LY_ERR ret = LY_SUCCESS;
1294 enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx, 0);
aPiecek49e2a3a2021-05-13 10:36:46 +02001295 const char *name, *prefix = NULL, *expected = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001296 size_t name_len, prefix_len = 0;
Radek Krejci857189e2020-09-01 13:26:36 +02001297 ly_bool is_meta = 0;
Michal Vaskocabe0702020-08-12 10:14:36 +02001298 const struct lysc_node *snode = NULL;
Michal Vasko32ac9942020-08-12 14:35:12 +02001299 struct lyd_node *node = NULL, *attr_node = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001300 const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
aPiecek49e2a3a2021-05-13 10:36:46 +02001301 char *value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001302
1303 assert(parent || first_p);
1304 assert(status == LYJSON_OBJECT);
1305
1306 /* process the node name */
aPiecekd2c39372021-06-16 14:03:12 +02001307 lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
1308 lyjson_ctx_submit_dynamic_value(lydctx->jsonctx, &value);
Radek Krejci1798aae2020-07-14 13:26:06 +02001309
Michal Vaskocabe0702020-08-12 10:14:36 +02001310 if (!is_meta || name_len || prefix_len) {
1311 /* get the schema node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001312 ret = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, (struct lyd_node_inner *)parent, &snode);
Michal Vaskocabe0702020-08-12 10:14:36 +02001313 if (ret == LY_ENOT) {
1314 /* skip element with children */
1315 ret = lydjson_data_skip(lydctx->jsonctx);
1316 LY_CHECK_GOTO(ret, cleanup);
1317 status = lyjson_ctx_status(lydctx->jsonctx, 0);
1318 /* nothing for now, continue with another call of lydjson_subtree_r() */
Radek Krejci1798aae2020-07-14 13:26:06 +02001319 goto cleanup;
1320 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001321 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001322
Michal Vaskocabe0702020-08-12 10:14:36 +02001323 if (!snode) {
1324 /* we will not be parsing it as metadata */
1325 is_meta = 0;
1326 }
1327 }
1328
1329 if (is_meta) {
1330 /* parse as metadata */
1331 if (!name_len && !prefix_len) {
1332 /* parent's metadata without a name - use the schema from the parent */
1333 if (!parent) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001334 LOGVAL(ctx, LYVE_SYNTAX_JSON,
Michal Vasko69730152020-10-09 16:30:07 +02001335 "Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
Michal Vaskocabe0702020-08-12 10:14:36 +02001336 ret = LY_EVALID;
1337 goto cleanup;
1338 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001339 attr_node = parent;
Michal Vaskocabe0702020-08-12 10:14:36 +02001340 snode = attr_node->schema;
1341 }
1342 ret = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
Michal Vasko69730152020-10-09 16:30:07 +02001343 first_p, &node);
Michal Vaskocabe0702020-08-12 10:14:36 +02001344 LY_CHECK_GOTO(ret, cleanup);
1345 } else if (!snode) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001346 /* parse as an opaq node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001347 assert((lydctx->parse_opts & LYD_PARSE_OPAQ) || (lydctx->int_opts));
Radek Krejci1798aae2020-07-14 13:26:06 +02001348
aPiecekb7b29e62021-05-11 10:02:43 +02001349 /* opaq node cannot have an empty string as the name. */
1350 if (name_len == 0) {
1351 LOGVAL(lydctx->jsonctx->ctx, LYVE_SYNTAX_JSON, "A JSON object member name cannot be a zero-length string.");
1352 ret = LY_EVALID;
1353 goto cleanup;
1354 }
1355
aPiecek6cee5d02021-05-12 10:32:00 +02001356 /* move to the second item in the name/X pair and parse opaq */
1357 ret = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len,
1358 (struct lyd_node_inner *)parent, &status, first_p, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001359 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskocabe0702020-08-12 10:14:36 +02001360 } else {
Michal Vasko32ac9942020-08-12 14:35:12 +02001361 /* parse as a standard lyd_node but it can still turn out to be an opaque node */
Radek Krejci1798aae2020-07-14 13:26:06 +02001362
1363 /* move to the second item in the name/X pair */
1364 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1365 LY_CHECK_GOTO(ret, cleanup);
1366
1367 /* first check the expected representation according to the nodetype and then continue with the content */
1368 switch (snode->nodetype) {
1369 case LYS_LEAFLIST:
Michal Vasko32ac9942020-08-12 14:35:12 +02001370 case LYS_LIST:
1371 if (snode->nodetype == LYS_LEAFLIST) {
1372 expected = "name/array of values";
1373 } else {
1374 expected = "name/array of objects";
1375 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001376
1377 LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
1378
1379 /* move into array */
1380 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1381 LY_CHECK_GOTO(ret, cleanup);
1382
Michal Vasko32ac9942020-08-12 14:35:12 +02001383 /* process all the values/objects */
1384 do {
Michal Vasko6ee6f432021-07-16 09:49:14 +02001385 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node,
1386 lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001387
Michal Vaskoe0665742021-02-11 11:08:44 +01001388 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1389 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001390 if (ret == LY_EINVAL) {
1391 goto representation_error;
1392 } else if (ret) {
1393 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001394 }
Michal Vasko32ac9942020-08-12 14:35:12 +02001395 } while (status != LYJSON_ARRAY_CLOSED);
Radek Krejci1798aae2020-07-14 13:26:06 +02001396
Michal Vasko32ac9942020-08-12 14:35:12 +02001397 /* move after the array */
1398 ret = lyjson_ctx_next(lydctx->jsonctx, &status);
1399 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001400
1401 break;
Michal Vasko32ac9942020-08-12 14:35:12 +02001402 case LYS_LEAF:
Radek Krejci1798aae2020-07-14 13:26:06 +02001403 case LYS_CONTAINER:
1404 case LYS_NOTIF:
1405 case LYS_ACTION:
1406 case LYS_RPC:
Michal Vasko32ac9942020-08-12 14:35:12 +02001407 case LYS_ANYDATA:
1408 case LYS_ANYXML:
1409 if (snode->nodetype == LYS_LEAF) {
1410 if (status == LYJSON_ARRAY) {
1411 expected = "name/[null]";
1412 } else {
1413 expected = "name/value";
1414 }
1415 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +02001416 expected = "name/object";
1417 }
1418
Michal Vasko32ac9942020-08-12 14:35:12 +02001419 /* process the value/object */
Michal Vaskoe0665742021-02-11 11:08:44 +01001420 ret = lydjson_parse_instance(lydctx, (struct lyd_node_inner *)parent, first_p, snode, name, name_len,
1421 prefix, prefix_len, &status, &node);
Michal Vasko32ac9942020-08-12 14:35:12 +02001422 if (ret == LY_EINVAL) {
1423 goto representation_error;
1424 } else if (ret) {
1425 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001426 }
1427
Michal Vasko32ac9942020-08-12 14:35:12 +02001428 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001429 /* rememeber the RPC/action/notification */
1430 lydctx->op_node = node;
1431 }
1432
1433 break;
Radek Krejci1798aae2020-07-14 13:26:06 +02001434 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001435 }
1436
Michal Vasko32ac9942020-08-12 14:35:12 +02001437 /* finally connect the parsed node */
Michal Vasko6ee6f432021-07-16 09:49:14 +02001438 lydjson_maintain_children((struct lyd_node_inner *)parent, first_p, &node,
1439 lydctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001440
1441 /* rememeber a successfully parsed node */
1442 if (parsed) {
1443 ly_set_add(parsed, node, 1, NULL);
1444 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001445
Radek Krejci5813c362021-01-05 10:51:57 +01001446 /* success */
1447 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001448
1449representation_error:
Michal Vaskoe0665742021-02-11 11:08:44 +01001450 LOG_LOCSET(NULL, parent, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001451 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 +02001452 lys_nodetype2str(snode->nodetype), snode->name, expected, lyjson_token2str(status));
Radek Krejciddace2c2021-01-08 11:30:56 +01001453 LOG_LOCBACK(0, parent ? 1 : 0, 0, 0);
Radek Krejci1798aae2020-07-14 13:26:06 +02001454 ret = LY_EVALID;
Radek Krejci5813c362021-01-05 10:51:57 +01001455
1456cleanup:
aPiecek49e2a3a2021-05-13 10:36:46 +02001457 free(value);
Radek Krejci5813c362021-01-05 10:51:57 +01001458 lyd_free_tree(node);
1459 return ret;
Radek Krejci1798aae2020-07-14 13:26:06 +02001460}
1461
1462/**
1463 * @brief Common start of JSON parser processing different types of the input data.
1464 *
1465 * @param[in] ctx libyang context
1466 * @param[in] in Input structure.
Michal Vaskoe0665742021-02-11 11:08:44 +01001467 * @param[in] parse_opts Options for parser, see @ref dataparseroptions.
1468 * @param[in] val_opts Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +02001469 * @param[out] lydctx_p Data parser context to finish validation.
Radek Krejcicd5f6222020-11-12 08:54:13 +01001470 * @param[out] status Storage for the current context's status
Radek Krejci1798aae2020-07-14 13:26:06 +02001471 * @return LY_ERR value.
1472 */
1473static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001474lyd_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 +01001475 struct lyd_json_ctx **lydctx_p, enum LYJSON_PARSER_STATUS *status)
Radek Krejci1798aae2020-07-14 13:26:06 +02001476{
1477 LY_ERR ret = LY_SUCCESS;
1478 struct lyd_json_ctx *lydctx;
Radek Krejcid54412f2020-12-17 20:25:35 +01001479 size_t i;
Radek Krejci1798aae2020-07-14 13:26:06 +02001480
Radek Krejcicd5f6222020-11-12 08:54:13 +01001481 assert(lydctx_p);
1482 assert(status);
1483
Radek Krejci1798aae2020-07-14 13:26:06 +02001484 /* init context */
1485 lydctx = calloc(1, sizeof *lydctx);
1486 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
Michal Vaskoe0665742021-02-11 11:08:44 +01001487 lydctx->parse_opts = parse_opts;
1488 lydctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001489 lydctx->free = lyd_json_ctx_free;
Radek Krejci1798aae2020-07-14 13:26:06 +02001490
Radek Krejci284f31f2020-09-18 15:40:29 +02001491 /* starting top-level */
1492 for (i = 0; in->current[i] != '\0' && is_jsonws(in->current[i]); i++) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001493 if (in->current[i] == '\n') {
1494 /* new line */
Radek Krejcid54412f2020-12-17 20:25:35 +01001495 LY_IN_NEW_LINE(in);
Michal Vasko61d76362020-10-07 10:47:30 +02001496 }
Radek Krejci284f31f2020-09-18 15:40:29 +02001497 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001498
Radek Krejci284f31f2020-09-18 15:40:29 +02001499 LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001500 *status = lyjson_ctx_status(lydctx->jsonctx, 0);
Radek Krejcibb27df32020-11-13 15:39:16 +01001501 if ((*status == LYJSON_END) || (*status == LYJSON_OBJECT_EMPTY) || (*status == LYJSON_OBJECT)) {
Radek Krejci284f31f2020-09-18 15:40:29 +02001502 *lydctx_p = lydctx;
1503 return LY_SUCCESS;
Radek Krejcicd5f6222020-11-12 08:54:13 +01001504 } else {
1505 /* expecting top-level object */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001506 LOGVAL(ctx, LYVE_SYNTAX_JSON, "Expected top-level JSON object, but %s found.",
Radek Krejcicd5f6222020-11-12 08:54:13 +01001507 lyjson_token2str(*status));
1508 *lydctx_p = NULL;
Michal Vasko93509012020-11-13 10:26:09 +01001509 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejcicd5f6222020-11-12 08:54:13 +01001510 return LY_EVALID;
Radek Krejci284f31f2020-09-18 15:40:29 +02001511 }
Radek Krejci1798aae2020-07-14 13:26:06 +02001512}
1513
1514LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001515lyd_parse_json(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1516 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1517 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Radek Krejci1798aae2020-07-14 13:26:06 +02001518{
Michal Vaskoe0665742021-02-11 11:08:44 +01001519 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001520 struct lyd_json_ctx *lydctx = NULL;
1521 enum LYJSON_PARSER_STATUS status;
Michal Vaskoe0665742021-02-11 11:08:44 +01001522 uint32_t int_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001523
Michal Vaskoe0665742021-02-11 11:08:44 +01001524 rc = lyd_parse_json_init(ctx, in, parse_opts, val_opts, &lydctx, &status);
1525 LY_CHECK_GOTO(rc || status == LYJSON_END || status == LYJSON_OBJECT_EMPTY, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001526
Radek Krejcicd5f6222020-11-12 08:54:13 +01001527 assert(status == LYJSON_OBJECT);
Radek Krejci1798aae2020-07-14 13:26:06 +02001528
Michal Vaskoe0665742021-02-11 11:08:44 +01001529 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001530 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001531 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1532 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001533 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001534 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1535 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001536 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001537 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1538 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001539 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001540 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1541 break;
1542 default:
1543 LOGINT(ctx);
1544 rc = LY_EINT;
1545 goto cleanup;
1546 }
1547 lydctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001548 lydctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001549
1550 /* find the operation node if it exists already */
1551 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1552
Radek Krejci1798aae2020-07-14 13:26:06 +02001553 /* read subtree(s) */
Michal Vaskoe0665742021-02-11 11:08:44 +01001554 while (lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
1555 rc = lydjson_subtree_r(lydctx, parent, first_p, parsed);
1556 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001557
1558 status = lyjson_ctx_status(lydctx->jsonctx, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001559
1560 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1561 break;
1562 }
1563 }
1564
1565 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] &&
1566 (lyjson_ctx_status(lydctx->jsonctx, 0) != LYJSON_OBJECT_CLOSED)) {
1567 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1568 rc = LY_EVALID;
1569 goto cleanup;
1570 }
1571 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1572 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1573 rc = LY_EVALID;
1574 goto cleanup;
Radek Krejci1798aae2020-07-14 13:26:06 +02001575 }
1576
1577 /* finish linking metadata */
Michal Vaskoe0665742021-02-11 11:08:44 +01001578 rc = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
1579 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +02001580
1581cleanup:
1582 /* there should be no unresolved types stored */
Michal Vaskoe0665742021-02-11 11:08:44 +01001583 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vasko49c39d82020-11-06 17:20:27 +01001584 !lydctx->node_when.count));
Radek Krejci1798aae2020-07-14 13:26:06 +02001585
Michal Vaskoe0665742021-02-11 11:08:44 +01001586 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001587 lyd_json_ctx_free((struct lyd_ctx *)lydctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001588 } else {
1589 *lydctx_p = (struct lyd_ctx *)lydctx;
1590 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001591 return rc;
Michal Vasko90932a92020-02-12 14:33:03 +01001592}