On 2018-03-16 18:04, Mike Samuel wrote:

It is entirely unsuitable to embedding in HTML or XML though.
IIUC, with an implementation based on this

   JSON.canonicalize(JSON.stringify("</script>")) === `"</script>"` &&
JSON.canonicalize(JSON.stringify("]]>")) === `"]]>"`

I don't know what you are trying to prove here :-)


The output of JSON.canonicalize would also not be in the subset of JSON that is 
also a subset of JavaScript's PrimaryExpression.

    JSON.canonicalize(JSON.stringify("\u2028\u2029")) === `"\u2028\u2029"`

It also is not suitable for use internally within systems that internally use 
cstrings.

   JSON.canonicalize(JSON.stringify("\u0000")) === `"\u0000"`


JSON.canonicalize() would be [almost] identical to JSON.stringify()

JSON.canonicalize(JSON.parse('"\u2028\u2029"')) === '"\u2028\u2029"'  // 
Returns true

"Emulator":

var canonicalize = function(object) {

    var buffer = '';
    serialize(object);
    return buffer;

    function serialize(object) {
        if (object !== null && typeof object === 'object') {
            if (Array.isArray(object)) {
                buffer += '[';
                let next = false;
                object.forEach((element) => {
                    if (next) {
                        buffer += ',';
                    }
                    next = true;
                    serialize(element);
                });
                buffer += ']';
            } else {
                buffer += '{';
                let next = false;
                Object.keys(object).sort().forEach((property) => {
                    if (next) {
                        buffer += ',';
                    }
                    next = true;
                    buffer += JSON.stringify(property);
                    buffer += ':';
                    serialize(object[property]);
                });
                buffer += '}';
            }
        } else {
            buffer += JSON.stringify(object);
        }
    }
};
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to