Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

circular-json.max.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*!
  2. Copyright (C) 2013 by WebReflection
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. var CircularJSON = (function(JSON, RegExp){
  20. var
  21. // should be a not so common char
  22. // possibly one JSON does not encode
  23. // possibly one encodeURIComponent does not encode
  24. // right now this char is '~' but this might change in the future
  25. specialChar = '~',
  26. safeSpecialChar = '\\x' + (
  27. '0' + specialChar.charCodeAt(0).toString(16)
  28. ).slice(-2),
  29. escapedSafeSpecialChar = '\\' + safeSpecialChar,
  30. specialCharRG = new RegExp(safeSpecialChar, 'g'),
  31. safeSpecialCharRG = new RegExp(escapedSafeSpecialChar, 'g'),
  32. safeStartWithSpecialCharRG = new RegExp('(?:^|([^\\\\]))' + escapedSafeSpecialChar),
  33. indexOf = [].indexOf || function(v){
  34. for(var i=this.length;i--&&this[i]!==v;);
  35. return i;
  36. },
  37. $String = String // there's no way to drop warnings in JSHint
  38. // about new String ... well, I need that here!
  39. // faked, and happy linter!
  40. ;
  41. function generateReplacer(value, replacer, resolve) {
  42. var
  43. path = [],
  44. all = [value],
  45. seen = [value],
  46. mapp = [resolve ? specialChar : '[Circular]'],
  47. last = value,
  48. lvl = 1,
  49. i
  50. ;
  51. return function(key, value) {
  52. // the replacer has rights to decide
  53. // if a new object should be returned
  54. // or if there's some key to drop
  55. // let's call it here rather than "too late"
  56. if (replacer) value = replacer.call(this, key, value);
  57. // did you know ? Safari passes keys as integers for arrays
  58. // which means if (key) when key === 0 won't pass the check
  59. if (key !== '') {
  60. if (last !== this) {
  61. i = lvl - indexOf.call(all, this) - 1;
  62. lvl -= i;
  63. all.splice(lvl, all.length);
  64. path.splice(lvl - 1, path.length);
  65. last = this;
  66. }
  67. // console.log(lvl, key, path);
  68. if (typeof value === 'object' && value) {
  69. // if object isn't referring to parent object, add to the
  70. // object path stack. Otherwise it is already there.
  71. if (indexOf.call(all, value) < 0) {
  72. all.push(last = value);
  73. }
  74. lvl = all.length;
  75. i = indexOf.call(seen, value);
  76. if (i < 0) {
  77. i = seen.push(value) - 1;
  78. if (resolve) {
  79. // key cannot contain specialChar but could be not a string
  80. path.push(('' + key).replace(specialCharRG, safeSpecialChar));
  81. mapp[i] = specialChar + path.join(specialChar);
  82. } else {
  83. mapp[i] = mapp[0];
  84. }
  85. } else {
  86. value = mapp[i];
  87. }
  88. } else {
  89. if (typeof value === 'string' && resolve) {
  90. // ensure no special char involved on deserialization
  91. // in this case only first char is important
  92. // no need to replace all value (better performance)
  93. value = value .replace(safeSpecialChar, escapedSafeSpecialChar)
  94. .replace(specialChar, safeSpecialChar);
  95. }
  96. }
  97. }
  98. return value;
  99. };
  100. }
  101. function retrieveFromPath(current, keys) {
  102. for(var i = 0, length = keys.length; i < length; current = current[
  103. // keys should be normalized back here
  104. keys[i++].replace(safeSpecialCharRG, specialChar)
  105. ]);
  106. return current;
  107. }
  108. function generateReviver(reviver) {
  109. return function(key, value) {
  110. var isString = typeof value === 'string';
  111. if (isString && value.charAt(0) === specialChar) {
  112. return new $String(value.slice(1));
  113. }
  114. if (key === '') value = regenerate(value, value, {});
  115. // again, only one needed, do not use the RegExp for this replacement
  116. // only keys need the RegExp
  117. if (isString) value = value .replace(safeStartWithSpecialCharRG, '$1' + specialChar)
  118. .replace(escapedSafeSpecialChar, safeSpecialChar);
  119. return reviver ? reviver.call(this, key, value) : value;
  120. };
  121. }
  122. function regenerateArray(root, current, retrieve) {
  123. for (var i = 0, length = current.length; i < length; i++) {
  124. current[i] = regenerate(root, current[i], retrieve);
  125. }
  126. return current;
  127. }
  128. function regenerateObject(root, current, retrieve) {
  129. for (var key in current) {
  130. if (current.hasOwnProperty(key)) {
  131. current[key] = regenerate(root, current[key], retrieve);
  132. }
  133. }
  134. return current;
  135. }
  136. function regenerate(root, current, retrieve) {
  137. return current instanceof Array ?
  138. // fast Array reconstruction
  139. regenerateArray(root, current, retrieve) :
  140. (
  141. current instanceof $String ?
  142. (
  143. // root is an empty string
  144. current.length ?
  145. (
  146. retrieve.hasOwnProperty(current) ?
  147. retrieve[current] :
  148. retrieve[current] = retrieveFromPath(
  149. root, current.split(specialChar)
  150. )
  151. ) :
  152. root
  153. ) :
  154. (
  155. current instanceof Object ?
  156. // dedicated Object parser
  157. regenerateObject(root, current, retrieve) :
  158. // value as it is
  159. current
  160. )
  161. )
  162. ;
  163. }
  164. function stringifyRecursion(value, replacer, space, doNotResolve) {
  165. return JSON.stringify(value, generateReplacer(value, replacer, !doNotResolve), space);
  166. }
  167. function parseRecursion(text, reviver) {
  168. return JSON.parse(text, generateReviver(reviver));
  169. }
  170. return {
  171. stringify: stringifyRecursion,
  172. parse: parseRecursion
  173. };
  174. }(JSON, RegExp));