@oresoftware/safe-stringify

Safely stringify objects with recursive references.

Usage no npm install needed!

<script type="module">
  import oresoftwareSafeStringify from 'https://cdn.skypack.dev/@oresoftware/safe-stringify';
</script>

README

Version

@oresoftware/safe-stringify

Motivation/purpose for this library:

See this Github gist

Installation:

$ npm i -S '@oresoftware/safe-stringify'

For most objects (this is more performant)

import * as safe from '@oresoftware/safe-stringify';
const s = safe.stringify({});

For use with more complex deeply-nested objects with arrays:

Note: stringifyDeep is not production ready, please don't use it yet, without improving it and making sure it works for you.

import * as safe from '@oresoftware/safe-stringify';
const s = safe.stringifyDeep([{}]);

For example the following works with stringifyDeep but not stringify:

const x = {dog:'bark'};
x.mmm = {'zebra': 3};
x.mmm = x;

const v = [x,x,x];
v.zzz = v;
v.foo = 5;
v.dog = 3;

const mmm = safe.stringifyDeep([v,v,v]);
console.log(mmm);

> Using Map and Set, etc

This library does not treat Map and Set or other classes as special. To serialize a Map or Set instance, you might do:


class HasMapAndSet {
  
  constructor(){
    this.map = new Map([['one',1], ['two',2]]);
    this.set = new Set([1,2,3]);
  }
   
  toJSON(){  // use this to transform Map and Set to {} or []
    return {
      map: Array.from(this.map),
      set: Array.from(this.set)
    }
  }
  
}


console.log(
  JSON.stringify(
    new HasMapAndSet()
  )
);