Created
April 19, 2019 06:58
-
-
Save piemme/2f9454033955ce496610cee93760c019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof JSON.decycle !== "function") { | |
JSON.decycle = function decycle(object, replacer) { | |
"use strict"; | |
var objects = new WeakMap(); // object to path mappings | |
return (function derez(value, path) { | |
var old_path; | |
var nu; | |
if (replacer !== undefined) { | |
value = replacer(value); | |
} | |
if ( | |
typeof value === "object" && value !== null && | |
!(value instanceof Boolean) && | |
!(value instanceof Date) && | |
!(value instanceof Number) && | |
!(value instanceof RegExp) && | |
!(value instanceof String) | |
) { | |
old_path = objects.get(value); | |
if (old_path !== undefined) { | |
return {$ref: old_path}; | |
} | |
objects.set(value, path); | |
if (Array.isArray(value)) { | |
nu = []; | |
value.forEach(function (element, i) { | |
nu[i] = derez(element, path + "[" + i + "]"); | |
}); | |
} else { | |
nu = {}; | |
Object.keys(value).forEach(function (name) { | |
nu[name] = derez( | |
value[name], | |
path + "[" + JSON.stringify(name) + "]" | |
); | |
}); | |
} | |
return nu; | |
} | |
return value; | |
}(object, "$")); | |
}; | |
} | |
if (typeof JSON.retrocycle !== "function") { | |
JSON.retrocycle = function retrocycle($) { | |
"use strict"; | |
var px = /^\$(?:\[(?:\d+|"(?:[^\\"\u0000-\u001f]|\\([\\"\/bfnrt]|u[0-9a-zA-Z]{4}))*")\])*$/; | |
(function rez(value) { | |
if (value && typeof value === "object") { | |
if (Array.isArray(value)) { | |
value.forEach(function (element, i) { | |
if (typeof element === "object" && element !== null) { | |
var path = element.$ref; | |
if (typeof path === "string" && px.test(path)) { | |
value[i] = eval(path); | |
} else { | |
rez(element); | |
} | |
} | |
}); | |
} else { | |
Object.keys(value).forEach(function (name) { | |
var item = value[name]; | |
if (typeof item === "object" && item !== null) { | |
var path = item.$ref; | |
if (typeof path === "string" && px.test(path)) { | |
value[name] = eval(path); | |
} else { | |
rez(item); | |
} | |
} | |
}); | |
} | |
} | |
}($)); | |
return $; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(console){ | |
console.save = function(data, filename){ | |
if(!data) { | |
console.error('Console.save: No data') | |
return; | |
} | |
if(!filename) filename = 'console.json' | |
if(typeof data === "object"){ | |
data = JSON.stringify(data, undefined, 4) | |
} | |
var blob = new Blob([data], {type: 'text/json'}), | |
e = document.createEvent('MouseEvents'), | |
a = document.createElement('a') | |
a.download = filename | |
a.href = window.URL.createObjectURL(blob) | |
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':') | |
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null) | |
a.dispatchEvent(e) | |
} | |
})(console) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From: https://stackoverflow.com/a/49606202/3368902
If the object already logged:
console.save(JSON.decycle(temp1));