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

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

Expand Messages
  • Mark Miller
    ... Cool. Is there a draft we can read? -- Text by me above is hereby placed in the public domain Cheers, --MarkM
    Message 1 of 9 , Dec 5, 2006
    • 0 Attachment
      Douglas Crockford 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?


      --
      Text by me above is hereby placed in the public domain

      Cheers,
      --MarkM
    • Douglas Crockford
      BAD: eval( dx = this. + branch + (str, tests[t][1]) ); BETTER: dx = this[branch](str, tests[t][1]);
      Message 2 of 9 , Dec 5, 2006
      • 0 Attachment
        BAD:

        eval("dx = this." + branch + "(str, tests[t][1])");

        BETTER:

        dx = this[branch](str, tests[t][1]);
      • Douglas Crockford
        ... You are right, it would be bad to generate a string like new Date . Instead, you would generate an object that would be recognized by your hook function
        Message 3 of 9 , Dec 5, 2006
        • 0 Attachment
          > > 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.
          >
          > Any chance you will be adding this capability to the
          > http://json.org/json.js file?
          >
          > > So you can look for date objects. You
          > > could also replace Data.prototype.toJSONString with a function that
          > > generates those date objects.

          > Do
          > you mean it would generate a string like "new Date(2006, 05, 18, ...)"
          > so that when the JSON is eval'ed it autmatically converted to and
          > object. I doubt you mean this because it is not portable. Is the
          > "toJSONString" function involved in parsing JSON strings?

          You are right, it would be bad to generate a string like "new Date".
          Instead, you would generate an object that would be recognized by your
          hook function to parseJSON. For example,

          date.toJSONString = function () {
          return '{"type": "date", "value": ' + this.valueOf() +
          '}';
          };
        • Douglas Crockford
          ... http://developer.mozilla.org/es4/proposals/json_encoding_and_decoding.html
          Message 4 of 9 , Dec 5, 2006
          • 0 Attachment
            > > 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
          • 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 5 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.