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

Re: [json] Re: Dates do not automatically decode

Expand Messages
  • Peter Michaux
    Hi, ... I read the proposal and wonder if there is some loose wording for parseJSON. I doubt it but would like to double check. The optional hook function
    Message 1 of 9 , Dec 5, 2006
    • 0 Attachment
      Hi,

      On 12/5/06, Douglas Crockford <douglas@...> wrote:
      >
      > > > ECMAScript Fourth Edition's String.prototype.parseJSON method will
      > > > take an optional hook function which will have a chance to filter
      > > > every object in the structure.
      >
      > > Cool. Is there a draft we can read?
      >
      > http://developer.mozilla.org/es4/proposals/json_encoding_and_decoding.html


      I read the proposal and wonder if there is some loose wording for
      parseJSON. I doubt it but would like to double check.

      "The optional hook function will be called for each object value
      found. It is passed each object. Its return value will be used instead
      of the object in the final structure."

      If only "each object value" is run through the hook function then this
      won't allow for a date string to be run though the hook function and
      converted to a Date object. This is because a the string produced by
      eval is a primitive. Should "each object value" be changed to "each
      node value" or "each object property value"?

      If the wording is correct is it up to me too loop through the object
      properties manually in my hook function? A "yes you must do the
      looping" answer to this would be satisfy my curiosity.

      I tested the different interpretations with two functions that
      simulate parseJSON. I am using a namespaced library so it is a little
      different set up but I took the parseJSON function from
      http://www.json.org/json.js as the starting point. Also I don't
      augment prototypes so I have used for-in loops without checking own
      property.

      Douglas, could you please confirm which implementation is the intended
      one for the hook function.

      Thank you,
      Peter


      // My literal interpretation from the proposed JSON API

      FORK.Json.load = function (str, hook) {
      try {
      if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u
      \n\r\t])+?$/.test(str)) {

      var obj = eval('(' + str + ')');

      if (hook) {

      obj = (function(o) {
      if (typeof o == 'object') {
      for (var p in o) {
      o[p] = arguments.callee(o[p]);
      }
      return hook(o);
      } else {
      return o;
      }
      })(obj);

      }
      return obj;
      }
      } catch (e) {}

      throw new SyntaxError("parseJSON");
      };


      // This could be what is intended implementation of the proposed hook function.

      FORK.Json.load = function (str, hook) {
      try {
      if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u
      \n\r\t])+?$/.test(str)) {

      var obj = eval('(' + str + ')');

      if (hook) {
      obj = (function(o) {
      if (typeof o == 'object') {
      for (var p in o) {
      o[p] = arguments.callee(o[p]);
      }
      }
      return hook(o);
      })(obj);
      }
      return obj;
      }
      } catch (e) {}

      throw new SyntaxError("parseJSON");
      };
    Your message has been successfully submitted and would be delivered to recipients shortly.