
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
json-set-map
Advanced tools
Set and map extension that support JSON serialization and de-serialization
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.
yarn add json-set-map
The current version supports node v10.x+ and modern browsers.
import { Set, Map } from 'json-set-map'
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()
toJSONCalled 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]]'
fromJSONStatic 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.
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 }
FAQs
Set and map extension that support JSON serialization and de-serialization
We found that json-set-map demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.