# js0xn

> A Buffer encoding/decoding scheme for JSON with the aim of:

Latest version **0.2.1** (published 2017-09-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install js0xn
pnpm add js0xn
yarn add js0xn
bun add js0xn
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2017-09-08 |
| First published | 2017-07-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ates Goral |
| Maintainers | atesgoral |
| Keywords | json, buffer |

## Links

- npm: https://www.npmjs.com/package/js0xn
- Repository: https://github.com/atesgoral/js0xn
- Homepage: https://github.com/atesgoral/js0xn#readme
- Issues: https://github.com/atesgoral/js0xn/issues
- npm.io page: https://npm.io/package/js0xn

## 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.2.1 (latest) — 2017-09-08
- 0.2.0 — 2017-09-08
- 0.1.0 — 2017-08-28
- 0.0.2 — 2017-07-28
- 0.0.1 — 2017-07-27

## README

# js0xn

A Buffer encoding/decoding scheme for JSON with the aim of:

1. Keeping the payload human-readable
2. Minimizing bloat

It's still standard JSON. Buffers are encoded as strings.

For example, a Buffer instance with the bytes 1, 10, 100 gets encoded as the hex string `"0x010a64"`.

Regular strings are untouched, except getting escaped when they already start with a "0x".

## Installation

```
npm install --save js0xn
```

## Usage

```
const js0xn = require('js0xn');
```

### js0xn.stringify()

`js0xn.stringify()` stringifies a value just like `JSON.stringify()` does, with the additional handling of Buffer instances.

#### JSON-native types

Types that are native to JSON are handled the same way as `JSON.stringify()`.

```
js0xn.stringify(1) // '1'
js0xn.stringify(true) // 'true'
js0xn.stringify('hello') // '"hello"'
js0xn.stringify([ 1, 2, 'a', 'b' ]) // '[1,2,"a","b"]'
js0xn.stringify({ a: 1, b: 'foo', c: true }) // '{"a":1,"b":"foo","c":true}'
```

#### Buffer instances

Buffer instances are encoded as hex strings prefixed with "0x":

```
js0xn.stringify(Buffer.from([ 0x00, 0xc3, 0xff ])) // '"0x00c3ff"'
```

And they can be deeply nested:

```
js0xn.stringify({ a: [ Buffer.from([ 0x00, 0xc3, 0xff ]) ] }) // '{"a":["0x00c3ff"]}'
```

#### Strings that start with "0x"

If some strings happen to already start with the character sequence "0x", the sequence is escaped as "0xx" to disambiguate such strings from buffer strings:

```
js0xn.stringify('0xy6en') // '"0xxy6en"'
```

### js0xn.parse()

`js0xn.parse()` parses a JSON string just like `JSON.parse()` does, with the additional handling of buffer strings.

```
js0xn.parse('["hello","0xff"]') // [ 'hello', Buffer.from([ 255 ]) ]
js0xn.parse('"0xxym0r0n"') // '0xym0r0n'
```

### js0xn.encode()

`js0xn.encode()` does in-place encoding of Buffer instance values, even when deeply nested. Can be used to encode Buffer instances before passing down values for JSON stringification (e.g. when using Express, `response.body` will be set to an object for automatic JSON stringification, but encoding of Buffer instances is all that is needed.)

```
js0xn.encode({ b: Buffer.from([ 0x00, 0xc3, 0xff ]) }) // { b: '0x00c3ff' }
```

### js0xn.decode()

`js0xn.decode()` does in-place decoding of buffer strings, even when deeply nested. Can be used to decode buffer strings into Buffer instances when you already have values parsed from JSON (e.g. when the body-parser middleware for Express already parses JSON payload and presents it as `request.body` and all that is needed is to decode buffer strings.)

```
js0xn.decode({ b: '0x00c3ff' }) // { b: Buffer.from([ 0x00, 0xc3, 0xff ]) }
```

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