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

1461Re: [json] json2 and objects with toJSON function

Expand Messages
  • Dennis Gearon
    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]
    • Show all 7 messages in this topic