Skip to content

Commit 68f6eaf

Browse files
authored
parser json UPDATE allowing JSON 'null' value for inner nodes (#2218)
1 parent f6ad0b6 commit 68f6eaf

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/parser_json.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,11 +1491,11 @@ lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, str
14911491
r = lydjson_data_check_opaq(lydctx, snode, &type_hints);
14921492
if (r == LY_SUCCESS) {
14931493
assert(snode->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER | LYD_NODE_ANY));
1494-
if (snode->nodetype & LYD_NODE_TERM) {
1495-
if ((lydctx->parse_opts & LYD_PARSE_JSON_NULL) && (*status == LYJSON_NULL)) {
1496-
/* do not do anything if value is JSON 'null' */
1497-
goto cleanup;
1498-
} else if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
1494+
if ((lydctx->parse_opts & LYD_PARSE_JSON_NULL) && (*status == LYJSON_NULL)) {
1495+
/* do not do anything if value is JSON 'null' */
1496+
goto cleanup;
1497+
} else if (snode->nodetype & LYD_NODE_TERM) {
1498+
if ((*status != LYJSON_ARRAY) && (*status != LYJSON_NUMBER) && (*status != LYJSON_STRING) &&
14991499
(*status != LYJSON_FALSE) && (*status != LYJSON_TRUE) && (*status != LYJSON_NULL)) {
15001500
rc = LY_ENOT;
15011501
goto cleanup;
@@ -1522,10 +1522,6 @@ lydjson_parse_instance(struct lyd_json_ctx *lydctx, struct lyd_node *parent, str
15221522
r = lydjson_parse_instance_inner(lydctx, snode, ext, status, node);
15231523
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
15241524
} else {
1525-
if ((lydctx->parse_opts & LYD_PARSE_JSON_NULL) && (*status == LYJSON_NULL)) {
1526-
/* do not do anything if value is JSON 'null' */
1527-
goto cleanup;
1528-
}
15291525
/* create any node */
15301526
r = lydjson_parse_any(lydctx, snode, ext, status, node);
15311527
LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);

tests/utests/data/test_parser_json.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ test_container(void **state)
508508
CHECK_PARSE_LYD("{\"a:unknown\":{\"a\":\"val\",\"b\":5}}", 0, LYD_VALIDATE_PRESENT, tree);
509509
CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS, "{}");
510510
lyd_free_all(tree);
511+
512+
data = "{\"a:c\": null}";
513+
PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, "Expecting JSON name/object but container \"c\" is represented in input data as name/null.", NULL, 1);
514+
CHECK_PARSE_LYD(data, LYD_PARSE_JSON_NULL, LYD_VALIDATE_PRESENT, tree);
515+
assert_null(tree);
511516
}
512517

513518
static void

0 commit comments

Comments
 (0)