You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by et...@apache.org on 2009/04/25 12:06:52 UTC

svn commit: r768508 - in /incubator/shindig/trunk/features/src/main/javascript/features/core: json.js legacy.js

Author: etnu
Date: Sat Apr 25 10:06:51 2009
New Revision: 768508

URL: http://svn.apache.org/viewvc?rev=768508&view=rev
Log:
Native JSON support for gadgets.json.


Modified:
    incubator/shindig/trunk/features/src/main/javascript/features/core/json.js
    incubator/shindig/trunk/features/src/main/javascript/features/core/legacy.js

Modified: incubator/shindig/trunk/features/src/main/javascript/features/core/json.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/core/json.js?rev=768508&r1=768507&r2=768508&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/core/json.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/core/json.js Sat Apr 25 10:06:51 2009
@@ -40,129 +40,151 @@
  * Port of the public domain JSON library by Douglas Crockford.
  * See: http://www.json.org/json2.js
  */
-gadgets.json = function () {
-
-  /**
-   * Formats integers to 2 digits.
-   * @param {Number} n
-   */
-  function f(n) {
-    return n < 10 ? '0' + n : n;
-  }
-
-  Date.prototype.toJSON = function () {
-    return [this.getUTCFullYear(), '-',
-           f(this.getUTCMonth() + 1), '-',
-           f(this.getUTCDate()), 'T',
-           f(this.getUTCHours()), ':',
-           f(this.getUTCMinutes()), ':',
-           f(this.getUTCSeconds()), 'Z'].join("");
-  };
-
-  // table of character substitutions
-  var m = {
-    '\b': '\\b',
-    '\t': '\\t',
-    '\n': '\\n',
-    '\f': '\\f',
-    '\r': '\\r',
-    '"' : '\\"',
-    '\\': '\\\\'
-  };
-
-  /**
-   * Converts a json object into a string.
-   */
-  function stringify(value) {
-    var a,          // The array holding the partial texts.
-        i,          // The loop counter.
-        k,          // The member key.
-        l,          // Length.
-        r = /["\\\x00-\x1f\x7f-\x9f]/g,
-        v;          // The member value.
-
-    switch (typeof value) {
-    case 'string':
-    // If the string contains no control characters, no quote characters, and no
-    // backslash characters, then we can safely slap some quotes around it.
-    // Otherwise we must also replace the offending characters with safe ones.
-      return r.test(value) ?
-          '"' + value.replace(r, function (a) {
-            var c = m[a];
-            if (c) {
-              return c;
-            }
-            c = a.charCodeAt();
-            return '\\u00' + Math.floor(c / 16).toString(16) +
-                (c % 16).toString(16);
-            }) + '"' : '"' + value + '"';
-    case 'number':
-    // JSON numbers must be finite. Encode non-finite numbers as null.
-      return isFinite(value) ? String(value) : 'null';
-    case 'boolean':
-    case 'null':
-      return String(value);
-    case 'object':
-    // Due to a specification blunder in ECMAScript,
-    // typeof null is 'object', so watch out for that case.
-      if (!value) {
-        return 'null';
+if (window.JSON) {
+  // HTML5 implementation, or already defined.
+  // Not a direct alias as the opensocial specification disagrees with the HTML5 JSON spec.
+  // JSON says to throw on parse errors and to support filtering functions. OS does not.
+  gadgets.json = {
+    parse: function(str) {
+      try {
+        return window.JSON.parse(str);
+      } catch (e) {
+        return false;
       }
-      // toJSON check removed; re-implement when it doesn't break other libs.
-      a = [];
-      if (typeof value.length === 'number' &&
-          !value.propertyIsEnumerable('length')) {
-        // The object is an array. Stringify every element. Use null as a
-        // placeholder for non-JSON values.
-        l = value.length;
-        for (i = 0; i < l; i += 1) {
-          a.push(stringify(value[i]) || 'null');
-        }
-        // Join all of the elements together and wrap them in brackets.
-        return '[' + a.join(',') + ']';
+    },
+    stringify: function(obj) {
+      try {
+        return window.JSON.stringify(obj);
+      } catch (e) {
+        return null;
       }
-      // Otherwise, iterate through all of the keys in the object.
-      for (k in value) {
-        if (value.hasOwnProperty(k)) {
-          if (typeof k === 'string') {
-            v = stringify(value[k]);
-            if (v) {
-              a.push(stringify(k) + ':' + v);
+    }
+  };
+} else {
+  gadgets.json = function () {
+  
+    /**
+     * Formats integers to 2 digits.
+     * @param {Number} n
+     */
+    function f(n) {
+      return n < 10 ? '0' + n : n;
+    }
+  
+    Date.prototype.toJSON = function () {
+      return [this.getUTCFullYear(), '-',
+             f(this.getUTCMonth() + 1), '-',
+             f(this.getUTCDate()), 'T',
+             f(this.getUTCHours()), ':',
+             f(this.getUTCMinutes()), ':',
+             f(this.getUTCSeconds()), 'Z'].join("");
+    };
+  
+    // table of character substitutions
+    var m = {
+      '\b': '\\b',
+      '\t': '\\t',
+      '\n': '\\n',
+      '\f': '\\f',
+      '\r': '\\r',
+      '"' : '\\"',
+      '\\': '\\\\'
+    };
+  
+    /**
+     * Converts a json object into a string.
+     */
+    function stringify(value) {
+      var a,          // The array holding the partial texts.
+          i,          // The loop counter.
+          k,          // The member key.
+          l,          // Length.
+          r = /["\\\x00-\x1f\x7f-\x9f]/g,
+          v;          // The member value.
+  
+      switch (typeof value) {
+      case 'string':
+      // If the string contains no control characters, no quote characters, and no
+      // backslash characters, then we can safely slap some quotes around it.
+      // Otherwise we must also replace the offending characters with safe ones.
+        return r.test(value) ?
+            '"' + value.replace(r, function (a) {
+              var c = m[a];
+              if (c) {
+                return c;
+              }
+              c = a.charCodeAt();
+              return '\\u00' + Math.floor(c / 16).toString(16) +
+                  (c % 16).toString(16);
+              }) + '"' : '"' + value + '"';
+      case 'number':
+      // JSON numbers must be finite. Encode non-finite numbers as null.
+        return isFinite(value) ? String(value) : 'null';
+      case 'boolean':
+      case 'null':
+        return String(value);
+      case 'object':
+      // Due to a specification blunder in ECMAScript,
+      // typeof null is 'object', so watch out for that case.
+        if (!value) {
+          return 'null';
+        }
+        // toJSON check removed; re-implement when it doesn't break other libs.
+        a = [];
+        if (typeof value.length === 'number' &&
+            !value.propertyIsEnumerable('length')) {
+          // The object is an array. Stringify every element. Use null as a
+          // placeholder for non-JSON values.
+          l = value.length;
+          for (i = 0; i < l; i += 1) {
+            a.push(stringify(value[i]) || 'null');
+          }
+          // Join all of the elements together and wrap them in brackets.
+          return '[' + a.join(',') + ']';
+        }
+        // Otherwise, iterate through all of the keys in the object.
+        for (k in value) {
+          if (value.hasOwnProperty(k)) {
+            if (typeof k === 'string') {
+              v = stringify(value[k]);
+              if (v) {
+                a.push(stringify(k) + ':' + v);
+              }
             }
           }
         }
+        // Join all of the member texts together and wrap them in braces.
+        return '{' + a.join(',') + '}';
       }
-      // Join all of the member texts together and wrap them in braces.
-      return '{' + a.join(',') + '}';
     }
-  }
-
-  return {
-    stringify: stringify,
-    parse: function (text) {
-// Parsing happens in three stages. In the first stage, we run the text against
-// regular expressions that look for non-JSON patterns. We are especially
-// concerned with '()' and 'new' because they can cause invocation, and '='
-// because it can cause mutation. But just to be safe, we want to reject all
-// unexpected forms.
-
-// We split the first stage into 4 regexp operations in order to work around
-// crippling inefficiencies in IE's and Safari's regexp engines. First we
-// replace all backslash pairs with '@' (a non-JSON character). Second, we
-// replace all simple value tokens with ']' characters. Third, we delete all
-// open brackets that follow a colon or comma or that begin the text. Finally,
-// we look to see that the remaining characters are only whitespace or ']' or
-// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
-
-      if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/b-u]/g, '@').
-          replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
-          replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
-        return eval('(' + text + ')');
+  
+    return {
+      stringify: stringify,
+      parse: function (text) {
+      // Parsing happens in three stages. In the first stage, we run the text against
+      // regular expressions that look for non-JSON patterns. We are especially
+      // concerned with '()' and 'new' because they can cause invocation, and '='
+      // because it can cause mutation. But just to be safe, we want to reject all
+      // unexpected forms.
+      
+      // We split the first stage into 4 regexp operations in order to work around
+      // crippling inefficiencies in IE's and Safari's regexp engines. First we
+      // replace all backslash pairs with '@' (a non-JSON character). Second, we
+      // replace all simple value tokens with ']' characters. Third, we delete all
+      // open brackets that follow a colon or comma or that begin the text. Finally,
+      // we look to see that the remaining characters are only whitespace or ']' or
+      // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
+  
+        if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/b-u]/g, '@').
+            replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
+            replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
+          return eval('(' + text + ')');
+        }
+        // If the text is not JSON parseable, then return false.
+  
+        return false;
       }
-      // If the text is not JSON parseable, then return false.
-
-      return false;
-    }
-  };
-}();
+    };
+  }();
+}
 

Modified: incubator/shindig/trunk/features/src/main/javascript/features/core/legacy.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/core/legacy.js?rev=768508&r1=768507&r2=768508&view=diff
==============================================================================
--- incubator/shindig/trunk/features/src/main/javascript/features/core/legacy.js (original)
+++ incubator/shindig/trunk/features/src/main/javascript/features/core/legacy.js Sat Apr 25 10:06:51 2009
@@ -20,9 +20,9 @@
 /*global gadgets */
 
 // All functions in this file should be treated as deprecated legacy routines.
- // Gadget authors are explicitly discouraged from using any of them.
+// Gadget authors are explicitly discouraged from using any of them.
 
-var JSON = gadgets.json;
+var JSON = window.JSON || gadgets.json;
 
 var _IG_Prefs = (function() {