# pure-json

> Pure version of built-in JSON Object

Latest version **1.0.0-beta.1** (published 2022-07-10) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-beta.1 |
| Published | 2022-07-10 |
| First published | 2022-07-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | miyauci |
| Keywords | JSON, parse, stringify, pure |

## Links

- npm: https://www.npmjs.com/package/pure-json
- Repository: https://github.com/TomokiMiyauci/pure-JSON
- Issues: https://github.com/TomokiMiyauci/pure-JSON/issues
- npm.io page: https://npm.io/package/pure-json

## 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.0.0-beta.1 (latest) — 2022-07-10

## README

# pure-JSON

[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno&labelColor=black&color=black)](https://deno.land/x/pure_json)
[![deno doc](https://img.shields.io/badge/deno-doc-black)](https://doc.deno.land/https/deno.land/x/pure_json/mod.ts)

Pure version of built-in JSON Object

`JSON#parse` and `JSON#stringify` without throwing an error and `any` types.

## Why?

Methods of the `JSON` object in Built-in are not pure functions and may throw
errors. Also, as of TypeScript 4.7, there is a divergence between implementation
and types.

For example, `JSON#stringify` may return `undefined` for historical reasons.
However, the return type is `string`.

```ts
JSON.stringify(undefined); // undefined
```

This project will resolve these. The return value returns the return value of
the function and the error, as in GoLang. (But as a tuple.)

## Example

### JSON#parse

```ts
import { JSON } from "https://deno.land/x/pure_json@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

const [data, err] = JSON.parse(`{ "hello": "world"}`);

assertEquals(err, undefined);
assertEquals(data, { hello: "world" });
```

### JSON#stringify

```ts
import { JSON } from "https://deno.land/x/pure_json@$VERSION/mod.ts";
import {
  assertEquals,
  assertIsError,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";

const cycle: Record<string, unknown> = {};
cycle[""] = cycle;

const [data, err] = JSON.stringify(cycle);

assertIsError(err, TypeError);
assertEquals(data, undefined);
```

## API

### JSON

An intrinsic object that provides functions to convert JavaScript values to and
from the JavaScript Object Notation (JSON) format.

### parse

Safe converts a JavaScript Object Notation (JSON) string into an object. It does
not throw errors compared to `JSON.parse`.

```ts
import { parse } from "https://deno.land/x/pure_json@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std@$VERSION/testing/asserts.ts";

const [data, err] = parse(`{ "hello": "world"}`);

assertEquals(err, undefined);
assertEquals(data, { hello: "world" });
```

#### Parameters

- text: `string`
- reviver: `Parameters<typeof JSON.parse>[1]`

#### Return Type

`[data: json, err: undefined]` | `[data: undefined, err: SyntaxError]`

### stringify

Safe converts a JavaScript value to a JavaScript Object Notation (JSON) string.

```ts
import { stringify } from "https://deno.land/x/pure_json@$VERSION/mod.ts";
import {
  assertEquals,
  assertIsError,
} from "https://deno.land/std@$VERSION/testing/asserts.ts";

const cycle: Record<string, unknown> = {};
cycle[""] = cycle;

const [data, err] = stringify(cycle);

assertIsError(err, TypeError);
assertEquals(data, undefined);
```

#### Parameters

- value: `unknown`
- replacer?: `(number | string)[] | null`
- space?: `string | number`

#### Return Type

`[valid: false, error: TypeError] | [valid: true, data: string]`

### json

Types for JSON.

```ts
type json =
  | string
  | number
  | boolean
  | null
  | { [k: string]: json }
  | json[];
```

## License

Copyright © 2022-present [TomokiMiyauci](https://github.com/TomokiMiyauci).

Released under the [MIT](./LICENSE) license

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