Skip to content

Commit 88e7433

Browse files
committed
printer json FEATURE no prefix for nested nodes flag
New flag LYD_PRINT_NO_JSON_NESTED_PREFIX makes it possible to only include the module name in the JSON data if it corresponds to the top-level node in the schema. This is useful if you need libyang to only print a subtree of the data which you may then wrap.
1 parent d067cbc commit 88e7433

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/printer_data.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ struct ly_out;
111111
are not explicitly present in the original data tree despite their
112112
value is equal to their default value. There is the same limitation regarding
113113
the presence of ietf-netconf-with-defaults module in libyang context. */
114+
#define LYD_PRINT_JSON_NO_NESTED_PREFIX 0x100 /**< Do not print the prefix in JSON data for nested nodes (non-top-level). By nested
115+
we mean any node that does not appear in the top-level of a corresponding
116+
schema. */
114117
/**
115118
* @}
116119
*/

src/printer_json.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ json_print_member(struct jsonpr_ctx *pctx, const struct lyd_node *node, const st
307307
}
308308

309309
PRINT_COMMA;
310-
if ((LEVEL == 1) || json_nscmp(node, snode, pctx->parent)) {
310+
if (json_nscmp(node, snode, pctx->parent)) {
311311
/* print "namespace" */
312312
node_prefix(node, snode, &pref, NULL);
313313
ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", pref, name, DO_FORMAT ? " " : "");
@@ -1174,6 +1174,10 @@ json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t option
11741174
/* content */
11751175
LY_LIST_FOR(root, node) {
11761176
pctx.root = node;
1177+
if (options & LYD_PRINT_JSON_NO_NESTED_PREFIX) {
1178+
/* update parent, if it has the same namespace it will not be printed */
1179+
pctx.parent = (const struct lyd_node *)(node->parent);
1180+
}
11771181
LY_CHECK_RET(json_print_node(&pctx, node));
11781182
if (!(options & LYD_PRINT_SIBLINGS)) {
11791183
break;

tests/utests/data/test_printer_json.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,35 @@ test_empty_leaf_list(void **state)
135135
lyd_free_all(tree);
136136
}
137137

138+
static void
139+
test_no_json_nested_prefix(void **state)
140+
{
141+
struct lyd_node *tree;
142+
char *buffer = NULL;
143+
const char *data = "{\"schema2:a\":{\"b\":{\"c\":\"dflt\"}}}";
144+
145+
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
146+
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_ALL |
147+
LYD_PRINT_JSON_NO_NESTED_PREFIX));
148+
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{\"c\":\"dflt\"}}}");
149+
free(buffer);
150+
151+
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, lyd_child(tree), LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_ALL |
152+
LYD_PRINT_JSON_NO_NESTED_PREFIX));
153+
CHECK_STRING(buffer, "{\"b\":{\"c\":\"dflt\"}}");
154+
free(buffer);
155+
156+
lyd_free_all(tree);
157+
}
158+
138159
int
139160
main(void)
140161
{
141162
const struct CMUnitTest tests[] = {
142163
UTEST(test_container_presence, setup),
143164
UTEST(test_empty_container_wd_trim, setup),
144165
UTEST(test_empty_leaf_list, setup),
166+
UTEST(test_no_json_nested_prefix, setup),
145167
};
146168

147169
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)