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

1628[json] IE hosted objects and toJSON check

Expand Messages
  • Andrea Giammarchi
    Apr 3 3:34 AM
      This is mainly for Mr D and it's about json2.js ( probably the sans eval as
      well )

      If I create an object through a VBScript class definition and this object
      has a hosted toJSON public method the parser will fail, while it does not
      fail with native IE8 and IE9 implementation.

      The change should be straight forward, where there is a check about the
      method type there should be an "unknown" as well.

      In few words:

      if (typeof object === "object" && (typeof object.toJSON === "function" ||
      typeof object.toJSON === "unknown")) {
      value = object.toJSON(key);
      }

      Above code will already work as expected but to be honest I would rather
      make the check more portable

      function invokeToJSON(object, method, key) {
      switch (typeof object[method]) {
      case "function":
      case "unknown":
      try {
      return typeof key === "string" ?
      object[method]() :
      object[method](key)
      ;
      } catch(e) {
      // notify something here
      }
      }
      }

      // so that ...

      if (typeof object === "object") {
      value = invokeToJSON(object, "toJSON", key);
      }

      // then if value is not undefined ...

      the function could be reused for toString and valueOf, if necessary, without
      a key argument since VBScript methods are sensible to arguments length.

      Any comment/improvement will be appreciated, any update on github more than
      welcome.

      Best Regards,
      Andrea Giammarchi

      P.S. "unknown" is IE specific


      [Non-text portions of this message have been removed]
    • Show all 3 messages in this topic