json.decycled

2.1.1 • Public • Published

JSON.decycled

license Build Status npm version Github release npm downloads

Languages:[EN,ES]

Add to JSON JS .decycled() and .revive() method. They are a version of JSON.stringify() and JSON.parse() respectively, but support objects with circular references, regular expressions, Error and Date objects and functions.

  • Nodes where exists cyclic redundancy [Circular: reference] value appears, reference is the point where begins the circular reference indicated in dot notation.
JSON.decycled (val [, functions] [, deep])

Return JSON of the JS object val

  • val (object) object to decycled.
  • functions (boolean): true to show properties that are a function reference. By default is false. This properties have the format [Function:nombre function] and are not attend by .revive() function.
  • deep (integer): depth of the object to be analyzed.
JSON.revive (*val*)

Transform JSON structure in a JS object. If JSON is the result of .decycled() function; Date, RegExp and Error objects will revive.

  • val (object) JSON to "revive" in a JS object.
require('json.decycled');
 
var obj = {
  a: {
    a1:{
      'String': "String",
      'undefined': undefined,
      'Number': 14,
      'null': null,
      'Boolean': true,
      'Date': new Date(),
      'Regexp': /x+/,
      'Error': new Error('Opss'),
      'Array': [1,2,3,4,5],
      'function': function Hello(){}
    },
    a2:{
      'List': [
        {x:'X1',y:'Y1',z:'Z1'},
        {x:'X2',y:'Y2',z:'Z2'},
        {x:'X3',y:'Y3',z:'Z3'}
      ]
    }
  },
  b: {},
  c: {}
};
obj.b.a = obj;
obj.a.a1['Recursion'] = obj.a.a1;
console.log(obj);
console.log(JSON.decycled(obj));
console.log(JSON.revive(JSON.decycled(obj)));
 

Licence

MIT


JSON.decycled (es)

Añade a JSON nativo de JS el método .decycled() y el método .revive(). Son una versión de JSON.stringify() y JSON.parse() respectivamente, pero compatible con objetos con referencias circulares, expresiones regulares, objetos de error, objetos date y funciones.

  • En los nodos donde exista redundancia cíclica aparecerá el valor [Circular: reference], donde reference es el punto donde comienza la referencia circular indicado en notación por puntos.
JSON.decycled (val [, functions] [, deep])

Devuelve un JSON del objeto JS val.

  • val (object) objeto a realizar el decycled.
  • functions (boolean): true para mostrar las propiedades que hacen referencia a funciones. Por defecto es false. Estas propiedades tendrán el formato [Function:nombre function] y no son atendidas por la función .revive().
  • deep (integer): profundidad del objeto a analizar.
JSON.revive (*val*)

Convierte la estructura JSON en un Objeto JS. Si la estructura JSON viene dada por la función .decycled() se recreará los objetos Date, RegExp y Error.

  • val (object) JSON a "revivir" en un objeto JS.
require('json.decycled');
 
var obj = {
  a: {
    a1:{
      'String': "String",
      'undefined': undefined,
      'Number': 14,
      'null': null,
      'Boolean': true,
      'Date': new Date(),
      'Regexp': /x+/,
      'Error': new Error('Opss'),
      'Array': [1,2,3,4,5],
      'function': function Hello(){}
    },
    a2:{
      'List': [
        {x:'X1',y:'Y1',z:'Z1'},
        {x:'X2',y:'Y2',z:'Z2'},
        {x:'X3',y:'Y3',z:'Z3'}
      ]
    }
  },
  b: {},
  c: {}
};
obj.b.a = obj;
obj.a.a1['Recursion'] = obj.a.a1;
console.log(obj);
console.log(JSON.decycled(obj));
console.log(JSON.revive(JSON.decycled(obj)));
 

Licencia

MIT

Package Sidebar

Install

npm i json.decycled

Weekly Downloads

144

Version

2.1.1

License

MIT

Last publish

Collaborators

  • bifuer
  • lilxelo