* XML to JSON CDATA translation is NOT stripped
* Example, assume XML: `<s><![CDATA[ z > x & x < y ]]></s>`
* Previous bevavior:
* JSON: {"s":" z > x & x < y "}
* New behavior:
* JSON: `{"s":"<![CDATA[ z > x & x < y ]]>"}`
* To keep old behavior, set `JSON_CDATA_STRIP` in clixon_custom.h
This commit is contained in:
parent
7976c11f11
commit
81da71ffd7
5 changed files with 61 additions and 16 deletions
|
|
@ -49,6 +49,13 @@ Thanks netgate for providing the dispatcher code!
|
||||||
|
|
||||||
Users may have to change how they access the system
|
Users may have to change how they access the system
|
||||||
|
|
||||||
|
* XML to JSON CDATA translation is NOT stripped
|
||||||
|
* Example, assume XML: `<s><![CDATA[ z > x & x < y ]]></s>`
|
||||||
|
* Previous bevavior:
|
||||||
|
* JSON: {"s":" z > x & x < y "}
|
||||||
|
* New behavior:
|
||||||
|
* JSON: `{"s":"<![CDATA[ z > x & x < y ]]>"}`
|
||||||
|
* To keep old behavior, set `JSON_CDATA_STRIP` in clixon_custom.h
|
||||||
* New `clixon-config@2021-11-11.yang` revision
|
* New `clixon-config@2021-11-11.yang` revision
|
||||||
* Modified options:
|
* Modified options:
|
||||||
* CLICON_CLI_GENMODEL_TYPE: added OC_COMPRESS enum
|
* CLICON_CLI_GENMODEL_TYPE: added OC_COMPRESS enum
|
||||||
|
|
|
||||||
|
|
@ -117,3 +117,14 @@
|
||||||
* As defined in draft-wwlh-netconf-list-pagination-00 using Yang metadata value [RFC7952]
|
* As defined in draft-wwlh-netconf-list-pagination-00 using Yang metadata value [RFC7952]
|
||||||
*/
|
*/
|
||||||
#undef LIST_PAGINATION_REMAINING
|
#undef LIST_PAGINATION_REMAINING
|
||||||
|
|
||||||
|
/*! XML CDATA encoding in payload is stripped when translating to JSON
|
||||||
|
* Example: XML: "<s><![CDATA[ z > x & x < y ]]></s>" is translated to
|
||||||
|
* JSON: {"s":" z > x & x < y "}
|
||||||
|
* If not set:
|
||||||
|
* JSON: {"s":"<![CDATA[ z > 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
|
||||||
|
|
|
||||||
|
|
@ -256,8 +256,10 @@ json_str_escape_cdata(cbuf *cb,
|
||||||
{
|
{
|
||||||
int retval = -1;
|
int retval = -1;
|
||||||
int i;
|
int i;
|
||||||
int esc = 0; /* cdata escape */
|
|
||||||
size_t len;
|
size_t len;
|
||||||
|
#ifdef JSON_CDATA_STRIP
|
||||||
|
int esc = 0; /* cdata escape */
|
||||||
|
#endif
|
||||||
|
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
for (i=0; i<len; i++)
|
for (i=0; i<len; i++)
|
||||||
|
|
@ -271,6 +273,7 @@ json_str_escape_cdata(cbuf *cb,
|
||||||
case '\\':
|
case '\\':
|
||||||
cprintf(cb, "\\\\");
|
cprintf(cb, "\\\\");
|
||||||
break;
|
break;
|
||||||
|
#ifdef JSON_CDATA_STRIP
|
||||||
case '<':
|
case '<':
|
||||||
if (!esc &&
|
if (!esc &&
|
||||||
strncmp(&str[i], "<![CDATA[", strlen("<![CDATA[")) == 0){
|
strncmp(&str[i], "<![CDATA[", strlen("<![CDATA[")) == 0){
|
||||||
|
|
@ -289,6 +292,7 @@ json_str_escape_cdata(cbuf *cb,
|
||||||
else
|
else
|
||||||
cprintf(cb, "%c", str[i]);
|
cprintf(cb, "%c", str[i]);
|
||||||
break;
|
break;
|
||||||
|
#endif /* JSON_CDATA_STRIP */
|
||||||
default: /* fall thru */
|
default: /* fall thru */
|
||||||
cprintf(cb, "%c", str[i]);
|
cprintf(cb, "%c", str[i]);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -134,12 +134,9 @@ JSON='{"data": {"a": [],"b": [{"name": 17},{"name": []},{"name": 99}]}}'
|
||||||
new "empty list followed by list again empty"
|
new "empty list followed by list again empty"
|
||||||
expecteofx "$clixon_util_json" 0 "$JSON" "<data><a/><b><name>17</name></b><b><name/></b><b><name>99</name></b></data>"
|
expecteofx "$clixon_util_json" 0 "$JSON" "<data><a/><b><name>17</name></b><b><name/></b><b><name>99</name></b></data>"
|
||||||
|
|
||||||
# XXX CDATA translation, should work but does not
|
JSON='{"json:c":{"s":"<![CDATA[ z > x & x < y ]]>"}}'
|
||||||
if false; then
|
|
||||||
JSON='{"json:c": {"s": "<![CDATA[ z > x & x < y ]]>"}}'
|
|
||||||
new "json parse cdata xml"
|
new "json parse cdata xml"
|
||||||
expecteofx "$clixon_util_json -j -y $fyang" 0 "$JSON" "$JSON"
|
expecteofx "$clixon_util_json -j -y $fyang" 0 "$JSON" "$JSON"
|
||||||
fi
|
|
||||||
|
|
||||||
rm -rf $dir
|
rm -rf $dir
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,16 @@
|
||||||
# Test: XML parser tests and JSON translation
|
# Test: XML parser tests and JSON translation
|
||||||
# @see https://www.w3.org/TR/2008/REC-xml-20081126
|
# @see https://www.w3.org/TR/2008/REC-xml-20081126
|
||||||
# https://www.w3.org/TR/2009/REC-xml-names-20091208
|
# 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)
|
# Magic line must be first in script (see README.md)
|
||||||
|
|
||||||
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
|
||||||
|
|
||||||
: ${clixon_util_xml:="clixon_util_xml"}
|
: ${clixon_util_xml:="clixon_util_xml"}
|
||||||
|
: ${clixon_util_json:="clixon_util_json"}
|
||||||
|
|
||||||
new "xml parse"
|
new "xml parse"
|
||||||
expecteof "$clixon_util_xml -o" 0 "<a><b/></a>" "^<a><b/></a>$"
|
expecteof "$clixon_util_xml -o" 0 "<a><b/></a>" "^<a><b/></a>$"
|
||||||
|
|
@ -42,10 +47,10 @@ new "xml CDATA right square bracket: ]"
|
||||||
expecteofx "$clixon_util_xml -o" 0 "<a><![CDATA[]]]></a>" "<a><![CDATA[]]]></a>"
|
expecteofx "$clixon_util_xml -o" 0 "<a><![CDATA[]]]></a>" "<a><![CDATA[]]]></a>"
|
||||||
|
|
||||||
new "xml simple CDATA to json"
|
new "xml simple CDATA to json"
|
||||||
expecteofx "$clixon_util_xml -o -j" 0 '<a><![CDATA[a text]]></a>' '{"a":"a text"}'
|
expecteofx "$clixon_util_xml -o -j" 0 '<a><![CDATA[a text]]></a>' '{"a":"<![CDATA[a text]]>"}'
|
||||||
|
# Example partly from https://www.w3resource.com/xml/attribute.php
|
||||||
new "xml complex CDATA"
|
# XML complex CDATA (with comments for debug):;
|
||||||
XML=$(cat <<EOF
|
DUMMY=$(cat <<EOF
|
||||||
<a><description>An example of escaped CENDs</description>
|
<a><description>An example of escaped CENDs</description>
|
||||||
<sometext><![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]></sometext>
|
<sometext><![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]></sometext>
|
||||||
<!-- This text contains a CEND ]]> -->
|
<!-- This text contains a CEND ]]> -->
|
||||||
|
|
@ -57,19 +62,40 @@ XML=$(cat <<EOF
|
||||||
<alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative>
|
<alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative>
|
||||||
</a>
|
</a>
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
|
# without comments
|
||||||
expecteof "$clixon_util_xml -o" 0 "$XML" "^<a><description>An example of escaped CENDs</description><sometext>
|
XML=$(cat <<EOF
|
||||||
<![CDATA[ They're saying \"x < y\" & that \"z > y\" so I guess that means that z > x ]]>
|
<a><description>An example of escaped CENDs</description>
|
||||||
</sometext><data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data><alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative></a>$"
|
<sometext><![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]></sometext>
|
||||||
|
<data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data>
|
||||||
JSON=$(cat <<EOF
|
<alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative>
|
||||||
{"a":{"description":"An example of escaped CENDs","sometext":" They're saying \"x < y\" & that \"z > y\" so I guess that means that z > x ","data":"This text contains a CEND ]]>","alternative":"This text contains a CEND ]]>"}}
|
</a>
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
XML=$(cat <<'EOF'
|
||||||
|
<a><description>An example of escaped CENDs</description><sometext><![CDATA[ They're saying "x < y" & that "z > y" so I guess that means that z > x ]]></sometext><data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data><alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative></a>
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
new "xml complex CDATA to json"
|
|
||||||
|
new "complex CDATA xml to xml"
|
||||||
|
expecteof "$clixon_util_xml -o" 0 "$XML" "^$XML
|
||||||
|
$"
|
||||||
|
|
||||||
|
JSON=$(cat <<EOF
|
||||||
|
{"a":{"description":"An example of escaped CENDs","sometext":"<![CDATA[ They're saying \"x < y\" & that \"z > y\" so I guess that means that z > x ]]>","data":"<![CDATA[This text contains a CEND ]]]]><![CDATA[>]]>","alternative":"<![CDATA[This text contains a CEND ]]]><![CDATA[]>]]>"}}
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
new "complex CDATA xml to json"
|
||||||
expecteofx "$clixon_util_xml -oj" 0 "$XML" "$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 <<EOF
|
XML=$(cat <<EOF
|
||||||
<message>Less than: < , greater than: > ampersand: & </message>
|
<message>Less than: < , greater than: > ampersand: & </message>
|
||||||
EOF
|
EOF
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue