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