Giter VIP home page Giter VIP logo

json-set-map's Introduction

NPM version Build Status Coverage Status

json-set-map

Set and Map extension that support JSON serialization and de-serialization

The original Set and Map object are not modified, so can still be used.

This library does not provides polyfills for Set and Map.

Install

yarn add json-set-map

The current version supports node v10.x+ and modern browsers.

Usage

  • Typescript and es6
import { Set, Map } from 'json-set-map'
  • Nodejs
var Set = require('json-set-map').Set;
var Map = require('json-set-map').Map;

The unmodified Set and Map class can still be used:

import { Set as jSet, Map as jMap } from 'json-set-map'
let originalMap = new Map()
let originalSet = new Set()
let serializableMap = new jMap()
let serializableSet = new jSet()

API

Method toJSON

Called automaticly by JSON.stringify

// Set
> originalSet.add(1).add(2)
< Set { 1, 2 }
> JSON.stringify(originalSet)
< '{}'
> serializableSet.add(1).add(2)
< SerializableSet { 1, 2 }
> JSON.stringify(serializableSet)
< '[1,2]'

// Map
> originalMap.set(1,2).set(3,4)
< Map { 1 => 2, 3 => 4 }
> JSON.stringify(originalMap)
< '{}'
> serializableMap.add(1).add(2)
< SerializableMap { 1 => 2, 3 => 4 }
> JSON.stringify(serializableMap)
< '[[1,2],[3,4]]'

Method fromJSON

Static method useful to load a Set or Map from the object returned by JSON.parse. A parser function can be used to load the objects of the array as shown in the example below.

> import { Set, Map } from 'json-set-map'

// Used without a parser it's the same as calling new Set
> Set.fromJSON(["2016-05-16T22:26:47.534Z","2016-05-16T22:26:47.533Z"])
< SerializableSet { '2016-05-16T22:26:47.534Z', '2016-05-16T22:26:47.533Z' } // Items are strings

// A parser function can be used to load the object.
> let parser=(d) => new Date(d)
> Set.fromJSON(["2016-05-16T22:26:47.534Z","2016-05-16T22:26:47.533Z"], { parser })
< SerializableSet { 2016-05-16T22:26:47.534Z, 2016-05-16T22:26:47.533Z } // Items are Dates

// Used without a parser it's the same as calling new Map 
> Map.fromJSON([['KeY','ValuE']])
< SerializableMap { 'KeY' => 'ValuE' }

// Separate parser function for the key and the value can be used.
> let keyParser = (item) => item.toUpperCase()
> let valueParser = (item) => item.toLowerCase()
> Map.fromJSON([['KeY','ValuE']], {keyParser, valueParser})
< SerializableMap { 'KEY' => 'value' }

Refer to the JSDoc documentation on the files for mode details.

Node

JSON does not support undefined and is replaced with null. This prevents the correct de-serialization of a Set with both undefined and null as element or of a Map with both element as key.

const str = JSON.stringify(new jSet().add(null).add(undefined)) // => '[null,null]'
jSet.fromJSON(JSON.parse(str))                                  // => SerializableSet { null }

json-set-map's People

Contributors

caselit avatar dependabot[bot] avatar

Stargazers

 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.