Giter VIP home page Giter VIP logo

enhancejson's Introduction

enhanceJSON

Stringify/parse object trees to/from JSON, with support for Map, Set and Date.

enhanceJSON offers drop-in replacements for JSON.parse and JSON.stringify, with identical signatures to those methods.

Version badge Minified badge Minzipped badge Dependencies badge Types badge NodeJS CI

Installation

Run npm install --save enhancejson

Usage

enhanceJSON is used in the same way as you'd call JSON.stringify / JSON.parse.

Internally, it uses a custom replacer and reviver to handle Map, Set and Date values. Objects representing these types are identified by means of a __type field, which is added automatically. You should avoid using this key in your own data. Alternatively, you can specify a different key to use via the typeKey parameter on each method.

You can specify your own replacer and/or reviver, which will be used before (for replacers) or after (for revivers) the internal ones, when calling stringify or parse.

import * as enhanceJSON from 'enhancejson';

const data = {
  val1: 'test',
  map: new Map([[1, 'one'], [2, 'two']]),
  child: {
    sets: [
      new Set([1, 2, 3]),
      new Set(['a', 'b', 'c']),
    ],
    date: new Date('2024-04-06'),
  }
}

const strData = enhanceJSON.stringify(data);
const newData = enhanceJSON.parse(strData);

expect(newData).toEqual(data);

expect(typeof strData).toEqual('string');
expect(strData).toEqual(`{"val1":"test","map":{"__type":"M","v":[[1,"one"],[2,"two"]]},"child":{"sets":[{"__type":"S","v":[1,2,3]},{"__type":"S","v":["a","b","c"]}],"date":{"__type":"D","v":"2024-04-06T00:00:00.000Z"}}}`);

JSON format

When the stringify function finds a Map, Set or Date value, it will output an object with a __type key of "M", "S" or "D", respectively. Another field on this same object will contain an array (for Map and Set) or string (for Date) representation of the original object.

When the parse function finds an object with this key, it assumes that it represents the corresponding data type, and will revive it as such.

enhancejson's People

Contributors

ftwinston avatar

Stargazers

 avatar

Watchers

 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.