# json-enhancer

> Extension JavaScript native parser for ES new features.

Latest version **1.1.0** (published 2021-09-24) · ISC license · 0 weekly downloads

## Install

```sh
npm install json-enhancer
pnpm add json-enhancer
yarn add json-enhancer
bun add json-enhancer
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2021-09-24 |
| First published | 2021-09-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 25.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | galenyoung |
| Maintainers | galenyoung |
| Keywords | js, json, ES6, json parser, json enhancer, decodeJSON, encodeJSON |

## Links

- npm: https://www.npmjs.com/package/json-enhancer
- Repository: https://github.com/NickLJudy/json-enhancer
- Homepage: https://github.com/NickLJudy/json-enhancer#readme
- Issues: https://github.com/NickLJudy/json-enhancer/issues
- npm.io page: https://npm.io/package/json-enhancer

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.0 (latest) — 2021-09-24
- 1.0.4 — 2021-09-16
- 1.0.3 — 2021-09-14
- 1.0.2 — 2021-09-13
- 1.0.1 — 2021-09-13
- 1.0.0 — 2021-09-11

## README

# json-enhancer

Extension JavaScript native parser for ES new features.

[![Build Status](https://travis-ci.com/NickLJudy/json-enhancer.svg?branch=main)](https://travis-ci.com/NickLJudy/json-enhancer)
[![Coverage Status](https://coveralls.io/repos/github/NickLJudy/json-enhancer/badge.svg?branch=main)](https://coveralls.io/github/NickLJudy/json-enhancer?branch=main)
[![Version](https://img.shields.io/npm/v/json-enhancer.svg?maxAge=300&label=version&colorB=007ec6&maxAge=300)](./package.json)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/json-enhancer)](https://bundlephobia.com/package/json-enhancer)
![dependencies-logo](https://status.david-dm.org/gh/NickLJudy/json-enhancer.svg)
![last-commit-date-logo](https://img.shields.io/github/last-commit/NickLJudy/json-enhancer)
## Installation

The json-enhancer package lives in [npm](https://www.npmjs.com/get-npm). To install the latest stable version, run the following command:

```shell
npm i json-enhancer
```

Or if you're using [yarn](https://classic.yarnpkg.com/en/docs/install/):

```shell
yarn add json-enhancer
```

## Usage

```js
import {decodeJSON, encodeJSON} from 'json-enhancer';

/* big number */
decodeJSON('9007199254740997')  //'9007199254740997'

/* date */
const d = new Date()
const dCopy = decodeJSON(encodeJSON(d));

dCopy.toISOString() === d.toISOString();  //true

/* Omit null, undefined, NaN */
const obj = {
    prop0:0,
    prop1:null,
    prop2:undefined,
    prop3:NaN,
};

decodeJSON(encodeJSON(obj)) //{prop0: 0}

/* Regular expression */
const reg = new RegExp('ab+c', 'i');
const regCopy = decodeJSON(encodeJSON(reg));

reg.toString() === regCopy.toString() //true
regCopy instanceof RegExp //true

/* bigint */
const b = 10n;
const obj = {b};

decodeJSON(encodeJSON(obj));  //{b: '10'}

/* Set */
const b = 10n;
const s = new Set([b,'a',1,'10',2,10,]);

//If the set contains bigInt, json-enhancer parses the value as a numeric string. If the set contains the same numeric string, only one value is retained.
decodeJSON(encodeJSON(s));  //Set(5) {'10', 'a', 1, 2, 10}


/* Map */
let myMap = new Map();

let keyObj = {};
let keyString = 'a string';
let keyNull = null;

myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, 'value associated with keyObj');
myMap.set(keyNull, 'value associated with keyNull');

const targetMap = decodeJSON(encodeJSON(myMap));
 
/*     
targetMap:
    Map(3) {
      'a string' => "value associated with 'a string'",
      {} => 'value associated with keyObj',
      undefined => 'value associated with keyNull'
    } 
*/

/* Symbol */
const sym = Symbol('foo');
const obj = {sym};

decodeJSON(encodeJSON(obj));  //{sym: Symbol(foo)}

```

## Objective

Solve the problem of **big number** precision loss, using string representation.

**Methods** in an object are not lost after parsing.

**Date** objects are typed unchanged after parsing.

If the value of plain object is **null** / **undefiend** / **NaN**, it will be discarded.

**null**,**undefiend**,**NaN** will be treated as **undefined**. 

**Regular expression** parsing is supported.

**Bigint** parsing is supported, using string representation.

Support **Set** and **Map** data type resolution.

**Symbol** is passed to json-enhancer as a value that will be redeclared after parsing.

## Notice

WeakMap and WeakSet will not be handled.

Symbol as the key in the plain object will be omitted.

---
_Source: https://npm.io/package/json-enhancer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
