#define _GNU_SOURCE
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "context.h"
#include "dict.h"
#include "in_internal.h"
#include "json.h"
#include "log.h"
#include "ly_common.h"
#include "parser_data.h"
#include "parser_internal.h"
#include "plugins_exts.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_data_internal.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#include "validation.h"
static LY_ERR lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
struct ly_set *parsed);
static void
lyd_json_ctx_free(struct lyd_ctx *lydctx)
{
struct lyd_json_ctx *ctx = (struct lyd_json_ctx *)lydctx;
if (lydctx) {
lyd_ctx_free(lydctx);
lyjson_ctx_free(ctx->jsonctx);
free(ctx);
}
}
static void
lyjson_ctx_give_dynamic_value(struct lyjson_ctx *jsonctx, char **dst)
{
assert(jsonctx && dst);
if (!jsonctx->dynamic) {
return;
}
if (dst) {
free(*dst);
}
*dst = NULL;
*dst = (char *)jsonctx->value;
jsonctx->dynamic = 0;
}
static void
lydjson_parse_name(const char *value, uint32_t value_len, const char **name_p, uint32_t *name_len_p, const char **prefix_p,
uint32_t *prefix_len_p, ly_bool *is_meta_p)
{
const char *name, *prefix = NULL;
uint32_t name_len, prefix_len = 0;
ly_bool is_meta = 0;
name = memchr(value, ':', value_len);
if (name != NULL) {
prefix = value;
if (*prefix == '@') {
is_meta = 1;
prefix++;
}
prefix_len = name - prefix;
name++;
name_len = value_len - (prefix_len + 1) - is_meta;
} else {
name = value;
if (name[0] == '@') {
is_meta = 1;
name++;
}
name_len = value_len - is_meta;
}
*name_p = name;
*name_len_p = name_len;
*prefix_p = prefix;
*prefix_len_p = prefix_len;
*is_meta_p = is_meta;
}
static LY_ERR
lydjson_get_node_prefix(struct lyd_node *node, const char *local_prefix, uint32_t local_prefix_len, const char **prefix_p,
uint32_t *prefix_len_p)
{
struct lyd_node_opaq *onode;
const char *module_name = NULL;
assert(prefix_p && prefix_len_p);
if (local_prefix_len) {
*prefix_p = local_prefix;
*prefix_len_p = local_prefix_len;
return LY_SUCCESS;
}
while (node) {
if (node->schema) {
module_name = node->schema->module->name;
break;
}
onode = (struct lyd_node_opaq *)node;
if (onode->name.module_name) {
module_name = onode->name.module_name;
break;
} else if (onode->name.prefix) {
module_name = onode->name.prefix;
break;
}
node = node->parent;
}
*prefix_p = module_name;
*prefix_len_p = ly_strlen(module_name);
return LY_SUCCESS;
}
static LY_ERR
lydjson_data_skip(struct lyjson_ctx *jsonctx)
{
enum LYJSON_PARSER_STATUS status, current;
uint32_t depth;
status = lyjson_ctx_status(jsonctx);
depth = lyjson_ctx_depth(jsonctx);
switch (status) {
case LYJSON_OBJECT:
++depth;
do {
LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t));
} while ((current != LYJSON_OBJECT_CLOSED) || (depth != lyjson_ctx_depth(jsonctx)));
break;
case LYJSON_ARRAY:
++depth;
do {
LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t));
} while ((current != LYJSON_ARRAY_CLOSED) || (depth != lyjson_ctx_depth(jsonctx)));
break;
case LYJSON_OBJECT_NAME:
LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t));
if ((current == LYJSON_OBJECT) || (current == LYJSON_ARRAY)) {
LY_CHECK_RET(lydjson_data_skip(jsonctx));
}
break;
default:
LY_CHECK_RET(lyjson_ctx_next(jsonctx, ¤t));
break;
}
return LY_SUCCESS;
}
static LY_ERR
lydjson_get_snode(struct lyd_json_ctx *lydctx, ly_bool is_attr, const char *prefix, uint32_t prefix_len, const char *name,
uint32_t name_len, struct lyd_node *parent, const struct lysc_node **snode, struct lysc_ext_instance **ext)
{
LY_ERR r;
struct lys_module *mod = NULL;
const struct lysc_node *sparent;
uint32_t getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
*snode = NULL;
if (ext) {
*ext = NULL;
}
if (parent && parent->schema && !(parent->schema->nodetype & LYD_NODE_ANY)) {
sparent = parent->schema;
} else {
sparent = NULL;
}
if (!prefix_len) {
prefix = NULL;
}
r = lys_find_child_node(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, sparent, NULL, prefix, prefix_len,
LY_VALUE_JSON, NULL, name, name_len, getnext_opts, snode, ext);
LY_CHECK_RET(r && (r != LY_ENOT), r);
if (!r) {
LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
return LY_SUCCESS;
}
if (prefix_len) {
mod = ly_ctx_get_module_implemented2(parent ? LYD_CTX(parent) : lydctx->jsonctx->ctx, prefix, prefix_len);
} else if (parent) {
if (parent->schema) {
mod = parent->schema->module;
}
} else if (!(lydctx->int_opts & LYD_INTOPT_ANY)) {
LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_SYNTAX_JSON,
"Top-level JSON object member \"%.*s\" must be namespace-qualified.",
(int)(is_attr ? name_len + 1 : name_len), is_attr ? name - 1 : name);
return LY_EVALID;
}
if (!(lydctx->parse_opts & LYD_PARSE_STRICT)) {
return LY_SUCCESS;
}
if (!mod) {
LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_REFERENCE, "No module named \"%.*s\" in the context.",
(int)prefix_len, prefix);
return LY_EVALID;
}
if (sparent) {
LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
(int)name_len, name, sparent->name);
} else {
LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
(int)name_len, name, mod->name);
}
return LY_EVALID;
}
static LY_ERR
lydjson_value_type_hint(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, enum LYJSON_PARSER_STATUS *status_p,
uint32_t *type_hint_p)
{
struct lyjson_ctx *jsonctx = lydctx->jsonctx;
*type_hint_p = 0;
if (*status_p == LYJSON_ARRAY) {
LY_CHECK_RET(lyjson_ctx_next(jsonctx, status_p));
if (*status_p != LYJSON_NULL) {
if (snode) {
LOG_LOCSET(snode);
}
LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX_JSON,
"Expected JSON name/value or special name/[null], but input data contains name/[%s].",
lyjson_token2str(*status_p));
if (snode) {
LOG_LOCBACK(1);
}
return LY_EINVAL;
}
LY_CHECK_RET(lyjson_ctx_next(jsonctx, NULL));
if (lyjson_ctx_status(jsonctx) != LYJSON_ARRAY_CLOSED) {
if (snode) {
LOG_LOCSET(snode);
}
LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX_JSON, "Expected array end, but input data contains %s.",
lyjson_token2str(*status_p));
if (snode) {
LOG_LOCBACK(1);
}
return LY_EINVAL;
}
*type_hint_p = LYD_VALHINT_EMPTY;
} else if (*status_p == LYJSON_STRING) {
*type_hint_p = LYD_VALHINT_STRING | LYD_VALHINT_NUM64;
} else if (*status_p == LYJSON_NUMBER) {
*type_hint_p = LYD_VALHINT_DECNUM;
} else if ((*status_p == LYJSON_FALSE) || (*status_p == LYJSON_TRUE)) {
*type_hint_p = LYD_VALHINT_BOOLEAN;
} else if (*status_p == LYJSON_NULL) {
*type_hint_p = 0;
} else {
if (snode) {
LOG_LOCSET(snode);
}
LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX_JSON, "Unexpected input data %s.", lyjson_token2str(*status_p));
if (snode) {
LOG_LOCBACK(1);
}
return LY_EINVAL;
}
if (lydctx->parse_opts & LYD_PARSE_JSON_STRING_DATATYPES) {
*type_hint_p |= LYD_VALHINT_STRING_DATATYPES;
}
return LY_SUCCESS;
}
static LY_ERR
lydjson_check_list(struct lyd_json_ctx *lydctx, const struct lysc_node *list)
{
LY_ERR rc = LY_SUCCESS;
struct lyjson_ctx *jsonctx = lydctx->jsonctx;
enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx);
struct ly_set key_set = {0};
const struct lysc_node *snode;
uint32_t i, hints;
assert(list && (list->nodetype == LYS_LIST));
snode = NULL;
while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
rc = ly_set_add(&key_set, (void *)snode, 1, NULL);
LY_CHECK_GOTO(rc, cleanup);
}
if (!key_set.count) {
goto cleanup;
}
if (status == LYJSON_OBJECT) {
do {
const char *name, *prefix;
uint32_t name_len, prefix_len;
ly_bool is_attr;
LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
if (status != LYJSON_OBJECT_NAME) {
break;
}
snode = NULL;
lydjson_parse_name(jsonctx->value, jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
if (!is_attr && !prefix) {
for (i = 0; i < key_set.count; ++i) {
if (!ly_strncmp(key_set.snodes[i]->name, name, name_len)) {
snode = key_set.snodes[i];
break;
}
}
LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
if (snode) {
if ((status < LYJSON_NUMBER) || (status > LYJSON_NULL)) {
rc = LY_ENOT;
goto cleanup;
}
rc = lydjson_value_type_hint(lydctx, snode, &status, &hints);
LY_CHECK_GOTO(rc, cleanup);
rc = ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len * 8, LY_VALUE_JSON, NULL,
hints);
LY_CHECK_GOTO(rc, cleanup);
ly_set_rm_index(&key_set, i, NULL);
}
LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
} else {
LY_CHECK_GOTO(rc = lydjson_data_skip(jsonctx), cleanup);
LY_CHECK_GOTO(rc = lyjson_ctx_next(jsonctx, &status), cleanup);
}
} while (key_set.count && (status == LYJSON_OBJECT_NEXT));
}
if (key_set.count) {
rc = LY_ENOT;
}
cleanup:
ly_set_erase(&key_set, NULL);
return rc;
}
static LY_ERR
lydjson_data_check_opaq(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, uint32_t *type_hint_p)
{
LY_ERR ret = LY_SUCCESS;
struct lyjson_ctx *jsonctx = lydctx->jsonctx;
enum LYJSON_PARSER_STATUS status;
uint32_t *prev_lo, temp_lo = 0;
assert(snode);
lyjson_ctx_backup(jsonctx);
status = lyjson_ctx_status(jsonctx);
if (lydctx->parse_opts & LYD_PARSE_OPAQ) {
switch (snode->nodetype) {
case LYS_LEAFLIST:
case LYS_LEAF:
prev_lo = ly_temp_log_options(&temp_lo);
if (lydjson_value_type_hint(lydctx, snode, &status, type_hint_p)) {
ret = LY_ENOT;
}
if (!ret && ly_value_validate(NULL, snode, jsonctx->value, jsonctx->value_len * 8, LY_VALUE_JSON, NULL,
*type_hint_p)) {
ret = LY_ENOT;
}
ly_temp_log_options(prev_lo);
break;
case LYS_LIST:
if (lydjson_check_list(lydctx, snode)) {
ret = LY_ENOT;
}
break;
case LYS_CONTAINER:
case LYS_RPC:
case LYS_ACTION:
case LYS_NOTIF:
if (status != LYJSON_OBJECT) {
ret = LY_ENOT;
}
break;
}
} else if (snode->nodetype & LYD_NODE_TERM) {
ret = lydjson_value_type_hint(lydctx, snode, &status, type_hint_p);
}
lyjson_ctx_restore(jsonctx);
return ret;
}
static LY_ERR
lydjson_metadata_finish(struct lyd_json_ctx *lydctx, struct lyd_node **first_p)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_node *node, *attr, *next, *meta_iter;
uint64_t instance = 0;
const char *prev = NULL;
LY_LIST_FOR_SAFE(*first_p, next, attr) {
struct lyd_node_opaq *meta_container = (struct lyd_node_opaq *)attr;
uint64_t match = 0;
ly_bool is_attr;
const char *name, *prefix;
uint32_t name_len, prefix_len;
const struct lysc_node *snode;
if (attr->schema || (meta_container->name.name[0] != '@')) {
continue;
}
if (prev != meta_container->name.name) {
lydict_remove(lydctx->jsonctx->ctx, prev);
LY_CHECK_GOTO(ret = lydict_insert(lydctx->jsonctx->ctx, meta_container->name.name, 0, &prev), cleanup);
instance = 1;
} else {
instance++;
}
LY_LIST_FOR(*first_p, node) {
if (!node->schema) {
if (strcmp(&meta_container->name.name[1], ((struct lyd_node_opaq *)node)->name.name)) {
continue;
}
if (((struct lyd_node_opaq *)node)->hints & LYD_NODEHINT_LIST) {
LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_SYNTAX, "Metadata container references a sibling list node %s.",
((struct lyd_node_opaq *)node)->name.name);
ret = LY_EVALID;
goto cleanup;
}
match++;
if (match != instance) {
continue;
}
LY_LIST_FOR(meta_container->child, meta_iter) {
struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
ret = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, meta->name.name, strlen(meta->name.name),
meta->name.prefix, ly_strlen(meta->name.prefix), meta->name.module_name,
ly_strlen(meta->name.module_name), meta->value, ly_strlen(meta->value), NULL, LY_VALUE_JSON,
NULL, meta->hints);
LY_CHECK_GOTO(ret, cleanup);
}
break;
} else {
lydjson_parse_name(meta_container->name.name, strlen(meta_container->name.name), &name, &name_len,
&prefix, &prefix_len, &is_attr);
assert(is_attr);
lydjson_get_snode(lydctx, is_attr, prefix, prefix_len, name, name_len, (*first_p)->parent, &snode, NULL);
if (snode != node->schema) {
continue;
}
match++;
if (match != instance) {
continue;
}
LY_LIST_FOR(meta_container->child, meta_iter) {
struct lyd_node_opaq *meta = (struct lyd_node_opaq *)meta_iter;
struct lys_module *mod = NULL;
mod = ly_ctx_get_module_implemented(lydctx->jsonctx->ctx, meta->name.prefix);
if (mod) {
ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, meta->name.name,
strlen(meta->name.name), meta->value, ly_strlen(meta->value) * 8, NULL, LY_VALUE_JSON,
NULL, meta->hints, node->schema, node);
LY_CHECK_GOTO(ret, cleanup);
} else if (lydctx->parse_opts & LYD_PARSE_STRICT) {
if (meta->name.prefix) {
LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE,
"Unknown (or not implemented) YANG module \"%s\" of metadata \"%s%s%s\".",
meta->name.prefix, meta->name.prefix, ly_strlen(meta->name.prefix) ? ":" : "",
meta->name.name);
} else {
LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE, "Missing YANG module of metadata \"%s\".",
meta->name.name);
}
ret = LY_EVALID;
goto cleanup;
}
}
ret = lyd_parser_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL);
LY_CHECK_GOTO(ret, cleanup);
break;
}
}
if (match != instance) {
if (instance > 1) {
LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE,
"Missing JSON data instance #%" PRIu64 " to be coupled with %s metadata.",
instance, meta_container->name.name);
} else {
LOGVAL(lydctx->jsonctx->ctx, attr, LYVE_REFERENCE, "Missing JSON data instance to be coupled with %s metadata.",
meta_container->name.name);
}
ret = LY_EVALID;
} else {
if (attr == (*first_p)) {
*first_p = attr->next;
}
lyd_free_tree(attr);
}
}
cleanup:
lydict_remove(lydctx->jsonctx->ctx, prev);
return ret;
}
static LY_ERR
lydjson_meta_attr(struct lyd_json_ctx *lydctx, struct lyd_node *node)
{
LY_ERR rc = LY_SUCCESS, r;
enum LYJSON_PARSER_STATUS status;
const char *expected;
ly_bool in_parent = 0;
const char *name, *prefix = NULL;
char *dynamic_prefname = NULL;
uint32_t name_len, prefix_len = 0;
struct lys_module *mod;
const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
ly_bool is_attr = 0;
struct lyd_node *prev = node;
uint32_t instance = 0, val_hints;
uint16_t nodetype;
nodetype = node->schema ? node->schema->nodetype : LYS_CONTAINER;
LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
switch (nodetype) {
case LYS_LEAFLIST:
expected = "@name/array of objects/nulls";
LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
next_entry:
if (status == LYJSON_ARRAY_CLOSED) {
goto cleanup;
}
LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
instance++;
LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_NULL), representation_error);
if (!node || (node->schema != prev->schema)) {
LOG_LOCSET(prev->schema);
LOGVAL(lydctx->jsonctx->ctx, NULL, LYVE_REFERENCE, "Missing JSON data instance #%" PRIu32
" of %s:%s to be coupled with metadata.", instance, prev->schema->module->name, prev->schema->name);
LOG_LOCBACK(1);
rc = LY_EVALID;
goto cleanup;
}
if (status == LYJSON_NULL) {
prev = node;
node = node->next;
LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
goto next_entry;
}
break;
case LYS_LEAF:
case LYS_ANYXML:
expected = "@name/object";
LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
break;
case LYS_CONTAINER:
case LYS_LIST:
case LYS_ANYDATA:
case LYS_NOTIF:
case LYS_ACTION:
case LYS_RPC:
in_parent = 1;
expected = "@/object";
LY_CHECK_GOTO(status != LYJSON_OBJECT, representation_error);
break;
default:
LOGINT(ctx);
rc = LY_EINT;
goto cleanup;
}
assert(status == LYJSON_OBJECT);
do {
LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
LY_CHECK_GOTO(status != LYJSON_OBJECT_NAME, representation_error);
lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_attr);
lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &dynamic_prefname);
if (!name_len) {
LOGVAL(ctx, prev, LYVE_SYNTAX_JSON, "Metadata in JSON found with an empty name, followed by: %.10s", name);
rc = LY_EVALID;
goto cleanup;
} else if (!prefix_len) {
LOGVAL(ctx, prev, LYVE_SYNTAX_JSON, "Metadata in JSON must be namespace-qualified, missing prefix for \"%.*s\".",
(int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
rc = LY_EVALID;
goto cleanup;
} else if (is_attr) {
LOGVAL(ctx, prev, LYVE_SYNTAX_JSON, "Invalid format of the Metadata identifier in JSON, unexpected '@' in \"%.*s\"",
(int)lydctx->jsonctx->value_len, lydctx->jsonctx->value);
rc = LY_EVALID;
goto cleanup;
}
mod = ly_ctx_get_module_implemented2(ctx, prefix, prefix_len);
if (!mod) {
if (lydctx->parse_opts & LYD_PARSE_STRICT) {
LOGVAL(ctx, prev, LYVE_REFERENCE,
"Prefix \"%.*s\" of the metadata \"%.*s\" does not match any module in the context.",
(int)prefix_len, prefix, (int)name_len, name);
rc = LY_EVALID;
goto cleanup;
}
if (node->schema) {
LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup);
status = lyjson_ctx_status(lydctx->jsonctx);
continue;
}
assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
}
LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
LY_CHECK_GOTO(rc = lydjson_value_type_hint(lydctx, node->schema, &status, &val_hints), cleanup);
if (node->schema) {
rc = lyd_parser_create_meta((struct lyd_ctx *)lydctx, node, NULL, mod, name, name_len,
lydctx->jsonctx->value, lydctx->jsonctx->value_len * 8, &lydctx->jsonctx->dynamic, LY_VALUE_JSON,
NULL, val_hints, node->schema, node);
LY_CHECK_GOTO(rc, cleanup);
rc = lyd_parser_set_data_flags(node, &node->meta, (struct lyd_ctx *)lydctx, NULL);
LY_CHECK_GOTO(rc, cleanup);
} else {
const char *module_name;
uint32_t module_name_len;
lydjson_get_node_prefix(node, prefix, prefix_len, &module_name, &module_name_len);
rc = lyd_create_attr(node, NULL, lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name,
module_name_len, lydctx->jsonctx->value, lydctx->jsonctx->value_len, &lydctx->jsonctx->dynamic,
LY_VALUE_JSON, NULL, val_hints);
LY_CHECK_GOTO(rc, cleanup);
}
LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
} while (status == LYJSON_OBJECT_NEXT);
LY_CHECK_GOTO(status != LYJSON_OBJECT_CLOSED, representation_error);
if (nodetype == LYS_LEAFLIST) {
prev = node;
node = node->next;
LY_CHECK_GOTO(rc = lyjson_ctx_next(lydctx->jsonctx, &status), cleanup);
goto next_entry;
}
goto cleanup;
representation_error:
LOGVAL(ctx, prev, LYVE_SYNTAX_JSON,
"The attribute(s) of %s \"%s\" is expected to be represented as JSON %s, but input data contains @%s/%s.",
lys_nodetype2str(nodetype), node ? LYD_NAME(node) : LYD_NAME(prev), expected, lyjson_token2str(status),
in_parent ? "" : "name");
rc = LY_EVALID;
cleanup:
if ((rc == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
if ((r = lydjson_data_skip(lydctx->jsonctx))) {
rc = r;
}
}
free(dynamic_prefname);
return rc;
}
static LY_ERR
lydjson_create_opaq(struct lyd_json_ctx *lydctx, const char *name, uint32_t name_len, const char *prefix, uint32_t prefix_len,
struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_inner_p, struct lyd_node **node_p)
{
LY_ERR ret = LY_SUCCESS;
const char *value = NULL, *module_name;
uint32_t value_len = 0, module_name_len = 0;
ly_bool dynamic = 0;
uint32_t type_hint = 0;
if (*status_inner_p != LYJSON_OBJECT) {
value = lydctx->jsonctx->value;
value_len = lydctx->jsonctx->value_len;
dynamic = lydctx->jsonctx->dynamic;
lydctx->jsonctx->dynamic = 0;
LY_CHECK_RET(lydjson_value_type_hint(lydctx, NULL, status_inner_p, &type_hint));
}
lydjson_get_node_prefix(parent, prefix, prefix_len, &module_name, &module_name_len);
if (!module_name && !parent && lydctx->any_schema) {
module_name = lydctx->any_schema->module->name;
module_name_len = strlen(module_name);
}
ret = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, module_name, module_name_len, value,
value_len, &dynamic, LY_VALUE_JSON, NULL, type_hint, node_p);
if (dynamic) {
free((char *)value);
}
return ret;
}
static LY_ERR
lydjson_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, uint32_t name_len, const char *prefix, uint32_t prefix_len,
struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p, enum LYJSON_PARSER_STATUS *status_inner_p,
struct lyd_node **first_p, struct lyd_node **node)
{
LY_ERR ret = LY_SUCCESS;
LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node), cleanup);
ret = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node);
LY_CHECK_GOTO(ret, cleanup);
if ((*status_p == LYJSON_ARRAY) && (*status_inner_p == LYJSON_NULL)) {
((struct lyd_node_opaq *)*node)->hints |= LYD_VALHINT_EMPTY;
LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup);
if (*status_inner_p != LYJSON_ARRAY_CLOSED) {
LOGVAL(lydctx->jsonctx->ctx, *node, LYVE_SYNTAX, "Array \"null\" member with another member.");
ret = LY_EVALID;
goto cleanup;
}
goto finish;
}
while (*status_p == LYJSON_ARRAY) {
if (*status_inner_p == LYJSON_OBJECT) {
((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
do {
LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL), cleanup);
*status_inner_p = lyjson_ctx_status(lydctx->jsonctx);
} while (*status_inner_p == LYJSON_OBJECT_NEXT);
} else {
((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
}
LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup);
if (*status_inner_p == LYJSON_ARRAY_CLOSED) {
goto finish;
}
assert(*status_inner_p == LYJSON_ARRAY_NEXT);
LY_CHECK_GOTO(ret = lyjson_ctx_next(lydctx->jsonctx, status_inner_p), cleanup);
LY_CHECK_GOTO(ret = lydjson_create_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_inner_p, node),
cleanup);
ret = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node);
LY_CHECK_GOTO(ret, cleanup);
}
if (*status_p == LYJSON_OBJECT) {
((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_CONTAINER;
do {
LY_CHECK_GOTO(ret = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL), cleanup);
*status_p = lyjson_ctx_status(lydctx->jsonctx);
} while (*status_p == LYJSON_OBJECT_NEXT);
}
finish:
ret = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
cleanup:
return ret;
}
static LY_ERR
lydjson_ctx_next_parse_opaq(struct lyd_json_ctx *lydctx, const char *name, uint32_t name_len, const char *prefix,
uint32_t prefix_len, struct lyd_node *parent, enum LYJSON_PARSER_STATUS *status_p,
struct lyd_node **first_p, struct lyd_node **node)
{
enum LYJSON_PARSER_STATUS status_inner = 0;
LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, status_p));
if (*status_p == LYJSON_ARRAY) {
LY_CHECK_RET(lyjson_ctx_next(lydctx->jsonctx, &status_inner));
} else {
status_inner = LYJSON_ERROR;
}
if (status_inner == LYJSON_ERROR) {
status_inner = *status_p;
}
LY_CHECK_RET(lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status_p, &status_inner,
first_p, node));
return LY_SUCCESS;
}
static LY_ERR
lydjson_parse_attribute(struct lyd_json_ctx *lydctx, struct lyd_node *attr_node, const struct lysc_node *snode,
const char *name, uint32_t name_len, const char *prefix, uint32_t prefix_len, struct lyd_node *parent,
enum LYJSON_PARSER_STATUS *status_p, struct lyd_node **first_p, struct lyd_node **node_p)
{
LY_ERR r;
const char *opaq_name, *mod_name, *attr_mod = NULL;
uint32_t opaq_name_len, attr_mod_len = 0;
if (!attr_node) {
if (!snode) {
if (!prefix) {
lydjson_get_node_prefix(parent, NULL, 0, &attr_mod, &attr_mod_len);
} else {
attr_mod = prefix;
attr_mod_len = prefix_len;
}
}
LY_LIST_FOR(parent ? lyd_child(parent) : *first_p, attr_node) {
if (snode) {
if (attr_node->schema) {
if (attr_node->schema == snode) {
break;
}
} else {
mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
if (!strcmp(LYD_NAME(attr_node), snode->name) && mod_name && !strcmp(mod_name, snode->module->name)) {
break;
}
}
} else {
if (attr_node->schema) {
mod_name = attr_node->schema->module->name;
} else {
mod_name = ((struct lyd_node_opaq *)attr_node)->name.module_name;
}
if (!ly_strncmp(LYD_NAME(attr_node), name, name_len) && mod_name && attr_mod &&
!ly_strncmp(mod_name, attr_mod, attr_mod_len)) {
break;
}
}
}
}
if (!attr_node) {
uint32_t prev_opts;
prev_opts = lydctx->parse_opts;
lydctx->parse_opts &= ~LYD_PARSE_STRICT;
lydctx->parse_opts |= LYD_PARSE_OPAQ;
opaq_name = prefix ? prefix - 1 : name - 1;
opaq_name_len = prefix ? prefix_len + name_len + 2 : name_len + 1;
r = lydjson_ctx_next_parse_opaq(lydctx, opaq_name, opaq_name_len, NULL, 0, parent, status_p, first_p, node_p);
lydctx->parse_opts = prev_opts;
LY_CHECK_RET(r);
} else {
LY_CHECK_RET(lydjson_meta_attr(lydctx, attr_node));
}
return LY_SUCCESS;
}
static LY_ERR
lydjson_parse_any(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
struct lyd_node *parent, struct lyd_node **first_p, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
{
LY_ERR r, rc = LY_SUCCESS;
uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
struct ly_in in_start;
char *val = NULL;
const char *end;
assert(snode->nodetype & LYD_NODE_ANY);
*node = NULL;
if (snode->nodetype == LYS_ANYXML) {
LY_CHECK_RET((*status != LYJSON_OBJECT) && (*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) &&
(*status != LYJSON_STRING) && (*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) &&
(*status != LYJSON_NULL), LY_ENOT);
} else {
LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
}
switch (*status) {
case LYJSON_OBJECT:
r = lyd_create_any(snode, NULL, NULL, 0, 1, 0, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
break;
case LYJSON_ARRAY:
in_start = *lydctx->jsonctx->in;
LY_CHECK_GOTO(rc = lydjson_data_skip(lydctx->jsonctx), cleanup);
end = lydctx->jsonctx->in->current;
while (is_jsonws(end[-1])) {
--end;
}
if (asprintf(&val, "[%.*s", (int)(end - in_start.current), in_start.current) == -1) {
LOGMEM(lydctx->jsonctx->ctx);
rc = LY_EMEM;
goto cleanup;
}
r = lyd_create_any(snode, NULL, val, LYD_NODEHINT_LIST | LYD_NODEHINT_LEAFLIST, 1, 0, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
val = NULL;
break;
case LYJSON_STRING:
if (lydctx->jsonctx->dynamic) {
LY_CHECK_GOTO(rc = lyd_create_any(snode, NULL, lydctx->jsonctx->value,
LYD_VALHINT_STRING | LYD_VALHINT_NUM64, 1, 0, node), cleanup);
lydctx->jsonctx->dynamic = 0;
} else {
val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup);
r = lyd_create_any(snode, NULL, val, LYD_VALHINT_STRING | LYD_VALHINT_NUM64, 1, 0, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
val = NULL;
}
break;
case LYJSON_NUMBER:
case LYJSON_FALSE:
case LYJSON_TRUE:
assert(!lydctx->jsonctx->dynamic);
val = strndup(lydctx->jsonctx->value, lydctx->jsonctx->value_len);
LY_CHECK_ERR_GOTO(!val, LOGMEM(lydctx->jsonctx->ctx); rc = LY_EMEM, cleanup);
r = lyd_create_any(snode, NULL, val, (*status == LYJSON_NUMBER) ? LYD_VALHINT_DECNUM : LYD_VALHINT_BOOLEAN, 1, 0, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
val = NULL;
break;
case LYJSON_NULL:
r = lyd_create_any(snode, NULL, NULL, LYD_VALHINT_EMPTY, 1, 0, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
break;
default:
LOGINT(lydctx->jsonctx->ctx);
rc = LY_EINT;
goto cleanup;
}
if (ext) {
(*node)->flags |= LYD_EXT;
}
r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (*status == LYJSON_OBJECT) {
if (lydctx->parse_opts & LYD_PARSE_ANYDATA_STRICT) {
lydctx->parse_opts |= LYD_PARSE_STRICT;
} else {
lydctx->parse_opts &= ~LYD_PARSE_STRICT;
lydctx->parse_opts |= LYD_PARSE_OPAQ;
}
lydctx->parse_opts |= (ext ? LYD_PARSE_ONLY : 0);
lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
lydctx->any_schema = snode;
do {
r = lydjson_subtree_r(lydctx, *node, &((struct lyd_node_any *)*node)->child, NULL);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
*status = lyjson_ctx_status(lydctx->jsonctx);
} while (*status == LYJSON_OBJECT_NEXT);
r = lydjson_metadata_finish(lydctx, &((struct lyd_node_any *)*node)->child);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
cleanup:
lydctx->parse_opts = prev_parse_opts;
lydctx->int_opts = prev_int_opts;
lydctx->any_schema = NULL;
free(val);
return rc;
}
static LY_ERR
lydjson_parse_instance_inner(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
struct lyd_node *parent, struct lyd_node **first_p, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
{
LY_ERR r, rc = LY_SUCCESS;
uint32_t prev_parse_opts = lydctx->parse_opts;
LY_CHECK_RET(*status != LYJSON_OBJECT, LY_ENOT);
LY_CHECK_RET(lyd_create_inner(snode, node));
if (ext) {
(*node)->flags |= LYD_EXT;
}
r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (ext) {
lydctx->parse_opts |= LYD_PARSE_ONLY;
}
do {
r = lydjson_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
*status = lyjson_ctx_status(lydctx->jsonctx);
} while (*status == LYJSON_OBJECT_NEXT);
r = lydjson_metadata_finish(lydctx, lyd_node_child_p(*node));
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (snode->nodetype == LYS_LIST) {
r = lyd_parser_check_keys(*node);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) {
r = lyd_parser_validate_new_implicit((struct lyd_ctx *)lydctx, *node);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
cleanup:
lydctx->parse_opts = prev_parse_opts;
if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
lyd_parser_node_free(first_p, node);
}
return rc;
}
static LY_ERR
lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
const struct lysc_node *snode, const struct lysc_ext_instance *ext, const char *name, uint32_t name_len,
const char *prefix, uint32_t prefix_len, enum LYJSON_PARSER_STATUS *status, struct lyd_node **node)
{
LY_ERR r, rc = LY_SUCCESS;
uint32_t type_hints = 0;
r = lydjson_data_check_opaq(lydctx, snode, &type_hints);
if (r == LY_SUCCESS) {
assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
if ((lydctx->parse_opts & LYD_PARSE_JSON_NULL) && (*status == LYJSON_NULL)) {
goto cleanup;
} else if (snode->nodetype & LYD_NODE_TERM) {
if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
(*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) {
rc = LY_ENOT;
goto cleanup;
}
r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, parent, lydctx->jsonctx->value,
(uint64_t)lydctx->jsonctx->value_len * 8, &lydctx->jsonctx->dynamic, LY_VALUE_JSON, NULL,
type_hints, node);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
if (ext) {
(*node)->flags |= LYD_EXT;
}
r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, *node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (*status == LYJSON_ARRAY) {
r = lyjson_ctx_next(lydctx->jsonctx, status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
assert(*status == LYJSON_NULL);
r = lyjson_ctx_next(lydctx->jsonctx, status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
assert(*status == LYJSON_ARRAY_CLOSED);
}
} else if (snode->nodetype & LYD_NODE_INNER) {
r = lydjson_parse_instance_inner(lydctx, snode, ext, parent, first_p, status, node);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
} else {
r = lydjson_parse_any(lydctx, snode, ext, parent, first_p, status, node);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
LY_CHECK_GOTO(!*node, cleanup);
r = lyd_parser_set_data_flags(*node, &(*node)->meta, (struct lyd_ctx *)lydctx, ext);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
} else if (r == LY_ENOT) {
r = lydjson_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, status, status, first_p, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (snode->nodetype == LYS_LIST) {
((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LIST;
} else if (snode->nodetype == LYS_LEAFLIST) {
((struct lyd_node_opaq *)*node)->hints |= LYD_NODEHINT_LEAFLIST;
}
} else {
rc = r;
goto cleanup;
}
cleanup:
return rc;
}
static LY_ERR
lydjson_subtree_r(struct lyd_json_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
{
LY_ERR r, rc = LY_SUCCESS;
enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(lydctx->jsonctx);
const char *name, *prefix = NULL, *expected = NULL;
uint32_t name_len, prefix_len = 0;
ly_bool is_meta = 0;
const struct lysc_node *snode = NULL;
struct lysc_ext_instance *ext = NULL;
struct lyd_node *node = NULL, *attr_node = NULL;
const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
char *value = NULL;
assert(parent || first_p);
assert((status == LYJSON_OBJECT) || (status == LYJSON_OBJECT_NEXT));
r = lyjson_ctx_next(lydctx->jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (status == LYJSON_OBJECT_CLOSED) {
goto cleanup;
}
assert(status == LYJSON_OBJECT_NAME);
lydjson_parse_name(lydctx->jsonctx->value, lydctx->jsonctx->value_len, &name, &name_len, &prefix, &prefix_len, &is_meta);
lyjson_ctx_give_dynamic_value(lydctx->jsonctx, &value);
if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && !is_meta && name_len && !prefix_len &&
!ly_strncmp("eventTime", name, name_len)) {
r = lyjson_ctx_next(lydctx->jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (status != LYJSON_STRING) {
LOGVAL(lydctx->jsonctx->ctx, NULL, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.",
lyjson_token2str(LYJSON_STRING), lyjson_token2str(status));
rc = LY_EVALID;
goto cleanup;
}
r = lyd_create_opaq(lydctx->jsonctx->ctx, name, name_len, prefix, prefix_len, prefix, prefix_len,
lydctx->jsonctx->value, lydctx->jsonctx->value_len, NULL, LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, &node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
r = lyd_parser_node_insert(parent, first_p, NULL, lydctx->parse_opts, node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
r = lyd_parser_notif_eventtime_validate(node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
goto node_parsed;
} else if (!is_meta || name_len || prefix_len) {
r = lydjson_get_snode(lydctx, is_meta, prefix, prefix_len, name, name_len, parent, &snode, &ext);
if (r == LY_ENOT) {
goto cleanup;
} else if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
rc = r;
if ((r = lydjson_data_skip(lydctx->jsonctx))) {
rc = r;
}
r = lyjson_ctx_next(lydctx->jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
goto cleanup;
} else if (r) {
rc = r;
goto cleanup;
}
}
if (is_meta) {
if (!name_len && !prefix_len && !parent) {
LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON,
"Invalid metadata format - \"@\" can be used only inside anydata, container or list entries.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
} else if (!name_len && !prefix_len) {
attr_node = parent;
snode = attr_node->schema;
}
r = lydjson_parse_attribute(lydctx, attr_node, snode, name, name_len, prefix, prefix_len, parent, &status,
first_p, &node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
} else if (!snode) {
if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
r = lydjson_data_skip(lydctx->jsonctx);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
} else {
if (name_len == 0) {
LOGVAL(lydctx->jsonctx->ctx, parent, LYVE_SYNTAX_JSON, "JSON object member name cannot be a zero-length string.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
r = lydjson_ctx_next_parse_opaq(lydctx, name, name_len, prefix, prefix_len, parent, &status, first_p, &node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
} else {
r = lyjson_ctx_next(lydctx->jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
switch (snode->nodetype) {
case LYS_LEAFLIST:
expected = "name/array of values";
break;
case LYS_LIST:
expected = "name/array of objects";
break;
case LYS_LEAF:
if (status == LYJSON_ARRAY) {
expected = "name/[null]";
} else {
expected = "name/value";
}
break;
case LYS_CONTAINER:
case LYS_NOTIF:
case LYS_ACTION:
case LYS_RPC:
case LYS_ANYDATA:
expected = "name/object";
break;
case LYS_ANYXML:
if (status == LYJSON_ARRAY) {
expected = "name/array";
} else {
expected = "name/value";
}
break;
}
switch (snode->nodetype) {
case LYS_LEAFLIST:
case LYS_LIST:
LY_CHECK_GOTO(status != LYJSON_ARRAY, representation_error);
do {
r = lyjson_ctx_next(lydctx->jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (status == LYJSON_ARRAY_CLOSED) {
break;
}
r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
&status, &node);
if (r == LY_ENOT) {
goto representation_error;
}
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
r = lyjson_ctx_next(lydctx->jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
} while (status == LYJSON_ARRAY_NEXT);
break;
case LYS_LEAF:
case LYS_CONTAINER:
case LYS_NOTIF:
case LYS_ACTION:
case LYS_RPC:
case LYS_ANYDATA:
case LYS_ANYXML:
r = lydjson_parse_instance(lydctx, parent, first_p, snode, ext, name, name_len, prefix, prefix_len,
&status, &node);
if (r == LY_ENOT) {
goto representation_error;
}
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
lydctx->op_node = node;
}
break;
}
}
node_parsed:
if (parsed && node) {
ly_set_add(parsed, node, 1, NULL);
}
r = lyjson_ctx_next(lydctx->jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
goto cleanup;
representation_error:
LOGVAL(ctx, parent, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as name/%s.",
expected, lys_nodetype2str(snode->nodetype), snode->name, lyjson_token2str(status));
rc = LY_EVALID;
if (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) {
if ((r = lydjson_data_skip(lydctx->jsonctx))) {
rc = r;
}
}
cleanup:
free(value);
if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
lyd_parser_node_free(first_p, &node);
}
return rc;
}
static LY_ERR
lyd_parse_json_init(const struct ly_ctx *ctx, const struct lysc_node *schema, struct ly_in *in, uint32_t parse_opts,
uint32_t val_opts, enum LYJSON_PARSER_STATUS *status_p, struct lyd_json_ctx **lydctx_p)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_json_ctx *lydctx;
enum LYJSON_PARSER_STATUS status;
assert(lydctx_p);
lydctx = calloc(1, sizeof *lydctx);
LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
lydctx->parse_opts = parse_opts;
lydctx->val_opts = val_opts;
lydctx->free = lyd_json_ctx_free;
LY_CHECK_ERR_RET(ret = lyjson_ctx_new(ctx, in, &lydctx->jsonctx), free(lydctx), ret);
status = lyjson_ctx_status(lydctx->jsonctx);
if (schema &&
(((status == LYJSON_ARRAY) && (schema->nodetype & LYS_LEAFLIST)) ||
(((status == LYJSON_NUMBER) || (status == LYJSON_STRING) || (status == LYJSON_FALSE) ||
(status == LYJSON_TRUE) || (status == LYJSON_NULL) || (status == LYJSON_ARRAY)) && (schema->nodetype & LYS_LEAF)))) {
} else if (status == LYJSON_OBJECT) {
} else {
LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON, "Expected top-level JSON object or correct bare value, but %s found.",
lyjson_token2str(status));
*lydctx_p = NULL;
lyd_json_ctx_free((struct lyd_ctx *)lydctx);
return LY_EVALID;
}
*lydctx_p = lydctx;
if (status_p) {
*status_p = status;
}
return LY_SUCCESS;
}
static LY_ERR
lyd_parse_json_bare_value(struct lyd_json_ctx *lydctx, enum LYJSON_PARSER_STATUS *status, struct lyd_node *parent,
const struct lysc_node *schema, struct lyd_node **first_p)
{
LY_ERR r, rc = LY_SUCCESS;
const struct ly_ctx *ctx = lydctx->jsonctx->ctx;
struct lyd_node *node = NULL;
const char *expected = NULL;
assert(schema);
switch (schema->nodetype) {
case LYS_LEAFLIST:
expected = "array of values";
break;
case LYS_LEAF:
if (*status == LYJSON_ARRAY) {
expected = "[null]";
} else {
expected = "value";
}
break;
}
switch (schema->nodetype) {
case LYS_LEAFLIST:
LY_CHECK_GOTO(*status != LYJSON_ARRAY, representation_error);
do {
r = lyjson_ctx_next(lydctx->jsonctx, status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (*status == LYJSON_ARRAY_CLOSED) {
break;
}
r = lydjson_parse_instance(lydctx, parent, first_p, schema, NULL, schema->name, strlen(schema->name),
NULL, 0, status, &node);
if (r == LY_ENOT) {
goto representation_error;
}
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
r = lyjson_ctx_next(lydctx->jsonctx, status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
} while (*status == LYJSON_ARRAY_NEXT);
break;
case LYS_LEAF:
r = lydjson_parse_instance(lydctx, parent, first_p, schema, NULL, schema->name, strlen(schema->name),
NULL, 0, status, &node);
if (r == LY_ENOT) {
goto representation_error;
}
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
break;
}
goto cleanup;
representation_error:
LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s \"%s\" is represented in input data as %s.",
expected, lys_nodetype2str(schema->nodetype), schema->name, lyjson_token2str(*status));
rc = LY_EVALID;
cleanup:
if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
lyd_parser_node_free(first_p, &node);
}
return rc;
}
LY_ERR
lyd_parse_json(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lysc_node *schema,
struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
struct ly_set *parsed, struct lyd_ctx **lydctx_p)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyd_json_ctx *lydctx = NULL;
enum LYJSON_PARSER_STATUS status = LYJSON_ERROR;
rc = lyd_parse_json_init(ctx, schema, in, parse_opts, val_opts, &status, &lydctx);
LY_CHECK_GOTO(rc, cleanup);
lydctx->int_opts = int_opts;
LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
if (status != LYJSON_OBJECT) {
r = lyd_parse_json_bare_value(lydctx, &status, parent, schema, first_p);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
} else {
do {
r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
status = lyjson_ctx_status(lydctx->jsonctx);
if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
break;
}
} while (status == LYJSON_OBJECT_NEXT);
}
if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lydctx->jsonctx->in->current[0] && (status != LYJSON_OBJECT_CLOSED)) {
LOGVAL(ctx, NULL, LYVE_SYNTAX, "Unexpected sibling node.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
LOGVAL(ctx, NULL, LYVE_DATA, "Missing the operation node.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
cleanup:
assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
!lydctx->node_when.count));
if (rc && (!lydctx || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
lyd_json_ctx_free((struct lyd_ctx *)lydctx);
} else {
lyjson_ctx_free(lydctx->jsonctx);
lydctx->jsonctx = NULL;
if (lydctx_p) {
*lydctx_p = (struct lyd_ctx *)lydctx;
} else {
lyd_json_ctx_free((struct lyd_ctx *)lydctx);
}
}
return rc;
}
static LY_ERR
lydjson_envelope(struct lyjson_ctx *jsonctx, const char *name, const char *module, struct lyd_node **envp)
{
LY_ERR rc = LY_SUCCESS, r;
enum LYJSON_PARSER_STATUS status = lyjson_ctx_status(jsonctx);
const char *nam, *prefix;
uint32_t nam_len, prefix_len;
ly_bool is_meta;
assert(status == LYJSON_OBJECT);
*envp = NULL;
r = lyjson_ctx_next(jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
if (status == LYJSON_OBJECT_CLOSED) {
LOGVAL(jsonctx->ctx, NULL, LYVE_SYNTAX, "Empty JSON object.");
rc = LY_EVALID;
goto cleanup;
}
assert(status == LYJSON_OBJECT_NAME);
lydjson_parse_name(jsonctx->value, jsonctx->value_len, &nam, &nam_len, &prefix, &prefix_len, &is_meta);
if (is_meta) {
LOGVAL(jsonctx->ctx, NULL, LYVE_DATA, "Unexpected metadata.");
rc = LY_EVALID;
goto cleanup;
} else if (module && ly_strncmp(module, prefix, prefix_len)) {
LOGVAL(jsonctx->ctx, NULL, LYVE_DATA, "Unexpected module \"%.*s\" instead of \"%s\".", (int)prefix_len, prefix,
module);
rc = LY_EVALID;
goto cleanup;
} else if (ly_strncmp(name, nam, nam_len)) {
LOGVAL(jsonctx->ctx, NULL, LYVE_DATA, "Unexpected object \"%.*s\" instead of \"%s\".", (int)nam_len, nam, name);
rc = LY_EVALID;
goto cleanup;
}
r = lyjson_ctx_next(jsonctx, &status);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
rc = lyd_create_opaq(jsonctx->ctx, name, strlen(name), prefix, prefix_len, prefix, prefix_len, NULL, 0, NULL,
LY_VALUE_JSON, NULL, LYD_VALHINT_STRING, envp);
LY_CHECK_GOTO(rc, cleanup);
cleanup:
if (rc) {
lyd_free_tree(*envp);
*envp = NULL;
}
return rc;
}
LY_ERR
lyd_parse_json_restconf(const struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_in *in,
uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, struct lyd_node **envp, struct ly_set *parsed,
struct lyd_ctx **lydctx_p)
{
LY_ERR rc = LY_SUCCESS, r;
struct lyd_json_ctx *lydctx = NULL;
struct lyd_node *node;
uint32_t i, int_opts = 0, close_elem = 0;
assert(ctx && in && lydctx_p);
assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
assert((data_type == LYD_TYPE_RPC_RESTCONF) || (data_type == LYD_TYPE_NOTIF_RESTCONF) ||
(data_type == LYD_TYPE_REPLY_RESTCONF));
rc = lyd_parse_json_init(ctx, NULL, in, parse_opts, val_opts, NULL, &lydctx);
LY_CHECK_GOTO(rc, cleanup);
switch (data_type) {
case LYD_TYPE_RPC_RESTCONF:
assert(parent);
rc = lydjson_envelope(lydctx->jsonctx, "input", lyd_node_module(parent)->name, envp);
LY_CHECK_GOTO(rc, cleanup);
int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
close_elem = 1;
break;
case LYD_TYPE_NOTIF_RESTCONF:
assert(!parent);
rc = lydjson_envelope(lydctx->jsonctx, "notification", "ietf-restconf", envp);
LY_CHECK_GOTO(rc, cleanup);
int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;
close_elem = 1;
break;
case LYD_TYPE_REPLY_RESTCONF:
assert(parent);
rc = lydjson_envelope(lydctx->jsonctx, "output", lyd_node_module(parent)->name, envp);
LY_CHECK_GOTO(rc, cleanup);
int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
close_elem = 1;
break;
default:
LOGINT(ctx);
rc = LY_EINT;
goto cleanup;
}
lydctx->int_opts = int_opts;
LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
do {
r = lydjson_subtree_r(lydctx, parent, first_p, parsed);
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
} while (lyjson_ctx_status(lydctx->jsonctx) == LYJSON_OBJECT_NEXT);
for (i = 0; i < close_elem; ++i) {
if (lyjson_ctx_status(lydctx->jsonctx) != LYJSON_OBJECT_CLOSED) {
LOGVAL(ctx, NULL, LYVE_SYNTAX_JSON, "Expecting JSON %s but %s found.", lyjson_token2str(LYJSON_OBJECT_CLOSED),
lyjson_token2str(lyjson_ctx_status(lydctx->jsonctx)));
rc = LY_EVALID;
goto cleanup;
}
r = lyjson_ctx_next(lydctx->jsonctx, NULL);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
LOGVAL(ctx, NULL, LYVE_DATA, "Missing the operation node.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
}
if (int_opts & LYD_INTOPT_EVENTTIME) {
node = (*first_p)->prev;
if (node->schema) {
LOGVAL(ctx, NULL, LYVE_DATA, "Missing notification \"eventTime\" node.");
r = LY_EVALID;
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
} else {
assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
lyd_unlink(node);
assert(*envp);
lyd_insert_child(*envp, node);
}
}
r = lydjson_metadata_finish(lydctx, parent ? lyd_node_child_p(parent) : first_p);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
cleanup:
assert(!(parse_opts & LYD_PARSE_ONLY) || !lydctx || (!lydctx->node_types.count && !lydctx->meta_types.count &&
!lydctx->node_when.count));
if (rc) {
lyd_json_ctx_free((struct lyd_ctx *)lydctx);
} else {
*lydctx_p = (struct lyd_ctx *)lydctx;
lyjson_ctx_free(lydctx->jsonctx);
lydctx->jsonctx = NULL;
}
return rc;
}