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

1629Re: IE hosted objects and toJSON check

Expand Messages
  • Douglas Crockford
    Apr 3, 2011
    • 0 Attachment
      --- In json@yahoogroups.com, Andrea Giammarchi <andrea.giammarchi@...> wrote:
      >
      > 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

      I am happy that you were able to get it working, but I have to tell you, I have absolutely no interest in supporting VBScript or typeof unknown. I have a very strong interest in seeing that crap go extinct.
    • Show all 3 messages in this topic