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
24 // should be a not so common char
25 // possibly one JSON does not encode
26 // possibly one encodeURIComponent does not encode
27 // right now this char is '~' but this might change in the future
29 safeSpecialChar = '\\x' + (
30 '0' + specialChar.charCodeAt(0).toString(16)
32 escapedSafeSpecialChar = '\\' + safeSpecialChar,
33 specialCharRG = new RegExp(safeSpecialChar, 'g'),
34 safeSpecialCharRG = new RegExp(escapedSafeSpecialChar, 'g'),
36 safeStartWithSpecialCharRG = new RegExp('(?:^|([^\\\\]))' + escapedSafeSpecialChar),
38 indexOf = [].indexOf || function(v){
39 for(var i=this.length;i--&&this[i]!==v;);
42 $String = String // there's no way to drop warnings in JSHint
43 // about new String ... well, I need that here!
44 // faked, and happy linter!
47 function generateReplacer(value, replacer, resolve) {
52 mapp = [resolve ? specialChar : '[Circular]'],
57 return function(key, value) {
58 // the replacer has rights to decide
59 // if a new object should be returned
60 // or if there's some key to drop
61 // let's call it here rather than "too late"
62 if (replacer) value = replacer.call(this, key, value);
64 // did you know ? Safari passes keys as integers for arrays
65 // which means if (key) when key === 0 won't pass the check
68 i = lvl - indexOf.call(all, this) - 1;
70 all.splice(lvl, all.length);
71 path.splice(lvl - 1, path.length);
74 // console.log(lvl, key, path);
75 if (typeof value === 'object' && value) {
76 // if object isn't referring to parent object, add to the
77 // object path stack. Otherwise it is already there.
78 if (indexOf.call(all, value) < 0) {
79 all.push(last = value);
82 i = indexOf.call(seen, value);
84 i = seen.push(value) - 1;
86 // key cannot contain specialChar but could be not a string
87 path.push(('' + key).replace(specialCharRG, safeSpecialChar));
88 mapp[i] = specialChar + path.join(specialChar);
96 if (typeof value === 'string' && resolve) {
97 // ensure no special char involved on deserialization
98 // in this case only first char is important
99 // no need to replace all value (better performance)
100 value = value .replace(safeSpecialChar, escapedSafeSpecialChar)
101 .replace(specialChar, safeSpecialChar);
109 function retrieveFromPath(current, keys) {
110 for(var i = 0, length = keys.length; i < length; current = current[
111 // keys should be normalized back here
112 keys[i++].replace(safeSpecialCharRG, specialChar)
117 function generateReviver(reviver) {
118 return function(key, value) {
119 var isString = typeof value === 'string';
120 if (isString && value.charAt(0) === specialChar) {
121 return new $String(value.slice(1));
123 if (key === '') value = regenerate(value, value, {});
124 // again, only one needed, do not use the RegExp for this replacement
125 // only keys need the RegExp
126 if (isString) value = value .replace(safeStartWithSpecialCharRG, '$1' + specialChar)
127 .replace(escapedSafeSpecialChar, safeSpecialChar);
128 return reviver ? reviver.call(this, key, value) : value;
132 function regenerateArray(root, current, retrieve) {
133 for (var i = 0, length = current.length; i < length; i++) {
134 current[i] = regenerate(root, current[i], retrieve);
139 function regenerateObject(root, current, retrieve) {
140 for (var key in current) {
141 if (current.hasOwnProperty(key)) {
142 current[key] = regenerate(root, current[key], retrieve);
148 function regenerate(root, current, retrieve) {
149 return current instanceof Array ?
150 // fast Array reconstruction
151 regenerateArray(root, current, retrieve) :
153 current instanceof $String ?
155 // root is an empty string
158 retrieve.hasOwnProperty(current) ?
160 retrieve[current] = retrieveFromPath(
161 root, current.split(specialChar)
167 current instanceof Object ?
168 // dedicated Object parser
169 regenerateObject(root, current, retrieve) :
177 function stringifyRecursion(value, replacer, space, doNotResolve) {
178 return JSON.stringify(value, generateReplacer(value, replacer, !doNotResolve), space);
181 function parseRecursion(text, reviver) {
182 return JSON.parse(text, generateReviver(reviver));
184 this.stringify = stringifyRecursion;
185 this.parse = parseRecursion;