Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/printer_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,16 @@ json_nscmp(const struct lyd_node *node1, const struct lysc_node *snode1, const s
}

/**
* @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
* @brief Print the @p text with JSON special characters encoded, without quotation.
*
* @param[in] out The output handler.
* @param[in] text The string to print.
* @return LY_ERR value.
*/
static LY_ERR
json_print_string(struct ly_out *out, const char *text)
static void
json_print_escaped(struct ly_out *out, const char *text)
{
uint64_t i;

if (!text) {
return LY_SUCCESS;
}

ly_write_(out, "\"", 1);
for (i = 0; text[i]; i++) {
const unsigned char byte = text[i];

Expand Down Expand Up @@ -280,6 +274,24 @@ json_print_string(struct ly_out *out, const char *text)
break;
}
}
}

/**
* @brief Print the @p text as JSON string - encode special characters and add quotation around the string.
*
* @param[in] out The output handler.
* @param[in] text The string to print.
* @return LY_ERR value.
*/
static LY_ERR
json_print_string(struct ly_out *out, const char *text)
{
if (!text) {
return LY_SUCCESS;
}

ly_write_(out, "\"", 1);
json_print_escaped(out, text);
ly_write_(out, "\"", 1);

return LY_SUCCESS;
Expand Down Expand Up @@ -367,11 +379,14 @@ json_print_member2(struct jsonpr_ctx *pctx, const struct lyd_node *parent, LY_VA

/* print the member, strcmp because node prefix is in the schema dict, module_name in data dict */
node_prefix(parent, parent ? parent->schema : NULL, &pmod_name, NULL);
ly_print_(pctx->out, "%*s\"%s", INDENT, is_attr ? "@" : "");
if (module_name && (!parent || !pmod_name || strcmp(pmod_name, module_name))) {
ly_print_(pctx->out, "%*s\"%s%s:%s\":%s", INDENT, is_attr ? "@" : "", module_name, name_str, DO_FORMAT ? " " : "");
} else {
ly_print_(pctx->out, "%*s\"%s%s\":%s", INDENT, is_attr ? "@" : "", name_str, DO_FORMAT ? " " : "");
/* opaque names come from the input and may need encoding */
json_print_escaped(pctx->out, module_name);
ly_write_(pctx->out, ":", 1);
}
json_print_escaped(pctx->out, name_str);
ly_print_(pctx->out, "\":%s", DO_FORMAT ? " " : "");

return LY_SUCCESS;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/utests/data/test_parser_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ test_anydata(void **state)
CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);

/* member names that need encoding */
data = "{\"a:any\":{\"x\\\":\\\"v\\\"},\\\"a:foo\\\":\\\"str\":\"y\",\"m\\\\d:n\\\"\":\"z\"}}";
CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree);
assert_non_null(tree);
tree = tree->next;
CHECK_LYD_STRING(tree, LYD_PRINT_SHRINK | LYD_PRINT_SIBLINGS, data);
lyd_free_all(tree);

data = "{\"a:any\": null}";
PARSER_CHECK_ERROR(data, 0, LYD_VALIDATE_PRESENT, tree, LY_EVALID, "Expecting JSON name/object but anydata \"any\" is represented in input data as name/null.", NULL, 1);
CHECK_PARSE_LYD(data, LYD_PARSE_JSON_NULL, LYD_VALIDATE_PRESENT, tree);
Expand Down
Loading