# @light0x00/jsonx

> A JSON extension for Javascript.

Latest version **0.1.1** (published 2020-03-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @light0x00/jsonx
pnpm add @light0x00/jsonx
yarn add @light0x00/jsonx
bun add @light0x00/jsonx
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2020-03-05 |
| First published | 2020-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 227.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Chen Yang |
| Maintainers | light0x00 |
| Keywords | json |

## Links

- npm: https://www.npmjs.com/package/@light0x00/jsonx
- Repository: https://github.com/light0x00/jsonx
- Homepage: https://github.com/light0x00/jsonx#readme
- Issues: https://github.com/light0x00/jsonx/issues
- npm.io page: https://npm.io/package/@light0x00/jsonx

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^4.1.1
- [typescript-collections](https://npm.io/package/typescript-collections.md) ^1.3.3
- [@parser-generator/definition](https://npm.io/package/@parser-generator/definition.md) ^0.1.0-alpha.0

## 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

- 0.1.1 (latest) — 2020-03-05
- 0.1.0 — 2020-03-05
- 0.0.6 — 2020-03-05
- 0.0.5 — 2020-03-05
- 0.0.4-alpha.3 — 2020-03-05
- 0.0.4-alpha.2 — 2020-03-05
- 0.0.4-alpha.1 — 2020-03-05
- 0.0.4-alpha — 2020-03-05
- 0.0.3 — 2020-03-05
- 0.0.2 — 2020-03-05
- 0.0.1 — 2020-03-04
- 0.0.0 — 2020-03-04

## README

# JSONX

A JSON Extension Implementation for Javascript.

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)  <a href="https://travis-ci.com/light0x00/jsonx"><img src="https://travis-ci.com/light0x00/jsonx.svg?branch=master"></a>  <a href="https://www.npmjs.com/package/@light0x00/jsonx"><img src="https://img.shields.io/npm/v/@light0x00/jsonx"></a>

[中文文档](./README.zh.md)

## Features

**Customizable converter**

Whenever matching data is encountered,  the specified converter-function is called,  and the return value determines the converting result of the matching data.

Two types of converters are supported:

- **class conveter** ,Match by Class (`xx.constructor`)
	```ts
	let jsonx = new JSONXBuilder()
		.addClassConverter(Date, (d: Date) => d.getFullYear()+"-"+d.getMonth())
		.build()
	jsonx.stringify({date:new Date()}  //output: "{"date":"2020-2"}"
	```
- **property conveter**, Match by property name

	```ts
	let jsonx = new JSONXBuilder()
		.addPropertyConverter("map", (obj: Map<any, any>) => Array.from(obj.entries()))
		.build()
	let r = jsonx.stringify( { map: new Map([["key1", "val1"]]) } )
	console.log(r)  //output: {"map":[["key1","val1"]]}
	```

**Loose analysis rules**

The comment、unclosed comma are supported

```ts
let r = jsonx.parse(`{
	/* Some comments */
	"target": "ESNext", //Some comments
	"lib": [
		"ESNext", /* multiline comment is supported */
		null  //null is supported
	], //unclosed comma is supported
}`)
console.log(r)  //output: {"target":"ESNext","lib": ["ESNext",null]}
```

**Detailed error reports**

When an error occurs, report the expected content, and the error location

```ts
let r = jsonx.parse(`{
	"target":"ESNext",
	:
 }`)

//Error: The expected input is "}" or "STRING" ,but got ":" at (3,1) ~ (3,2)
```

> The location format where error occurred: (start row,start column) ~ (end row,end column)


**Circular references detection**

When a circular reference detected, report the reference chain

```ts
class Member {
	name: string;
	lover?: Member;
	constructor(name: string) {
		this.name = name
	}
	toString() { return this.name }
}

let jack = new Member("jack")
let rose = new Member("rose")
let alice = new Member("alice")
let bob = new Member("bob")
jack.lover = rose
rose.lover = alice
alice.lover = bob
bob.lover = jack

jsonx.stringify(jack)
// Error: circular references detected: jack->rose->alice->bob->jack
```

## Install


**npm**

```bash
npm install @light0x00/jsonx
```

**browser**

```html
<script src="path/to/jsonx.min.js"></script>
<script>
	let {default: JSONX , JSONXBuilder } = JSONX_LIB 
</script>
```

## Usage

Support both ESModule and CommonJS, use ESModule as example below.

**Basic usage**

```ts
import JSONX from "@light0x00/jsonx"

JSONX.parse(`{"foo":"bar"}`)
JSONX.parse(`["foo","bar"]`)

JSONX.stringify({foo:"bar"})
JSONX.stringify(["foo","bar"])

//By default `Date` type is converted to : `dateObj.toISOString()`
let r  =JSONX.stringify([new Date()])
console.log(r)  //output: ["2020-03-04T06:24:02.927Z"]
```

**Customized usage**

```ts
import { JSONXBuilder } from "@light0x00/jsonx"

//Build a `JSONX` object according your demand
let jsonx = new JSONXBuilder()
	.addClassConverter(Date, (obj: Date) => obj.toLocaleDateString())
	.addPropertyConverter("relationships", (obj: Map<string, string>) => Array.from(obj.entries()))
	.build()

//Use it
let d = {
	name: "foo",
	date: new Date(),
	members: [
		{ name: "alice" },
		{ name: "bob" },
	],
	relationships: new Map<string, string>([["alice", "bob"]])
}
let r = jsonx.stringify(d)

//Result
should(r).eql(`{"name":"foo","date":"${d.date.toLocaleDateString()}","members":[{"name":"alice"},{"name":"bob"}],"relationships":[["alice","bob"]]}`)
```

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