PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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