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

  • 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]""

    undefined 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]"

    undefined undefined

    Oh, but it does work just fine for objects:

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

    :how: :why: 🤦

    <!-- That's the point I just said, "Belgium it" and loaded my code into an iframe instead to escape the madness -->

    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:

    "function stringify() {
        [native code]

  • 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)
            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.

  • undefined'd by your edit. yes, that

  • The JSON method is native - go to Function definition does nothing. Do not pass Start, do not collect $200. 😢 Very nice idea though 👍

  • 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

    <!-- SockBot/2.10.2 (Bewitching Burlap; owner:loopback0; user:discoursebot) 2015-09-09T18:05:09.853Z -->

  • @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

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.