JSON parser/printer BUGFIX anyxml's JSON representation differs from anydata
distinguish between anyxml and anydata in JSON - anyxml allows to have
any XML represented and any JSON node/value pair, anydata allows only the
data that are modeled by a YANG modules despite they are unknown.
anyxml JSON representation is too generic, so we support only the string
value of the anyxml node. Otherwise we are not able to parse it into the
internal libyang structures.
Fixes #200
diff --git a/src/printer_json.c b/src/printer_json.c
index 938523a..ca0c0a5 100644
--- a/src/printer_json.c
+++ b/src/printer_json.c
@@ -307,6 +307,66 @@
}
static void
+json_print_anyxml(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
+{
+ struct lyd_node_anydata *any = (struct lyd_node_anydata *)node;
+ int isobject = 0;
+ char *buf;
+ const char *schema = NULL;
+
+ if (toplevel || !node->parent || nscmp(node, node->parent)) {
+ /* print "namespace" */
+ schema = lys_node_module(node->schema)->name;
+ ly_print(out, "%*s\"%s:%s\":%s", LEVEL, INDENT, schema, node->schema->name, (level ? " " : ""));
+ } else {
+ ly_print(out, "%*s\"%s\":%s", LEVEL, INDENT, node->schema->name, (level ? " " : ""));
+ }
+ if (level) {
+ level++;
+ }
+
+ switch (any->value_type) {
+ case LYD_ANYDATA_DATATREE:
+ isobject = 1;
+ ly_print(out, level ? "{\n" : "{");
+ json_print_nodes(out, level, any->value.tree, 1, 0, options);
+ break;
+ case LYD_ANYDATA_JSON:
+ isobject = 1;
+ ly_print(out, level ? "{\n" : "{");
+ if (any->value.str) {
+ ly_print(out, "%*s%s%s", LEVEL, INDENT, any->value.str, level ? "\n" : "");
+ }
+ break;
+ case LYD_ANYDATA_XML:
+ lyxml_print_mem(&buf, any->value.xml, (level ? LYXML_PRINT_FORMAT | LYXML_PRINT_NO_LAST_NEWLINE : 0)
+ | LYXML_PRINT_SIBLINGS);
+ json_print_string(out, buf);
+ free(buf);
+ break;
+ case LYD_ANYDATA_CONSTSTRING:
+ case LYD_ANYDATA_SXML:
+ if (any->value.str) {
+ json_print_string(out, any->value.str);
+ } else {
+ ly_print(out, "\"\"");
+ }
+ break;
+ default:
+ /* other formats are not supported */
+ LOGWRN("Unable to print anydata content (type %d) as JSON.", any->value_type);
+ break;
+ }
+
+ if (level) {
+ level--;
+ }
+ if (isobject) {
+ ly_print(out, "%*s}", LEVEL, INDENT);
+ }
+}
+
+static void
json_print_anydata(struct lyout *out, int level, const struct lyd_node *node, int toplevel, int options)
{
const char *schema = NULL;
@@ -407,6 +467,12 @@
}
break;
case LYS_ANYXML:
+ if (node->prev->next) {
+ /* print the previous comma */
+ ly_print(out, ",%s", (level ? "\n" : ""));
+ }
+ json_print_anyxml(out, level, node, toplevel, options);
+ break;
case LYS_ANYDATA:
if (node->prev->next) {
/* print the previous comma */