Skip to content

Instantly share code, notes, and snippets.

@Hoff97
Last active September 24, 2020 03:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hoff97/9842228 to your computer and use it in GitHub Desktop.
Save Hoff97/9842228 to your computer and use it in GitHub Desktop.
Special JSON object that is able to convert Objects with their methods and also converts cyclic objects.
//Erweiterte JSON, die auch spezielle Objekte parst und zu JSON umwandelt:
//Objekte, die sich selbst enthalten
//Objekte, die Funktionen enthalten
var JSONE = {};
JSONE.to = {};
JSONE.to.removeCycle = function(obj,already = [],lvl = 1,path="PATH#obj"){
if(typeof obj === "object")
{
for(var i in already)
{
if(already[i].obj===obj && (already[i].lvl<lvl-1||already[i].lvl==0))
{
return already[i].loc;
}
}
if(lvl===1)
{
already.push({obj:obj,loc:path,lvl:0});
}
for(var i in obj)
{
already.push({obj:obj[i],loc:path + "." + i,lvl:lvl});
}
for(var i in obj)
{
obj[i] = JSONE.to.removeCycle(obj[i],already,lvl+1,path + "." + i);
}
}
return obj;
}
JSONE.to.changeFuncs = function(obj)
{
if(typeof obj === "function")
{
return "FUNCTION#" + obj.toString();
}
else if(typeof obj === "object")
{
var cl = clone(obj);
for(var i in cl)
{
cl[i] = JSONE.to.changeFuncs(cl[i]);
}
return cl;
}
return obj;
}
JSONE.stringify = function(obj)
{
if(typeof obj === "object")
{
for(var i in JSONE.to)
{
obj = JSONE.to[i](obj);
}
}
return JSON.stringify(obj);
}
JSONE.from = {};
JSONE.from.addFuncs = function(obj)
{
if(typeof obj === "string" && obj.substr(0,9)==="FUNCTION#")
{
var ret;
try
{
eval("ret = " + obj.substr(9));
return ret;
}
catch(e)
{
return obj;
}
}
else if(typeof obj === "object")
{
for(var i in obj)
{
obj[i] = JSONE.from.addFuncs(obj[i]);
}
return obj;
}
return obj;
}
JSONE.from.addCycle = function(obj,paths = [],already = [],path="PATH#obj")
{
for(var i in already)
{
if(already[i]===obj)
{
return obj;
}
}
for(var i in paths)
{
if(paths[i].path===obj)
{
obj = paths[i].obj;
}
}
for(var i in already)
{
if(already[i]===obj)
{
return obj;
}
}
paths.push({path:path,obj:obj});
already.push(obj);
for(var i in obj)
{
obj[i] = JSONE.from.addCycle(obj[i],paths,already,path + "." + i);
}
return obj;
}
JSONE.parse = function(txt)
{
var obj = JSON.parse(txt);
if(typeof obj === "object")
{
for(var i in JSONE.from)
{
obj = JSONE.from[i](obj);
}
}
return obj;
}
//Funktion, die auch Objekte klont, die sich selbst enthalten
function clone(obj,already = [])
{
if(typeof obj === "object")
{
for(var i in already)
{
if(already[i].obj === obj)
{
return already[i].cl;
}
}
var cl = {};
already.push({obj:obj,cl:cl});
for(var i in obj)
{
cl[i] = clone(obj[i],already);
}
return cl;
}
return obj;
}
@david-a-wheeler
Copy link

What license? Public domain? MIT?

@skvskv
Copy link

skvskv commented Apr 12, 2016

Sound nice, yet how does one uses it?
I ain't getting nothing from JSONE.stringify(). No result, no error, no output, no nothing.

@skvskv
Copy link

skvskv commented Apr 12, 2016

Seems it really would fail upon complex circular object argument.
Chrome DevTools Console goes unresponsive for a minute or so, then it drops the error
'Uncaught DOMException: Failed to set the 'outerHTML' property on 'Element': This element has no parent node.(…)'
with call stack having huge pile of JSONE.to.removeCycle in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment