What is the correct and reliable way to store an XML string as a JSON property?

I want to save a string, which itself is an XML string, as a property of a JSON object, what is a reliable and correct way to dong? Do I have to encode XML data in BASE64 first before storing it in a JSON object, because JSON does not support binary data?

Example data that I want to save:

{ 
"string1" : "<xml>...moderately complex XML...</xml>" 
} 
+3
source share
2 answers

In fact, base 64 should work. But you may want to tag the property to make it clear.

{
    "Property" : {
        "Type" : "XML",
        "Encoding" : "Base64",
        "Value" : "PFhNTD48WE1MPjxYTUw+PC9YTUw+PC9YTUw+PC9YTUw+"
    }
}
+2
source

JSON does not support binary data?

, bytes, , ? JavaScript , :

"string1": "\u0000\u0001\u0002..."

( , XML.)

, , :

"xml": "<el>caf\u00C3\u00A9</el>"
// "café", encoded as a UTF-8 byte sequence read as ISO-8859-1

XML Unicode JSON- :

"xml": "<el>caf\u00E9</el>"
// or assuming your channel encoding is OK, simply
"xml": "<el>café</el>"
0

Source: https://habr.com/ru/post/1747531/


All Articles