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

/wp-content/plugins/admin-menu-editor/jquery.json-1.3.js

https://gitlab.com/endomorphosis/reservationtelco
JavaScript | 156 lines | 107 code | 18 blank | 31 comment | 34 complexity | 8ae8435eac89c48a5a2080806799df1f MD5 | raw file
  1. /*
  2. * jQuery JSON Plugin
  3. * version: 1.0 (2008-04-17)
  4. *
  5. * This document is licensed as free software under the terms of the
  6. * MIT License: http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * Brantley Harris technically wrote this plugin, but it is based somewhat
  9. * on the JSON.org website's http://www.json.org/json2.js, which proclaims:
  10. * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
  11. * I uphold. I really just cleaned it up.
  12. *
  13. * It is also based heavily on MochiKit's serializeJSON, which is
  14. * copywrited 2005 by Bob Ippolito.
  15. */
  16. (function($) {
  17. function toIntegersAtLease(n)
  18. // Format integers to have at least two digits.
  19. {
  20. return n < 10 ? '0' + n : n;
  21. }
  22. Date.prototype.toJSON = function(date)
  23. // Yes, it polutes the Date namespace, but we'll allow it here, as
  24. // it's damned usefull.
  25. {
  26. return this.getUTCFullYear() + '-' +
  27. toIntegersAtLease(this.getUTCMonth()) + '-' +
  28. toIntegersAtLease(this.getUTCDate());
  29. };
  30. var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
  31. var meta = { // table of character substitutions
  32. '\b': '\\b',
  33. '\t': '\\t',
  34. '\n': '\\n',
  35. '\f': '\\f',
  36. '\r': '\\r',
  37. '"' : '\\"',
  38. '\\': '\\\\'
  39. };
  40. $.quoteString = function(string)
  41. // Places quotes around a string, inteligently.
  42. // If the string contains no control characters, no quote characters, and no
  43. // backslash characters, then we can safely slap some quotes around it.
  44. // Otherwise we must also replace the offending characters with safe escape
  45. // sequences.
  46. {
  47. if (escapeable.test(string))
  48. {
  49. return '"' + string.replace(escapeable, function (a)
  50. {
  51. var c = meta[a];
  52. if (typeof c === 'string') {
  53. return c;
  54. }
  55. c = a.charCodeAt();
  56. return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
  57. }) + '"';
  58. }
  59. return '"' + string + '"';
  60. };
  61. $.toJSON = function(o, compact)
  62. {
  63. var type = typeof(o);
  64. if (type == "undefined")
  65. return "undefined";
  66. else if (type == "number" || type == "boolean")
  67. return o + "";
  68. else if (o === null)
  69. return "null";
  70. // Is it a string?
  71. if (type == "string")
  72. {
  73. return $.quoteString(o);
  74. }
  75. // Does it have a .toJSON function?
  76. if (type == "object" && typeof o.toJSON == "function")
  77. return o.toJSON(compact);
  78. // Is it an array?
  79. if (type != "function" && typeof(o.length) == "number")
  80. {
  81. var ret = [];
  82. for (var i = 0; i < o.length; i++) {
  83. ret.push( $.toJSON(o[i], compact) );
  84. }
  85. if (compact)
  86. return "[" + ret.join(",") + "]";
  87. else
  88. return "[" + ret.join(", ") + "]";
  89. }
  90. // If it's a function, we have to warn somebody!
  91. if (type == "function") {
  92. throw new TypeError("Unable to convert object of type 'function' to json.");
  93. }
  94. // It's probably an object, then.
  95. var ret = [];
  96. for (var k in o) {
  97. var name;
  98. type = typeof(k);
  99. if (type == "number")
  100. name = '"' + k + '"';
  101. else if (type == "string")
  102. name = $.quoteString(k);
  103. else
  104. continue; //skip non-string or number keys
  105. var val = $.toJSON(o[k], compact);
  106. if (typeof(val) != "string") {
  107. // skip non-serializable values
  108. continue;
  109. }
  110. if (compact)
  111. ret.push(name + ":" + val);
  112. else
  113. ret.push(name + ": " + val);
  114. }
  115. return "{" + ret.join(", ") + "}";
  116. };
  117. $.compactJSON = function(o)
  118. {
  119. return $.toJSON(o, true);
  120. };
  121. $.evalJSON = function(src)
  122. // Evals JSON that we know to be safe.
  123. {
  124. return eval("(" + src + ")");
  125. };
  126. $.secureEvalJSON = function(src)
  127. // Evals JSON in a way that is *more* secure.
  128. {
  129. var filtered = src;
  130. filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
  131. filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
  132. filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
  133. if (/^[\],:{}\s]*$/.test(filtered))
  134. return eval("(" + src + ")");
  135. else
  136. throw new SyntaxError("Error parsing JSON, source is not valid.");
  137. };
  138. })(jQuery);