UNPKG

6.85 kBJavaScriptView Raw
1/*!
2Copyright (C) 2013-2017 by Andrea Giammarchi - @WebReflection
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21
22*/
23var CircularJSON = (function(JSON, RegExp){
24var
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
29 specialChar = '~',
30 safeSpecialChar = '\\x' + (
31 '0' + specialChar.charCodeAt(0).toString(16)
32 ).slice(-2),
33 escapedSafeSpecialChar = '\\' + safeSpecialChar,
34 specialCharRG = new RegExp(safeSpecialChar, 'g'),
35 safeSpecialCharRG = new RegExp(escapedSafeSpecialChar, 'g'),
36
37 safeStartWithSpecialCharRG = new RegExp('(?:^|([^\\\\]))' + escapedSafeSpecialChar),
38
39 indexOf = [].indexOf || function(v){
40 for(var i=this.length;i--&&this[i]!==v;);
41 return i;
42 },
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!
46;
47
48function generateReplacer(value, replacer, resolve) {
49 var
50 doNotIgnore = false,
51 inspect = !!replacer,
52 path = [],
53 all = [value],
54 seen = [value],
55 mapp = [resolve ? specialChar : '[Circular]'],
56 last = value,
57 lvl = 1,
58 i, fn
59 ;
60 if (inspect) {
61 fn = typeof replacer === 'object' ?
62 function (key, value) {
63 return key !== '' && replacer.indexOf(key) < 0 ? void 0 : value;
64 } :
65 replacer;
66 }
67 return function(key, value) {
68 // the replacer has rights to decide
69 // if a new object should be returned
70 // or if there's some key to drop
71 // let's call it here rather than "too late"
72 if (inspect) value = fn.call(this, key, value);
73
74 // first pass should be ignored, since it's just the initial object
75 if (doNotIgnore) {
76 if (last !== this) {
77 i = lvl - indexOf.call(all, this) - 1;
78 lvl -= i;
79 all.splice(lvl, all.length);
80 path.splice(lvl - 1, path.length);
81 last = this;
82 }
83 // console.log(lvl, key, path);
84 if (typeof value === 'object' && value) {
85 // if object isn't referring to parent object, add to the
86 // object path stack. Otherwise it is already there.
87 if (indexOf.call(all, value) < 0) {
88 all.push(last = value);
89 }
90 lvl = all.length;
91 i = indexOf.call(seen, value);
92 if (i < 0) {
93 i = seen.push(value) - 1;
94 if (resolve) {
95 // key cannot contain specialChar but could be not a string
96 path.push(('' + key).replace(specialCharRG, safeSpecialChar));
97 mapp[i] = specialChar + path.join(specialChar);
98 } else {
99 mapp[i] = mapp[0];
100 }
101 } else {
102 value = mapp[i];
103 }
104 } else {
105 if (typeof value === 'string' && resolve) {
106 // ensure no special char involved on deserialization
107 // in this case only first char is important
108 // no need to replace all value (better performance)
109 value = value .replace(safeSpecialChar, escapedSafeSpecialChar)
110 .replace(specialChar, safeSpecialChar);
111 }
112 }
113 } else {
114 doNotIgnore = true;
115 }
116 return value;
117 };
118}
119
120function retrieveFromPath(current, keys) {
121 for(var i = 0, length = keys.length; i < length; current = current[
122 // keys should be normalized back here
123 keys[i++].replace(safeSpecialCharRG, specialChar)
124 ]);
125 return current;
126}
127
128function generateReviver(reviver) {
129 return function(key, value) {
130 var isString = typeof value === 'string';
131 if (isString && value.charAt(0) === specialChar) {
132 return new $String(value.slice(1));
133 }
134 if (key === '') value = regenerate(value, value, {});
135 // again, only one needed, do not use the RegExp for this replacement
136 // only keys need the RegExp
137 if (isString) value = value .replace(safeStartWithSpecialCharRG, '$1' + specialChar)
138 .replace(escapedSafeSpecialChar, safeSpecialChar);
139 return reviver ? reviver.call(this, key, value) : value;
140 };
141}
142
143function regenerateArray(root, current, retrieve) {
144 for (var i = 0, length = current.length; i < length; i++) {
145 current[i] = regenerate(root, current[i], retrieve);
146 }
147 return current;
148}
149
150function regenerateObject(root, current, retrieve) {
151 for (var key in current) {
152 if (current.hasOwnProperty(key)) {
153 current[key] = regenerate(root, current[key], retrieve);
154 }
155 }
156 return current;
157}
158
159function regenerate(root, current, retrieve) {
160 return current instanceof Array ?
161 // fast Array reconstruction
162 regenerateArray(root, current, retrieve) :
163 (
164 current instanceof $String ?
165 (
166 // root is an empty string
167 current.length ?
168 (
169 retrieve.hasOwnProperty(current) ?
170 retrieve[current] :
171 retrieve[current] = retrieveFromPath(
172 root, current.split(specialChar)
173 )
174 ) :
175 root
176 ) :
177 (
178 current instanceof Object ?
179 // dedicated Object parser
180 regenerateObject(root, current, retrieve) :
181 // value as it is
182 current
183 )
184 )
185 ;
186}
187
188var CircularJSON = {
189 stringify: function stringify(value, replacer, space, doNotResolve) {
190 return CircularJSON.parser.stringify(
191 value,
192 generateReplacer(value, replacer, !doNotResolve),
193 space
194 );
195 },
196 parse: function parse(text, reviver) {
197 return CircularJSON.parser.parse(
198 text,
199 generateReviver(reviver)
200 );
201 },
202 // A parser should be an API 1:1 compatible with JSON
203 // it should expose stringify and parse methods.
204 // The default parser is the native JSON.
205 parser: JSON
206};
207
208return CircularJSON;
209
210}(JSON, RegExp));
\No newline at end of file