Loading ...
Sorry, an error occurred while loading the content.

json2 and objects with toJSON function

Expand Messages
  • soren.hilmer
    Hi Just ran into this issue with json2 (http://www.json.org/json2.js). In the environment where I m developing the situation is that plain Objects does not
    Message 1 of 7 , Feb 14, 2010
    • 0 Attachment
      Hi

      Just ran into this issue with json2 (http://www.json.org/json2.js).

      In the environment where I'm developing the situation is that plain Objects does not have a toJSON function, but Arrays has one.

      When I then call JSON.stringify in json2 with an Object having an Array inside like this one:
      { b:[{k:2, v:3}] }

      I get {"b":"[{\"k\": 2, \"v\": 3}]"}, that is the Array is quoted, now this comes from this piece of code in the str function of json2:

      // If the value has a toJSON method, call it to obtain a replacement value.
      if (value && typeof value === 'object' &&
      typeof value.toJSON === 'function') {
      value = value.toJSON(key);
      }
      ...
      // What happens next depends on the value's type.
      switch (typeof value) {
      case 'string':
      return quote(value);

      Now as the array has a toJSON function, it is called, an assigned to value which is now a string, and later all string's are quoted.

      Would it not be more correct if json2 took the approach that it trusted the output of an objects toJSON function to be correct? And thus simply returned the result of the toJSON call?

      I tried to remedy the situation by supplying a replacer function in my call to stringify, but unfortunately in json2, the replacer function is called after the call to toJSON, so at the time it is called it is called not with an array but again with the string resulting from the toJSON call.

      I believe these to be bugs in json2. IMO the correct code would look something like this:

      // If we were called with a replacer function, then call the replacer to
      // obtain a replacement value.

      if (typeof rep === 'function') {
      value = rep.call(holder, key, value);
      }

      // If the value has a toJSON method, call it to obtain a replacement value.
      if (value && typeof value === 'object' &&
      typeof value.toJSON === 'function') {
      return value.toJSON(key);
      }


      Where I have interchanged two code blocks and return the value of toJSON.
      I'am not sure if the value returned from rep.call, should also be returned directly, though.

      Best
      Søren
    • Dennis Gearon
      what s json2? Dennis Gearon Signature Warning ... EARTH has a Right To Life, otherwise we all die. Read Hot, Flat, and Crowded Laugh at
      Message 2 of 7 , Feb 15, 2010
      • 0 Attachment
        what's json2?

        Dennis Gearon



        Signature Warning

        ----------------

        EARTH has a Right To Life,

        otherwise we all die.



        Read 'Hot, Flat, and Crowded'

        Laugh at http://www.yert.com/film.php

        --- On Sun, 2/14/10, soren.hilmer <sh@...> wrote:

        From: soren.hilmer <sh@...>
        Subject: [json] json2 and objects with toJSON function
        To: json@yahoogroups.com
        Date: Sunday, February 14, 2010, 11:09 PM







         









        Hi



        Just ran into this issue with json2 (http://www.json org/json2. js).



        In the environment where I'm developing the situation is that plain Objects does not have a toJSON function, but Arrays has one.



        When I then call JSON.stringify in json2 with an Object having an Array inside like this one:

        { b:[{k:2, v:3}] }



        I get {"b":"[{\"k\ ": 2, \"v\": 3}]"}, that is the Array is quoted, now this comes from this piece of code in the str function of json2:



        // If the value has a toJSON method, call it to obtain a replacement value.

        if (value && typeof value === 'object' &&

        typeof value.toJSON === 'function') {

        value = value.toJSON( key);

        }

        ...

        // What happens next depends on the value's type.

        switch (typeof value) {

        case 'string':

        return quote(value) ;



        Now as the array has a toJSON function, it is called, an assigned to value which is now a string, and later all string's are quoted.



        Would it not be more correct if json2 took the approach that it trusted the output of an objects toJSON function to be correct? And thus simply returned the result of the toJSON call?



        I tried to remedy the situation by supplying a replacer function in my call to stringify, but unfortunately in json2, the replacer function is called after the call to toJSON, so at the time it is called it is called not with an array but again with the string resulting from the toJSON call.



        I believe these to be bugs in json2. IMO the correct code would look something like this:



        // If we were called with a replacer function, then call the replacer to

        // obtain a replacement value.



        if (typeof rep === 'function') {

        value = rep.call(holder, key, value);

        }



        // If the value has a toJSON method, call it to obtain a replacement value.

        if (value && typeof value === 'object' &&

        typeof value.toJSON === 'function') {

        return value.toJSON( key);

        }



        Where I have interchanged two code blocks and return the value of toJSON.

        I'am not sure if the value returned from rep.call, should also be returned directly, though.



        Best

        Søren






















        [Non-text portions of this message have been removed]
      • Andrea Giammarchi
        Weird environment, I would trust more native implementations or json2.js itself nullifying the method. Array.prototype.toJSON = null; Could it be an acceptable
        Message 3 of 7 , Feb 15, 2010
        • 0 Attachment
          Weird environment, I would trust more native implementations or json2.js
          itself nullifying the method.

          Array.prototype.toJSON = null;

          Could it be an acceptable work around?


          On Mon, Feb 15, 2010 at 8:09 AM, soren.hilmer <sh@...> wrote:

          >
          >
          > Hi
          >
          > Just ran into this issue with json2 (http://www.json.org/json2.js).
          >
          > In the environment where I'm developing the situation is that plain Objects
          > does not have a toJSON function, but Arrays has one.
          >


          [Non-text portions of this message have been removed]
        • soren.hilmer
          json2 is (http://www.json.org/json2.js). --Søren
          Message 4 of 7 , Feb 16, 2010
          • 0 Attachment
            json2 is (http://www.json.org/json2.js).

            --Søren

            --- In json@yahoogroups.com, Dennis Gearon <gearond@...> wrote:
            >
            > what's json2?
            >
            > Dennis Gearon
            >
            >
            >
            > Signature Warning
            >
            > ----------------
            >
            > EARTH has a Right To Life,
            >
            > otherwise we all die.
            >
            >
            >
            > Read 'Hot, Flat, and Crowded'
            >
            > Laugh at http://www.yert.com/film.php
            >
            > --- On Sun, 2/14/10, soren.hilmer <sh@...> wrote:
            >
            > From: soren.hilmer <sh@...>
            > Subject: [json] json2 and objects with toJSON function
            > To: json@yahoogroups.com
            > Date: Sunday, February 14, 2010, 11:09 PM
            >
            >
            >
            >
            >
            >
            >
            >  
            >
            >
            >
            >
            >
            >
            >
            >
            >
            > Hi
            >
            >
            >
            > Just ran into this issue with json2 (http://www.json org/json2. js).
            >
            >
            >
            > In the environment where I'm developing the situation is that plain Objects does not have a toJSON function, but Arrays has one.
            >
            >
            >
            > When I then call JSON.stringify in json2 with an Object having an Array inside like this one:
            >
            > { b:[{k:2, v:3}] }
            >
            >
            >
            > I get {"b":"[{\"k\ ": 2, \"v\": 3}]"}, that is the Array is quoted, now this comes from this piece of code in the str function of json2:
            >
            >
            >
            > // If the value has a toJSON method, call it to obtain a replacement value.
            >
            > if (value && typeof value === 'object' &&
            >
            > typeof value.toJSON === 'function') {
            >
            > value = value.toJSON( key);
            >
            > }
            >
            > ...
            >
            > // What happens next depends on the value's type.
            >
            > switch (typeof value) {
            >
            > case 'string':
            >
            > return quote(value) ;
            >
            >
            >
            > Now as the array has a toJSON function, it is called, an assigned to value which is now a string, and later all string's are quoted.
            >
            >
            >
            > Would it not be more correct if json2 took the approach that it trusted the output of an objects toJSON function to be correct? And thus simply returned the result of the toJSON call?
            >
            >
            >
            > I tried to remedy the situation by supplying a replacer function in my call to stringify, but unfortunately in json2, the replacer function is called after the call to toJSON, so at the time it is called it is called not with an array but again with the string resulting from the toJSON call.
            >
            >
            >
            > I believe these to be bugs in json2. IMO the correct code would look something like this:
            >
            >
            >
            > // If we were called with a replacer function, then call the replacer to
            >
            > // obtain a replacement value.
            >
            >
            >
            > if (typeof rep === 'function') {
            >
            > value = rep.call(holder, key, value);
            >
            > }
            >
            >
            >
            > // If the value has a toJSON method, call it to obtain a replacement value.
            >
            > if (value && typeof value === 'object' &&
            >
            > typeof value.toJSON === 'function') {
            >
            > return value.toJSON( key);
            >
            > }
            >
            >
            >
            > Where I have interchanged two code blocks and return the value of toJSON.
            >
            > I'am not sure if the value returned from rep.call, should also be returned directly, though.
            >
            >
            >
            > Best
            >
            > Søren
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            > [Non-text portions of this message have been removed]
            >
          • soren.hilmer
            Maybe. But I m not looking for a workaround. I m trying to push a bug fix towards json2.js This group seams to be the main communication entrypoint to
            Message 5 of 7 , Feb 16, 2010
            • 0 Attachment
              Maybe.
              But I'm not looking for a workaround.
              I'm trying to push a bug fix towards json2.js

              This group seams to be the main communication entrypoint to
              http://www.json.org

              --Søren

              --- In json@yahoogroups.com, Andrea Giammarchi <andrea.giammarchi@...> wrote:
              >
              > Weird environment, I would trust more native implementations or json2.js
              > itself nullifying the method.
              >
              > Array.prototype.toJSON = null;
              >
              > Could it be an acceptable work around?
              >
              >
              > On Mon, Feb 15, 2010 at 8:09 AM, soren.hilmer <sh@...> wrote:
              >
              > >
              > >
              > > Hi
              > >
              > > Just ran into this issue with json2 (http://www.json.org/json2.js).
              > >
              > > In the environment where I'm developing the situation is that plain Objects
              > > does not have a toJSON function, but Arrays has one.
              > >
              >
              >
              > [Non-text portions of this message have been removed]
              >
            • Erik Arvidsson
              toJSON is supposed to return the object to serialize. See step 2 in the abstract operation Str(key, holder) (page 204, section 15.12.3) in the spec. Your
              Message 6 of 7 , Feb 16, 2010
              • 0 Attachment
                toJSON is supposed to return the object to serialize. See step 2 in the
                abstract operation Str(key, holder) (page 204, section 15.12.3) in the spec.

                Your environment incorrectly returns a string for Array.prototype.toJSON
                which of course means you end up with a string in your serialized JSON.

                HTH,

                erik

                On Feb 16, 2010 4:32 AM, "soren.hilmer" <sh@...> wrote:





                Maybe.
                But I'm not looking for a workaround.
                I'm trying to push a bug fix towards json2.js

                This group seams to be the main communication entrypoint to
                http://www.json.org

                --Søren



                --- In json@yahoogroups.com, Andrea Giammarchi <andrea.giammarchi@...>
                wrote:
                >
                > Weird environmen...

                > On Mon, Feb 15, 2010 at 8:09 AM, soren.hilmer <sh@...> wrote:
                >
                > >
                > >
                > > Hi
                > >
                > > Just ran i...




                [Non-text portions of this message have been removed]
              • Erik Arvidsson
                ... spec = ECMA 262 edition 5, http://www.ecma-international.org/publications/standards/Ecma-262.htm ... -- erik [Non-text portions of this message have been
                Message 7 of 7 , Feb 16, 2010
                • 0 Attachment
                  On Tue, Feb 16, 2010 at 09:14, Erik Arvidsson <erik.arvidsson@...>wrote:

                  > toJSON is supposed to return the object to serialize. See step 2 in the
                  > abstract operation Str(key, holder) (page 204, section 15.12.3) in the spec.
                  >
                  spec = ECMA 262 edition 5,
                  http://www.ecma-international.org/publications/standards/Ecma-262.htm

                  > Your environment incorrectly returns a string for Array.prototype.toJSON
                  > which of course means you end up with a string in your serialized JSON.
                  >
                  > HTH,
                  >
                  > erik
                  >
                  > On Feb 16, 2010 4:32 AM, "soren.hilmer" <sh@...> wrote:
                  >
                  >
                  >
                  >
                  >
                  > Maybe.
                  > But I'm not looking for a workaround.
                  > I'm trying to push a bug fix towards json2.js
                  >
                  > This group seams to be the main communication entrypoint to
                  > http://www.json.org
                  >
                  > --Søren
                  >
                  >
                  >
                  > --- In json@yahoogroups.com, Andrea Giammarchi <andrea.giammarchi@...>
                  > wrote:
                  > >
                  > > Weird environmen...
                  >
                  > > On Mon, Feb 15, 2010 at 8:09 AM, soren.hilmer <sh@...> wrote:
                  > >
                  > > >
                  > > >
                  > > > Hi
                  > > >
                  > > > Just ran i...
                  >
                  >
                  >
                  >


                  --
                  erik


                  [Non-text portions of this message have been removed]
                Your message has been successfully submitted and would be delivered to recipients shortly.