Author: lindner
Date: Thu May 6 10:14:24 2010
New Revision: 941640
URL: http://svn.apache.org/viewvc?rev=941640&view=rev
Log:
add a flatten routine to gadgets.json
Modified:
shindig/trunk/features/src/main/javascript/features/core.json/json.js
Modified: shindig/trunk/features/src/main/javascript/features/core.json/json.js
URL:
http://svn.apache.org/viewvc/shindig/trunk/features/src/main/javascript/features/core.json/json.js?rev=941640&r1=941639&r2=941640&view=diff
==============================================================================
--- shindig/trunk/features/src/main/javascript/features/core.json/json.js
(original)
+++ shindig/trunk/features/src/main/javascript/features/core.json/json.js Thu
May 6 10:14:24 2010
@@ -26,7 +26,7 @@
*
* gadgets.json.parse(text) takes a JSON text and produces a JavaScript value.
* It will return false if there is an error.
-*/
+ */
/**
* @static
@@ -45,6 +45,7 @@ if (window.JSON && window.JSON.parse &&
gadgets['json'] = (function() {
var endsWith___ = /___$/;
return {
+ /* documented below */
'parse': function(str) {
try {
return window.JSON.parse(str);
@@ -52,6 +53,7 @@ if (window.JSON && window.JSON.parse &&
return false;
}
},
+ /* documented below */
'stringify': function(obj) {
try {
return window.JSON.stringify(obj, function(k,v) {
@@ -64,6 +66,10 @@ if (window.JSON && window.JSON.parse &&
};
})();
} else {
+/**
+ * Port of the public domain JSON library by Douglas Crockford.
+ * See: http://www.json.org/json2.js
+ */
gadgets['json'] = function () {
/**
@@ -200,4 +206,28 @@ if (window.JSON && window.JSON.parse &&
};
}();
}
+/**
+ * Flatten an object to a stringified values. Useful for dealing with
+ * json->querystring transformations.
+ *
+ * @param obj {Object}
+ * @return {Object} object with only string values
+ * @private not in official specification yet
+ */
+
+gadgets['json'].flatten = function(obj) {
+ var flat = {};
+
+ if (obj === null || obj === undefined) return flat;
+ for (var k in obj) {
+ if (obj.hasOwnProperty(k)) {
+ var value = obj[k];
+ if (null === value || undefined === value) {
+ continue;
+ }
+ flat[k] = (typeof value === 'string') ? value :
gadgets.json.stringify(value);
+ }
+ }
+ return flat;
+}