2c7dfc15e0e5a4524086b24759dcb6f3a2a0818e.svn-base 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.util.JSON = new (function() {
  7. var useHasOwn = {}.hasOwnProperty ? true : false;
  8. var pad = function(n) {
  9. return n < 10 ? "0" + n : n
  10. };
  11. var m = {
  12. "\b" : "\\b",
  13. "\t" : "\\t",
  14. "\n" : "\\n",
  15. "\f" : "\\f",
  16. "\r" : "\\r",
  17. "\"" : "\\\"",
  18. "\\" : "\\\\"
  19. };
  20. var encodeString = function(s) {
  21. if (/["\\\x00-\x1f]/.test(s)) {
  22. return "\"" + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
  23. var c = m[b];
  24. if (c) {
  25. return c
  26. }
  27. c = b.charCodeAt();
  28. return "\\u00" + Math.floor(c / 16).toString(16)
  29. + (c % 16).toString(16)
  30. }) + "\""
  31. }
  32. return "\"" + s + "\""
  33. };
  34. var encodeArray = function(o) {
  35. var a = ["["], b, i, l = o.length, v;
  36. for (i = 0; i < l; i += 1) {
  37. v = o[i];
  38. switch (typeof v) {
  39. case "undefined" :
  40. case "function" :
  41. case "unknown" :
  42. break;
  43. default :
  44. if (b) {
  45. a.push(",")
  46. }
  47. a.push(v === null ? "null" : Ext.util.JSON.encode(v));
  48. b = true
  49. }
  50. }
  51. a.push("]");
  52. return a.join("")
  53. };
  54. var encodeDate = function(o) {
  55. return "\"" + o.getFullYear() + "-" + pad(o.getMonth() + 1) + "-"
  56. + pad(o.getDate()) + "T" + pad(o.getHours()) + ":"
  57. + pad(o.getMinutes()) + ":" + pad(o.getSeconds()) + "\""
  58. };
  59. this.encode = function(o) {
  60. if (typeof o == "undefined" || o === null) {
  61. return "null"
  62. } else {
  63. if (o instanceof Array) {
  64. return encodeArray(o)
  65. } else {
  66. if (o instanceof Date) {
  67. return encodeDate(o)
  68. } else {
  69. if (typeof o == "string") {
  70. return encodeString(o)
  71. } else {
  72. if (typeof o == "number") {
  73. return isFinite(o) ? String(o) : "null"
  74. } else {
  75. if (typeof o == "boolean") {
  76. return String(o)
  77. } else {
  78. var a = ["{"], b, i, v;
  79. for (i in o) {
  80. if (!useHasOwn || o.hasOwnProperty(i)) {
  81. v = o[i];
  82. switch (typeof v) {
  83. case "undefined" :
  84. case "function" :
  85. case "unknown" :
  86. break;
  87. default :
  88. if (b) {
  89. a.push(",")
  90. }
  91. a
  92. .push(
  93. this.encode(i),
  94. ":",
  95. v === null
  96. ? "null"
  97. : this
  98. .encode(v));
  99. b = true
  100. }
  101. }
  102. }
  103. a.push("}");
  104. return a.join("")
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. };
  112. this.decode = function(json) {
  113. return eval("(" + json + ")")
  114. }
  115. })();
  116. Ext.encode = Ext.util.JSON.encode;
  117. Ext.decode = Ext.util.JSON.decode;