What is this, I don't even ...


  • :belt_onion:

    While working in the cavernous bowels of the Internet, laboring upon a page over which I had no control (save to load my custom code), I had an occasion to create some JSON. "Okay," I thought to myself, "this is a problem that has been solved before - I'll just use my handy batserializer JSON object to stringify this naughty, naughty object for me":

    JSON.stringify([1, 2, 3])
    ""[1, 2, 3]""
    

    :wtf: thought I, this will not do ... I wonder if it is something they must do to pass through some XML-to-JSON-to-XML conversion portal prepared by the gods of the Big Blue ... if so, it must handle the parsing at least:

    JSON.parse(JSON.stringify([1, 2, 3]))
    "[1, 2, 3]"
    

    :wtf: :wtf:

    Oh, but it does work just fine for objects:

    JSON.parse(JSON.stringify({x: 1, y: 2}))
    Object { x: 1, y: 2 } 
    

    :how: :why: :facepalm:

    Oh, and to add insult after injury, it looks just like the normal JSON.stringify function, so I can't even find the author of this horrible monkey-patch without more work than it's worth:

    JSON.stringify.toString()
    "function stringify() {
        [native code]
    }"
    


  • Considered Harmful

    It's somewhaton reflection, not very likely, that they were trying to protect vs Array prototype injection - some shibboleth might have been being expect to properly deserialize an Array - see #Don'tBeEvil - maybe the extra quoting, they figured it was enough?



  • Are you in the Chrome developer console? Type JSON.stringify as a command and right click the result and click to go to the source or whatever. I'm interested to see the source code of this abomination.



  • var oldStringify = JSON.stringify
    JSON.stringify = function (o)
    {
        if(Array.isArray(o))
            return '"'+JSON.stringify(o)+'"';
        return JSON.stringify(o);
    }
    

    πŸ’» πŸ”«



  • That doesn't explain why JSON.parse accepted the input.



  • a string is valid JSON



  • ""foo"" isn't a valid JSON string, though.

    Edit: oh, it's just the console trying to be "helpful" by using two different string syntaxes.



  • @ben_lubar said:

    ""foo"" isn't a valid JSON string, though.

    I dare say that's not a valid string anywhere.



  • :hanzo:'d by your edit. yes, that


  • :belt_onion:

    The JSON method is native - go to Function definition does nothing. Do not pass Start, do not collect $200. 😒 Very nice idea though πŸ‘


  • :belt_onion:

    Thanks to @ben_lubar's suggestion I started thinking about other things ... and low and behold:

    > Array.prototype.toJSON
      function toJSON() {
        var results = [];
        this.each(function(object) {
          var value = Object.toJSON(object);
          if (!Object.isUndefined(value)) results.push(value);
        });
        return '[' + results.join(', ') + ']';
      }
    

    Ah ... what evil has monkey-patched Array.prototype?

    var Prototype = {
      Version: '1.6.1'
    

    Ah, that evil.

    Let that be a lesson to you boys and girls - never upgrading can break your web.

    Discourse, why on earth would a trailing space result in the code block being ignored? @discoursebot!



  • @‍svieira - Last Day Without A Discourse Bug: null



  • @svieira said:

    low and behold

    Is that why you keep using <sub> instead of <small>?



  • Just for fun...

    β—€ JSON.stringify([1, 2, 3], function (k, o) { return Array.isArray(o) ? o.toSource() : o; })
    β–Ά ""[1, 2, 3]""
    
    β—€ JSON.parse(JSON.stringify([1, 2, 3], function (k, o) { return Array.isArray(o) ? o.toSource() : o; }))
    β–Ά "[1, 2, 3]"
    
    β—€ JSON.parse(JSON.stringify({x: 1, y: 2}, function (k, o) { return Array.isArray(o) ? o.toSource() : o; }))
    β–Ά Object { x: 1, y: 2 }
    

    Note: the outer quote marks delimit the string. Inner quote marks aren't escaped because fuck that. Hence the weird string syntax.


Log in to reply