npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js0xn

v0.2.1

Published

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

Downloads

42

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 ]) }