Zorbing

stringifyCyclicJSON.js

Jun 27th, 2015
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function stringifyCyclicJSON(sourceObject, maxDepth = 5)
  2. {
  3.     var knownObjects = {};
  4.     var jsonString = '';
  5.     recStringifyObject(sourceObject, '', 1, 'root');
  6.     return jsonString;
  7.    
  8.     function recStringifyObject(currentObject, indent, level, path)
  9.     {
  10.         var isArray = (currentObject instanceof Array);
  11.         var subIndent = indent + '\t';
  12.         var i = 0;
  13.         jsonString += (isArray ? '[' : '{');
  14.         if (isArray)
  15.         {
  16.             for (; i < currentObject.length; i++)
  17.             {
  18.                 doSingleStringify(i, '');
  19.             }
  20.         }
  21.         else
  22.         {
  23.             for (var key in currentObject)
  24.             {
  25.                 if (currentObject[key] !== undefined)
  26.                 {
  27.                     doSingleStringify(key, '"' + key + '": ');
  28.                     i++;
  29.                 }
  30.             }
  31.         }
  32.         jsonString += (i > 0 ? '\n' + indent : ' ') + (isArray ? ']' : '}');
  33.        
  34.         function doSingleStringify(key, addBefore)
  35.         {
  36.             var value = currentObject[key];
  37.             jsonString += '\n'
  38.                 + subIndent
  39.                 + (i > 0 ? ', ' : '')
  40.                 + addBefore;
  41.             if (typeof value == 'object')
  42.             {
  43.                 // first search in known Objects
  44.                 var found = false;
  45.                 for (var pathKey in knownObjects)
  46.                 {
  47.                     if (knownObjects[pathKey] == value)
  48.                     {
  49.                         jsonString += '"[' + pathKey + ']"';
  50.                         found = true;
  51.                     }
  52.                 }
  53.                 if (!found)
  54.                 {
  55.                     var newPath = path + (isArray ? '[' + key + ']' : '.' + key);
  56.                     knownObjects[newPath] = value;
  57.                     if (level < maxDepth)
  58.                     {
  59.                         recStringifyObject(value, subIndent, level + 1, newPath);
  60.                     }
  61.                     else
  62.                     {
  63.                         jsonString += '"[' + (value instanceof Array ? 'Array' : 'Object') + ']"';
  64.                     }
  65.                 }
  66.             }
  67.             else if (typeof value == 'function')
  68.             {
  69.                 jsonString += '"[Function]"';
  70.             }
  71.             else
  72.             {
  73.                 jsonString += JSON.stringify(value);
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment