printer & parser json BUGFIX anyxml node
Anyxml was processed exactly as anydata,
which is completely wrong and is more
similar to leaf.
Refs #1762
diff --git a/src/printer_json.c b/src/printer_json.c
index 6b2ee02..7c251a1 100644
--- a/src/printer_json.c
+++ b/src/printer_json.c
@@ -518,21 +518,27 @@
}
/**
- * @brief Print anydata data node including its metadata.
+ * @brief Print anydata/anyxml content.
*
* @param[in] ctx JSON printer context.
* @param[in] any Anydata node to print.
* @return LY_ERR value.
*/
static LY_ERR
-json_print_anydata(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
+json_print_any_content(struct jsonpr_ctx *ctx, struct lyd_node_any *any)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_node *iter;
uint32_t prev_opts, prev_lo;
+ /* anyxml - printed as name/value pair;
+ * anydata - printed as an object */
+
if (!any->value.tree) {
/* no content */
+ if (any->schema->nodetype == LYS_ANYXML) {
+ ly_print_(ctx->out, "null");
+ }
return LY_SUCCESS;
}
@@ -556,6 +562,11 @@
switch (any->value_type) {
case LYD_ANYDATA_DATATREE:
+ if (any->schema->nodetype == LYS_ANYXML) {
+ /* print always as a string */
+ ly_print_(ctx->out, "\"%s", DO_FORMAT ? "\n" : "");
+ }
+
/* close opening tag and print data */
prev_opts = ctx->options;
ctx->options &= ~LYD_PRINT_WITHSIBLINGS;
@@ -566,28 +577,42 @@
}
ctx->options = prev_opts;
+
+ if (any->schema->nodetype == LYS_ANYXML) {
+ /* terminate the string */
+ ly_print_(ctx->out, "\"");
+ }
break;
case LYD_ANYDATA_JSON:
/* print without escaping special characters */
- if (!any->value.str[0]) {
- return LY_SUCCESS;
+ if (any->schema->nodetype == LYS_ANYXML) {
+ /* print as a string */
+ ly_print_(ctx->out, "\"%s\"", any->value.str);
+ } else if (any->value.str[0]) {
+ /* print with indent */
+ ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
}
- ly_print_(ctx->out, "%*s%s", INDENT, any->value.str);
break;
case LYD_ANYDATA_STRING:
case LYD_ANYDATA_XML:
+ if (any->schema->nodetype == LYS_ANYXML) {
+ /* print as a string */
+ ly_print_(ctx->out, "\"%s\"", any->value.str);
+ break;
+ }
+ /* fallthrough */
case LYD_ANYDATA_LYB:
/* JSON and LYB format is not supported */
LOGWRN(ctx->ctx, "Unable to print anydata content (type %d) as XML.", any->value_type);
- return LY_SUCCESS;
+ break;
}
return LY_SUCCESS;
}
/**
- * @brief Print content of a single container/list data node including its metadata.
- * The envelope specific to container and list are expected to be printed by the caller.
+ * @brief Print content of a single container/list/anydata data node including its metadata.
+ * The envelope specific to nodes are expected to be printed by the caller.
*
* @param[in] ctx JSON printer context.
* @param[in] node Data node to print.
@@ -629,7 +654,7 @@
}
} else {
/* anydata */
- json_print_anydata(ctx, (struct lyd_node_any *)node);
+ json_print_any_content(ctx, (struct lyd_node_any *)node);
}
LEVEL_DEC;
@@ -660,6 +685,26 @@
}
/**
+ * @brief Print anyxml data node including its metadata.
+ *
+ * @param[in] ctx JSON printer context.
+ * @param[in] node Data node to print.
+ * @return LY_ERR value.
+ */
+static int
+json_print_anyxml(struct jsonpr_ctx *ctx, const struct lyd_node *node)
+{
+ LY_CHECK_RET(json_print_member(ctx, node, 0));
+ LY_CHECK_RET(json_print_any_content(ctx, (struct lyd_node_any *)node));
+ LEVEL_PRINTED;
+
+ /* print attributes as sibling */
+ json_print_attributes(ctx, node, 0);
+
+ return LY_SUCCESS;
+}
+
+/**
* @brief Check whether a node is the last printed instance of a (leaf-)list.
*
* @param[in] ctx JSON printer context.
@@ -867,6 +912,7 @@
case LYS_RPC:
case LYS_ACTION:
case LYS_NOTIF:
+ case LYS_ANYDATA:
case LYS_CONTAINER:
LY_CHECK_RET(json_print_container(ctx, node));
break;
@@ -878,8 +924,7 @@
LY_CHECK_RET(json_print_leaf_list(ctx, node));
break;
case LYS_ANYXML:
- case LYS_ANYDATA:
- LY_CHECK_RET(json_print_container(ctx, node));
+ LY_CHECK_RET(json_print_anyxml(ctx, node));
break;
default:
LOGINT(node->schema->module->ctx);