Coder Social home page Coder Social logo

oresoftware / safe-stringify Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 0.0 27 KB

Handles circular objects/references for JS to JSON serialization.

License: MIT License

JavaScript 34.29% Dockerfile 2.52% Shell 17.89% TypeScript 45.30%
json json-api json-server nodejs javascript javascript-library npm-package npm

safe-stringify's Introduction

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

safe-stringify's People

Contributors

oresoftware avatar the1mills avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.