diff --git a/CHANGELOG.md b/CHANGELOG.md
index bae8d056..06369202 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -49,6 +49,13 @@ Thanks netgate for providing the dispatcher code!
Users may have to change how they access the system
+* XML to JSON CDATA translation is NOT stripped
+ * Example, assume XML: ` x & x < y ]]>`
+ * Previous bevavior:
+ * JSON: {"s":" z > x & x < y "}
+ * New behavior:
+ * JSON: `{"s":" x & x < y ]]>"}`
+ * To keep old behavior, set `JSON_CDATA_STRIP` in clixon_custom.h
* New `clixon-config@2021-11-11.yang` revision
* Modified options:
* CLICON_CLI_GENMODEL_TYPE: added OC_COMPRESS enum
diff --git a/include/clixon_custom.h b/include/clixon_custom.h
index 779df211..b73d0004 100644
--- a/include/clixon_custom.h
+++ b/include/clixon_custom.h
@@ -117,3 +117,14 @@
* As defined in draft-wwlh-netconf-list-pagination-00 using Yang metadata value [RFC7952]
*/
#undef LIST_PAGINATION_REMAINING
+
+/*! XML CDATA encoding in payload is stripped when translating to JSON
+ * Example: XML: " x & x < y ]]>" is translated to
+ * JSON: {"s":" z > x & x < y "}
+ * If not set:
+ * JSON: {"s":" x & x < y ]]>"}`
+ * Clixon stripped CDATA in 5.3 and earlier versions.
+ * To keep this backward-compatible enable this option
+ * Consider removing this option after 5.4
+ */
+#undef JSON_CDATA_STRIP
diff --git a/lib/src/clixon_json.c b/lib/src/clixon_json.c
index 6242e4b0..9c87d9ef 100644
--- a/lib/src/clixon_json.c
+++ b/lib/src/clixon_json.c
@@ -256,9 +256,11 @@ json_str_escape_cdata(cbuf *cb,
{
int retval = -1;
int i;
- int esc = 0; /* cdata escape */
size_t len;
-
+#ifdef JSON_CDATA_STRIP
+ int esc = 0; /* cdata escape */
+#endif
+
len = strlen(str);
for (i=0; i1799"
-# XXX CDATA translation, should work but does not
-if false; then
-JSON='{"json:c": {"s": " x & x < y ]]>"}}'
+JSON='{"json:c":{"s":" x & x < y ]]>"}}'
new "json parse cdata xml"
expecteofx "$clixon_util_json -j -y $fyang" 0 "$JSON" "$JSON"
-fi
rm -rf $dir
diff --git a/test/test_xml.sh b/test/test_xml.sh
index 65d76646..bdf3bfa4 100755
--- a/test/test_xml.sh
+++ b/test/test_xml.sh
@@ -2,11 +2,16 @@
# Test: XML parser tests and JSON translation
# @see https://www.w3.org/TR/2008/REC-xml-20081126
# https://www.w3.org/TR/2009/REC-xml-names-20091208
-#PROG="valgrind --leak-check=full --show-leak-kinds=all ../util/clixon_util_xml"
+# Note CDATA to JSON: in earlier versions, CDATA was stripped when converting to JSON
+# but this has been changed so that the CDATA is a part of the payload, eg shows up also in
+# JSON strings
+
# Magic line must be first in script (see README.md)
+
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
: ${clixon_util_xml:="clixon_util_xml"}
+: ${clixon_util_json:="clixon_util_json"}
new "xml parse"
expecteof "$clixon_util_xml -o" 0 "" "^$"
@@ -42,10 +47,10 @@ new "xml CDATA right square bracket: ]"
expecteofx "$clixon_util_xml -o" 0 "" ""
new "xml simple CDATA to json"
-expecteofx "$clixon_util_xml -o -j" 0 '' '{"a":"a text"}'
-
-new "xml complex CDATA"
-XML=$(cat <' '{"a":""}'
+# Example partly from https://www.w3resource.com/xml/attribute.php
+# XML complex CDATA (with comments for debug):;
+DUMMY=$(cat <An example of escaped CENDs
y" so I guess that means that z > x ]]>
@@ -56,20 +61,41 @@ XML=$(cat < in the second CDATA block -->
]]>
+EOF
+ )
+# without comments
+XML=$(cat <An example of escaped CENDs
+ y" so I guess that means that z > x ]]>
+]]>
+]]>
+
+EOF
+ )
+XML=$(cat <<'EOF'
+An example of escaped CENDs y" so I guess that means that z > x ]]>]]>]]>
EOF
)
-expecteof "$clixon_util_xml -o" 0 "$XML" "^An example of escaped CENDs
- y\" so I guess that means that z > x ]]>
-]]>]]>$"
+new "complex CDATA xml to xml"
+expecteof "$clixon_util_xml -o" 0 "$XML" "^$XML
+$"
JSON=$(cat < y\" so I guess that means that z > x ","data":"This text contains a CEND ]]>","alternative":"This text contains a CEND ]]>"}}
+{"a":{"description":"An example of escaped CENDs","sometext":" y\" so I guess that means that z > x ]]>","data":"]]>","alternative":"]]>"}}
EOF
)
-new "xml complex CDATA to json"
+new "complex CDATA xml to json"
expecteofx "$clixon_util_xml -oj" 0 "$XML" "$JSON"
+# reverse
+new "complex CDATA json to json"
+expecteofx "$clixon_util_json -j" 0 "$JSON" "$JSON"
+
+# reverse
+new "complex CDATA json to xml"
+expecteofx "$clixon_util_json" 0 "$JSON" "$XML"
+
XML=$(cat <Less than: < , greater than: > ampersand: &
EOF