0.0.111 • Published 5 years ago

@oresoftware/safe-stringify v0.0.111

Weekly downloads
81
License
SEE LICENSE IN LI...
Repository
github
Last release
5 years ago

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()
  )
);
0.0.111

5 years ago

0.0.109

5 years ago

0.0.108

5 years ago

0.0.107

5 years ago

0.0.1006

5 years ago

0.0.1005

5 years ago

0.0.1004

5 years ago

0.0.1003

5 years ago

0.0.1002

5 years ago

0.0.1001

6 years ago