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.node.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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
  20. // should be a not so common char
  21. // possibly one JSON does not encode
  22. // possibly one encodeURIComponent does not encode
  23. // right now this char is '~' but this might change in the future
  24. specialChar = '~',
  25. safeSpecialChar = '\\x' + (
  26. '0' + specialChar.charCodeAt(0).toString(16)
  27. ).slice(-2),
  28. escapedSafeSpecialChar = '\\' + safeSpecialChar,
  29. specialCharRG = new RegExp(safeSpecialChar, 'g'),
  30. safeSpecialCharRG = new RegExp(escapedSafeSpecialChar, 'g'),
  31. safeStartWithSpecialCharRG = new RegExp('(?:^|([^\\\\]))' + escapedSafeSpecialChar),
  32. indexOf = [].indexOf || function(v){
  33. for(var i=this.length;i--&&this[i]!==v;);
  34. return i;
  35. },
  36. $String = String // there's no way to drop warnings in JSHint
  37. // about new String ... well, I need that here!
  38. // faked, and happy linter!
  39. ;
  40. function generateReplacer(value, replacer, resolve) {
  41. var
  42. path = [],
  43. all = [value],
  44. seen = [value],
  45. mapp = [resolve ? specialChar : '[Circular]'],
  46. last = value,
  47. lvl = 1,
  48. i
  49. ;
  50. return function(key, value) {
  51. // the replacer has rights to decide
  52. // if a new object should be returned
  53. // or if there's some key to drop
  54. // let's call it here rather than "too late"
  55. if (replacer) value = replacer.call(this, key, value);
  56. // did you know ? Safari passes keys as integers for arrays
  57. // which means if (key) when key === 0 won't pass the check
  58. if (key !== '') {
  59. if (last !== this) {
  60. i = lvl - indexOf.call(all, this) - 1;
  61. lvl -= i;
  62. all.splice(lvl, all.length);
  63. path.splice(lvl - 1, path.length);
  64. last = this;
  65. }
  66. // console.log(lvl, key, path);
  67. if (typeof value === 'object' && value) {
  68. // if object isn't referring to parent object, add to the
  69. // object path stack. Otherwise it is already there.
  70. if (indexOf.call(all, value) < 0) {
  71. all.push(last = value);
  72. }
  73. lvl = all.length;
  74. i = indexOf.call(seen, value);
  75. if (i < 0) {
  76. i = seen.push(value) - 1;
  77. if (resolve) {
  78. // key cannot contain specialChar but could be not a string
  79. path.push(('' + key).replace(specialCharRG, safeSpecialChar));
  80. mapp[i] = specialChar + path.join(specialChar);
  81. } else {
  82. mapp[i] = mapp[0];
  83. }
  84. } else {
  85. value = mapp[i];
  86. }
  87. } else {
  88. if (typeof value === 'string' && resolve) {
  89. // ensure no special char involved on deserialization
  90. // in this case only first char is important
  91. // no need to replace all value (better performance)
  92. value = value .replace(safeSpecialChar, escapedSafeSpecialChar)
  93. .replace(specialChar, safeSpecialChar);
  94. }
  95. }
  96. }
  97. return value;
  98. };
  99. }
  100. function retrieveFromPath(current, keys) {
  101. for(var i = 0, length = keys.length; i < length; current = current[
  102. // keys should be normalized back here
  103. keys[i++].replace(safeSpecialCharRG, specialChar)
  104. ]);
  105. return current;
  106. }
  107. function generateReviver(reviver) {
  108. return function(key, value) {
  109. var isString = typeof value === 'string';
  110. if (isString && value.charAt(0) === specialChar) {
  111. return new $String(value.slice(1));
  112. }
  113. if (key === '') value = regenerate(value, value, {});
  114. // again, only one needed, do not use the RegExp for this replacement
  115. // only keys need the RegExp
  116. if (isString) value = value .replace(safeStartWithSpecialCharRG, '$1' + specialChar)
  117. .replace(escapedSafeSpecialChar, safeSpecialChar);
  118. return reviver ? reviver.call(this, key, value) : value;
  119. };
  120. }
  121. function regenerateArray(root, current, retrieve) {
  122. for (var i = 0, length = current.length; i < length; i++) {
  123. current[i] = regenerate(root, current[i], retrieve);
  124. }
  125. return current;
  126. }
  127. function regenerateObject(root, current, retrieve) {
  128. for (var key in current) {
  129. if (current.hasOwnProperty(key)) {
  130. current[key] = regenerate(root, current[key], retrieve);
  131. }
  132. }
  133. return current;
  134. }
  135. function regenerate(root, current, retrieve) {
  136. return current instanceof Array ?
  137. // fast Array reconstruction
  138. regenerateArray(root, current, retrieve) :
  139. (
  140. current instanceof $String ?
  141. (
  142. // root is an empty string
  143. current.length ?
  144. (
  145. retrieve.hasOwnProperty(current) ?
  146. retrieve[current] :
  147. retrieve[current] = retrieveFromPath(
  148. root, current.split(specialChar)
  149. )
  150. ) :
  151. root
  152. ) :
  153. (
  154. current instanceof Object ?
  155. // dedicated Object parser
  156. regenerateObject(root, current, retrieve) :
  157. // value as it is
  158. current
  159. )
  160. )
  161. ;
  162. }
  163. function stringifyRecursion(value, replacer, space, doNotResolve) {
  164. return JSON.stringify(value, generateReplacer(value, replacer, !doNotResolve), space);
  165. }
  166. function parseRecursion(text, reviver) {
  167. return JSON.parse(text, generateReviver(reviver));
  168. }
  169. this.stringify = stringifyRecursion;
  170. this.parse = parseRecursion;