/framework/source/class/qx/util/Serializer.js

https://github.com/ghiata/qooxdoo · JavaScript · 374 lines · 214 code · 48 blank · 112 comment · 96 complexity · 7e44ea5901d962d033c8385ce7d46175 MD5 · raw file

  1. /* ************************************************************************
  2. qooxdoo - the new era of web development
  3. http://qooxdoo.org
  4. Copyright:
  5. 2004-2009 1&1 Internet AG, Germany, http://www.1und1.de
  6. License:
  7. LGPL: http://www.gnu.org/licenses/lgpl.html
  8. EPL: http://www.eclipse.org/org/documents/epl-v10.php
  9. See the LICENSE file in the project's top-level directory for details.
  10. Authors:
  11. * Martin Wittemann (martinwittemann)
  12. ************************************************************************ */
  13. /**
  14. * This is an util class responsible for serializing qooxdoo objects.
  15. *
  16. * @ignore(qx.data, qx.data.IListData)
  17. * @ignore(qx.locale, qx.locale.LocalizedString)
  18. */
  19. qx.Class.define("qx.util.Serializer",
  20. {
  21. statics :
  22. {
  23. /**
  24. * Serializes the properties of the given qooxdoo object. To get the
  25. * serialization working, every property needs to have a string
  26. * representation because the value of the property will be concatenated to the
  27. * serialized string.
  28. *
  29. * @param object {qx.core.Object} Any qooxdoo object
  30. * @param qxSerializer {Function} Function used for serializing qooxdoo
  31. * objects stored in the propertys of the object. Check for the type of
  32. * classes <ou want to serialize and return the serialized value. In all
  33. * other cases, just return nothing.
  34. * @param dateFormat {qx.util.format.DateFormat} If a date formater is given,
  35. * the format method of this given formater is used to convert date
  36. * objects into strings.
  37. * @return {String} The serialized object.
  38. */
  39. toUriParameter : function(object, qxSerializer, dateFormat)
  40. {
  41. var result = "";
  42. var properties = qx.util.PropertyUtil.getAllProperties(object.constructor);
  43. for (var name in properties) {
  44. // ignore property groups
  45. if (properties[name].group != undefined) {
  46. continue;
  47. }
  48. var value = object["get" + qx.lang.String.firstUp(name)]();
  49. // handle arrays
  50. if (qx.lang.Type.isArray(value)) {
  51. var isdataArray = qx.data && qx.data.IListData &&
  52. qx.Class.hasInterface(value && value.constructor, qx.data.IListData);
  53. for (var i = 0; i < value.length; i++) {
  54. var valueAtI = isdataArray ? value.getItem(i) : value[i];
  55. result += this.__toUriParameter(name, valueAtI, qxSerializer);
  56. }
  57. } else if (qx.lang.Type.isDate(value) && dateFormat != null) {
  58. result += this.__toUriParameter(
  59. name, dateFormat.format(value), qxSerializer
  60. );
  61. } else {
  62. result += this.__toUriParameter(name, value, qxSerializer);
  63. }
  64. }
  65. return result.substring(0, result.length - 1);
  66. },
  67. /**
  68. * Helper method for {@link #toUriParameter}. Check for qooxdoo objects
  69. * and returns the serialized name value pair for the given parameter.
  70. *
  71. * @param name {String} The name of the value
  72. * @param value {var} The value itself
  73. * @param qxSerializer {Function} The serializer for qooxdoo objects.
  74. * @return {String} The serialized name value pair.
  75. */
  76. __toUriParameter : function(name, value, qxSerializer)
  77. {
  78. if (value && value.$$type == "Class") {
  79. value = value.classname;
  80. }
  81. if (value && (value.$$type == "Interface" || value.$$type == "Mixin")) {
  82. value = value.name;
  83. }
  84. if (value instanceof qx.core.Object && qxSerializer != null) {
  85. var encValue = encodeURIComponent(qxSerializer(value));
  86. if (encValue === undefined) {
  87. var encValue = encodeURIComponent(value);
  88. }
  89. } else {
  90. var encValue = encodeURIComponent(value);
  91. }
  92. return encodeURIComponent(name) + "=" + encValue + "&";
  93. },
  94. /**
  95. * Serializes the properties of the given qooxdoo object into a native
  96. * object.
  97. *
  98. * @param object {qx.core.Object}
  99. * Any qooxdoo object
  100. *
  101. * @param qxSerializer {Function}
  102. * Function used for serializing qooxdoo objects stored in the propertys
  103. * of the object. Check for the type of classes you want to serialize
  104. * and return the serialized value. In all other cases, just return
  105. * nothing.
  106. * @param dateFormat {qx.util.format.DateFormat} If a date formater is given,
  107. * the format method of this given formater is used to convert date
  108. * objects into strings.
  109. * @return {String}
  110. * The serialized object.
  111. */
  112. toNativeObject : function(object, qxSerializer, dateFormat)
  113. {
  114. var result;
  115. // null or undefined
  116. if (object == null)
  117. {
  118. return null;
  119. }
  120. // data array
  121. if (qx.data && qx.data.IListData && qx.Class.hasInterface(object.constructor, qx.data.IListData))
  122. {
  123. result = [];
  124. for (var i = 0; i < object.getLength(); i++)
  125. {
  126. result.push(qx.util.Serializer.toNativeObject(
  127. object.getItem(i), qxSerializer, dateFormat)
  128. );
  129. }
  130. return result;
  131. }
  132. // other arrays
  133. if (qx.lang.Type.isArray(object))
  134. {
  135. result = [];
  136. for (var i = 0; i < object.length; i++)
  137. {
  138. result.push(qx.util.Serializer.toNativeObject(
  139. object[i], qxSerializer, dateFormat)
  140. );
  141. }
  142. return result;
  143. }
  144. // return names for qooxdoo classes
  145. if (object.$$type == "Class") {
  146. return object.classname;
  147. }
  148. // return names for qooxdoo interfaces and mixins
  149. if (object.$$type == "Interface" || object.$$type == "Mixin") {
  150. return object.name;
  151. }
  152. // qooxdoo object
  153. if (object instanceof qx.core.Object)
  154. {
  155. if (qxSerializer != null)
  156. {
  157. var returnValue = qxSerializer(object);
  158. // if we have something returned, return that
  159. if (returnValue != undefined)
  160. {
  161. return returnValue;
  162. }
  163. // continue otherwise
  164. }
  165. result = {};
  166. var properties =
  167. qx.util.PropertyUtil.getAllProperties(object.constructor);
  168. for (var name in properties)
  169. {
  170. // ignore property groups
  171. if (properties[name].group != undefined)
  172. {
  173. continue;
  174. }
  175. var value = object["get" + qx.lang.String.firstUp(name)]();
  176. result[name] = qx.util.Serializer.toNativeObject(
  177. value, qxSerializer, dateFormat
  178. );
  179. }
  180. return result;
  181. }
  182. // date objects with date format
  183. if (qx.lang.Type.isDate(object) && dateFormat != null) {
  184. return dateFormat.format(object);
  185. }
  186. // localized strings
  187. if (qx.locale && qx.locale.LocalizedString && object instanceof qx.locale.LocalizedString) {
  188. return object.toString();
  189. }
  190. // JavaScript objects
  191. if (qx.lang.Type.isObject(object))
  192. {
  193. result = {};
  194. for (var key in object)
  195. {
  196. result[key] = qx.util.Serializer.toNativeObject(
  197. object[key], qxSerializer, dateFormat
  198. );
  199. }
  200. return result;
  201. }
  202. // all other stuff, including String, Date, RegExp
  203. return object;
  204. },
  205. /**
  206. * Serializes the properties of the given qooxdoo object into a json object.
  207. *
  208. * @param object {qx.core.Object} Any qooxdoo object
  209. * @param qxSerializer {Function?} Function used for serializing qooxdoo
  210. * objects stored in the propertys of the object. Check for the type of
  211. * classes <ou want to serialize and return the serialized value. In all
  212. * other cases, just return nothing.
  213. * @param dateFormat {qx.util.format.DateFormat?} If a date formater is given,
  214. * the format method of this given formater is used to convert date
  215. * objects into strings.
  216. * @return {String} The serialized object.
  217. */
  218. toJson : function(object, qxSerializer, dateFormat) {
  219. var result = "";
  220. // null or undefined
  221. if (object == null) {
  222. return "null";
  223. }
  224. // data array
  225. if (qx.data && qx.data.IListData && qx.Class.hasInterface(object.constructor, qx.data.IListData)) {
  226. result += "[";
  227. for (var i = 0; i < object.getLength(); i++) {
  228. result += qx.util.Serializer.toJson(object.getItem(i), qxSerializer, dateFormat) + ",";
  229. }
  230. if (result != "[") {
  231. result = result.substring(0, result.length - 1);
  232. }
  233. return result + "]";
  234. }
  235. // other arrays
  236. if (qx.lang.Type.isArray(object)) {
  237. result += "[";
  238. for (var i = 0; i < object.length; i++) {
  239. result += qx.util.Serializer.toJson(object[i], qxSerializer, dateFormat) + ",";
  240. }
  241. if (result != "[") {
  242. result = result.substring(0, result.length - 1);
  243. }
  244. return result + "]";
  245. }
  246. // return names for qooxdoo classes
  247. if (object.$$type == "Class") {
  248. return '"' + object.classname + '"';
  249. }
  250. // return names for qooxdoo interfaces and mixins
  251. if (object.$$type == "Interface" || object.$$type == "Mixin") {
  252. return '"' + object.name + '"';
  253. }
  254. // qooxdoo object
  255. if (object instanceof qx.core.Object) {
  256. if (qxSerializer != null) {
  257. var returnValue = qxSerializer(object);
  258. // if we have something returned, ruturn that
  259. if (returnValue != undefined) {
  260. return '"' + returnValue + '"';
  261. }
  262. // continue otherwise
  263. }
  264. result += "{";
  265. var properties = qx.util.PropertyUtil.getAllProperties(object.constructor);
  266. for (var name in properties) {
  267. // ignore property groups
  268. if (properties[name].group != undefined) {
  269. continue;
  270. }
  271. var value = object["get" + qx.lang.String.firstUp(name)]();
  272. result += '"' + name + '":' + qx.util.Serializer.toJson(value, qxSerializer, dateFormat) + ",";
  273. }
  274. if (result != "{") {
  275. result = result.substring(0, result.length - 1);
  276. }
  277. return result + "}";
  278. }
  279. // localized strings
  280. if (qx.locale && qx.locale.LocalizedString && object instanceof qx.locale.LocalizedString) {
  281. object = object.toString();
  282. // no return here because we want to have the string checks as well!
  283. }
  284. // date objects with formater
  285. if (qx.lang.Type.isDate(object) && dateFormat != null) {
  286. return '"' + dateFormat.format(object) + '"';
  287. }
  288. // javascript objects
  289. if (qx.lang.Type.isObject(object)) {
  290. result += "{";
  291. for (var key in object) {
  292. result += '"' + key + '":' +
  293. qx.util.Serializer.toJson(object[key], qxSerializer, dateFormat) + ",";
  294. }
  295. if (result != "{") {
  296. result = result.substring(0, result.length - 1);
  297. }
  298. return result + "}";
  299. }
  300. // strings
  301. if (qx.lang.Type.isString(object)) {
  302. // escape
  303. object = object.replace(/([\\])/g, '\\\\');
  304. object = object.replace(/(["])/g, '\\"');
  305. object = object.replace(/([\r])/g, '\\r');
  306. object = object.replace(/([\f])/g, '\\f');
  307. object = object.replace(/([\n])/g, '\\n');
  308. object = object.replace(/([\t])/g, '\\t');
  309. object = object.replace(/([\b])/g, '\\b');
  310. return '"' + object + '"';
  311. }
  312. // Date and RegExp
  313. if (qx.lang.Type.isDate(object) || qx.lang.Type.isRegExp(object)) {
  314. return '"' + object + '"';
  315. }
  316. // all other stuff
  317. return object + "";
  318. }
  319. }
  320. });