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

Questions re javascript parseJSON()

Expand Messages
  • Tim
    I m experimenting with using parseJSON() rather than a simple eval . It seems to work except for dates. Given the following code, obj3 returns false, but
    Message 1 of 5 , Sep 20 7:26 AM
    • 0 Attachment
      I'm experimenting with using parseJSON() rather than a simple 'eval'.
      It seems to work except for dates. Given the following code, obj3
      returns false, but test4 returns a valid object with a date.

      var test3 = "{\"ADate\":new Date(Date.UTC(2005,7,12,9,46,4,677))}";
      var obj3 = test3.parseJSON();
      var output = (obj3.ADate) ? obj3.ADate.toString() : 'Bad Date';

      var test4 = eval('(' + test3 +')').ADate.toString();

      Can anyone explain this? Am I missing something?

      Also, while I'm familar with Regex, I'm having a hard time
      understanding what parseJSON is really filtering. Can anyone point to
      an explanation of what's really happening?

      Thanks,
      Tim
    • Douglas Crockford
      ... Your test3 string does not conform to the rules for a JSON text. You can find the rules at http:/www.JSON.org/ The regular expressions in parseJSON look
      Message 2 of 5 , Sep 20 9:43 AM
      • 0 Attachment
        > I'm experimenting with using parseJSON() rather than a simple 'eval'.
        > It seems to work except for dates. Given the following code, obj3
        > returns false, but test4 returns a valid object with a date.
        >
        > var test3 = "{\"ADate\":new Date(Date.UTC(2005,7,12,9,46,4,677))}";
        > var obj3 = test3.parseJSON();

        > Can anyone explain this? Am I missing something?
        >
        > Also, while I'm familar with Regex, I'm having a hard time
        > understanding what parseJSON is really filtering. Can anyone point to
        > an explanation of what's really happening?

        Your test3 string does not conform to the rules for a JSON text. You
        can find the rules at http:/www.JSON.org/

        The regular expressions in parseJSON look for characters which cannot
        appear outside of strings in JSON text. You particularly want to
        reject anything containing '-', '+', 'new', '=', or '(' because they
        can be employed by an evil server to cause mutation or invocation. The
        parseJSON contract is that only data can pass through.
      • Douglas Crockford
        Correction: + and - are allowed because they are required for numbers.
        Message 3 of 5 , Sep 20 9:46 AM
        • 0 Attachment
          Correction: '+' and '-' are allowed because they are required for numbers.
        • Tim
          ... This date is coming back from AjaxPro s JavaScriptSerializer.Serialize() method. Is this not following the standard? Is anyone else using AjaxPro? Are
          Message 4 of 5 , Sep 20 11:34 AM
          • 0 Attachment
            --- In json@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
            >
            > > I'm experimenting with using parseJSON() rather than a simple 'eval'.
            > > It seems to work except for dates. Given the following code, obj3
            > > returns false, but test4 returns a valid object with a date.
            > >
            > > var test3 = "{\"ADate\":new Date(Date.UTC(2005,7,12,9,46,4,677))}";
            > > var obj3 = test3.parseJSON();
            >
            > > Can anyone explain this? Am I missing something?
            > >
            > > Also, while I'm familar with Regex, I'm having a hard time
            > > understanding what parseJSON is really filtering. Can anyone point to
            > > an explanation of what's really happening?
            >
            > Your test3 string does not conform to the rules for a JSON text. You
            > can find the rules at http:/www.JSON.org/
            >

            This date is coming back from AjaxPro's
            JavaScriptSerializer.Serialize() method. Is this not following the
            standard?

            Is anyone else using AjaxPro? Are you just overriding the
            DateConverter to use ToString() and return something like
            Mon, 25 Dec 1995 13:30:00 GMT?

            Thanks,
            Tim
          • Michael Schwarz
            Hi, yes, AjaxPro is not using the standard, it is using something to get AJAX running on web browsers without parsing a lot. The second thing is that AjaxPro
            Message 5 of 5 , Sep 20 3:22 PM
            • 0 Attachment
              Hi,

              yes, AjaxPro is not using "the" standard, it is using something to get AJAX
              running on web browsers without parsing a lot. The second thing is that
              AjaxPro will try to simulate .NET programming style (like DataSets or
              DataTables).

              I was in discussion with others but there was nobody who wants to parse each
              date, so this is the reason why I use "new Date()" instead of a simple
              string.

              AjaxPro enables to build converters, and there is a new DateTime converter
              that is doing one of the JSON standard, also possible to serialize to the
              sortable date format (year-month-day T hour:minute:second).

              Regards,
              Michael




              On 9/20/06, Tim <tryan@...> wrote:
              >
              > --- In json@yahoogroups.com <json%40yahoogroups.com>, "Douglas
              > Crockford" <douglas@...> wrote:
              > >
              > > > I'm experimenting with using parseJSON() rather than a simple 'eval'.
              > > > It seems to work except for dates. Given the following code, obj3
              > > > returns false, but test4 returns a valid object with a date.
              > > >
              > > > var test3 = "{\"ADate\":new Date(Date.UTC(2005,7,12,9,46,4,677))}";
              > > > var obj3 = test3.parseJSON();
              > >
              > > > Can anyone explain this? Am I missing something?
              > > >
              > > > Also, while I'm familar with Regex, I'm having a hard time
              > > > understanding what parseJSON is really filtering. Can anyone point to
              > > > an explanation of what's really happening?
              > >
              > > Your test3 string does not conform to the rules for a JSON text. You
              > > can find the rules at http:/www.JSON.org/
              > >
              >
              > This date is coming back from AjaxPro's
              > JavaScriptSerializer.Serialize() method. Is this not following the
              > standard?
              >
              > Is anyone else using AjaxPro? Are you just overriding the
              > DateConverter to use ToString() and return something like
              > Mon, 25 Dec 1995 13:30:00 GMT?
              >
              > Thanks,
              > Tim
              >
              >
              >



              --
              Best regards | Schöne Grüße
              Michael

              Microsoft MVP - Most Valuable Professional
              Microsoft MCAD - Certified Application Developer

              http://weblogs.asp.net/mschwarz/
              http://www.schwarz-interactive.de/

              Skype: callto:schwarz-interactive
              MSN IM: passport@...


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