[qooxdoo-commit] SF.net SVN: qooxdoo:[19729] trunk/qooxdoo/framework/source/class/qx
Brought to you by:
ecker,
martinwittemann
From: <mar...@us...> - 2009-07-31 07:11:44
|
Revision: 19729 http://qooxdoo.svn.sourceforge.net/qooxdoo/?rev=19729&view=rev Author: martinwittemann Date: 2009-07-31 07:11:34 +0000 (Fri, 31 Jul 2009) Log Message: ----------- Added a json serializer. Modified Paths: -------------- trunk/qooxdoo/framework/source/class/qx/test/util/Serializer.js trunk/qooxdoo/framework/source/class/qx/util/Serializer.js Modified: trunk/qooxdoo/framework/source/class/qx/test/util/Serializer.js =================================================================== --- trunk/qooxdoo/framework/source/class/qx/test/util/Serializer.js 2009-07-30 22:18:23 UTC (rev 19728) +++ trunk/qooxdoo/framework/source/class/qx/test/util/Serializer.js 2009-07-31 07:11:34 UTC (rev 19729) @@ -37,7 +37,7 @@ nullable: true } } - }); + }); this.__s = qx.util.Serializer; }, @@ -57,7 +57,7 @@ }, - testString : function() { + testUrlString : function() { this.__model.setData1("a"); this.__model.setData2("b"); this.__model.setData3("c"); @@ -74,7 +74,7 @@ this.assertEquals("data1=1&data2=11&data3=111", this.__s.toUriParameter(this.__model)); }, - testStringEncoded : function() { + testUrlStringEncoded : function() { this.__model.setData1("ä"); this.__model.setData2("ö"); this.__model.setData3("ü"); @@ -86,14 +86,14 @@ this.assertEquals("data1=%E2%80%93&data2=%20&data3=%C3%9F", this.__s.toUriParameter(this.__model)); }, - testBoolean : function() { + testUrlBoolean : function() { this.__model.setData1(true); this.__model.setData2(false); this.__model.setData3(null); this.assertEquals("data1=true&data2=false&data3=null", this.__s.toUriParameter(this.__model)); }, - testNumber : function() { + testUrlNumber : function() { this.__model.setData1(10); this.__model.setData2(-15.3443); this.__model.setData3(Number.NaN); @@ -101,7 +101,7 @@ }, - testKeyEncoded : function() { + testUrlKeyEncoded : function() { qx.Class.define("qx.test.SerializModelEnc", { extend: qx.core.Object, properties : { @@ -115,6 +115,72 @@ this.assertEquals("%C3%A4%C3%BC%C3%B6=%C3%84%C3%9C%C3%96", this.__s.toUriParameter(model)); model.dispose(); + }, + + + testJsonFlat : function() { + this.__model.setData1("a"); + this.__model.setData2(10.456); + this.__model.setData3(true); + this.assertEquals('{data1:"a",data2:10.456,data3:true}', this.__s.toJson(this.__model)); + }, + + + testJsonExp : function() { + this.__model.setData1(new Date(1000)); + this.__model.setData2(/[0]/); + this.__model.setData3(45e12); + this.assertEquals('{data1:Thu Jan 01 1970 01:00:01 GMT+0100 (CET),data2:/[0]/,data3:45000000000000}', this.__s.toJson(this.__model)); + }, + + + testJsonDeep2 : function() { + var model = new qx.test.SerializModel(); + model.setData1("a"); + model.setData2(11); + model.setData3(false); + + this.__model.setData1(model); + this.__model.setData3(null); + this.assertEquals('{data1:{data1:"a",data2:11,data3:false},data2:null,data3:null}', this.__s.toJson(this.__model)); + + model.dispose(); + }, + + + testJsonArray : function() { + this.__model.setData1([12, 1]); + this.__model.setData2(["a", "b"]); + this.__model.setData3([true, false]); + this.assertEquals('{data1:[12,1],data2:["a","b"],data3:[true,false]}', this.__s.toJson(this.__model)); + }, + + testJsonDataArray : function() { + this.__model.setData1(new qx.data.Array([12, 1])); + this.__model.setData2(new qx.data.Array(["a", "b"])); + this.__model.setData3(new qx.data.Array([true, false])); + this.assertEquals('{data1:[12,1],data2:["a","b"],data3:[true,false]}', this.__s.toJson(this.__model)); + + this.__model.getData1().dispose(); + this.__model.getData2().dispose(); + this.__model.getData3().dispose(); + }, + + testJsonBig : function() { + var model = new qx.ui.core.Widget(); + this.__s.toJson(model); + model.dispose(); + }, + + + testJsonEmpty : function() { + this.__model.setData1(new qx.data.Array()); + this.__model.setData2([]); + this.__model.setData3(this); + this.assertEquals('{data1:[],data2:[],data3:{}}', this.__s.toJson(this.__model)); + + this.__model.getData1().dispose(); } + } }); Modified: trunk/qooxdoo/framework/source/class/qx/util/Serializer.js =================================================================== --- trunk/qooxdoo/framework/source/class/qx/util/Serializer.js 2009-07-30 22:18:23 UTC (rev 19728) +++ trunk/qooxdoo/framework/source/class/qx/util/Serializer.js 2009-07-31 07:11:34 UTC (rev 19729) @@ -45,6 +45,66 @@ result += encodeURIComponent(name) + "=" + encodeURIComponent(value) + "&"; } return result.substring(0, result.length - 1); + }, + + + /** + * Serializes the properties of the given qooxdoo object into a json object. + * + * @param object {qx.core.Object} Any qooxdoo object + * @return {String} The serialized object. + */ + toJson : function(object) { + var result = ""; + + // null or undefined + if (object == null) { + return "null"; + // data array + } else if (qx.Class.hasInterface(object.constructor, qx.data.IListData)) { + result += "["; + for (var i = 0; i < object.getLength(); i++) { + result += qx.util.Serializer.toJson(object.getItem(i)) + ","; + } + if (result != "[") { + result = result.substring(0, result.length - 1); + } + return result + "]"; + + // other arrays + } else if (qx.lang.Type.isArray(object)) { + result += "["; + for (var i = 0; i < object.length; i++) { + result += qx.util.Serializer.toJson(object[i]) + ","; + } + if (result != "[") { + result = result.substring(0, result.length - 1); + } + return result + "]"; + + // qooxdoo object + } else if (object instanceof qx.core.Object) { + result += "{"; + var properties = qx.util.PropertyUtil.getProperties(object.constructor); + for (var name in properties) { + // ignore property groups + if (properties[name].group != undefined) { + continue; + } + var value = object["get" + qx.lang.String.firstUp(name)](); + result += name + ":" + qx.util.Serializer.toJson(value) + ","; + } + if (result != "{") { + result = result.substring(0, result.length - 1); + } + return result + "}"; + + // strings + } else if (qx.lang.Type.isString(object)) { + return '"' + object + '"'; + } + // all other stuff + return object + ""; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |