Skip to content

About

store additional data types in JSON with ASCII escape character

Resources

Stars

4 stars

Watchers

1 watching

Forks

Latest commit

 

History

143 Commits

Folders and files

Repository files navigation

Arrow Code

Arrow Code extends JSON with NaN, infinities, undefined, bigint, Date, Uint8Array, and custom types. It stores encoded values as JSON strings prefixed by the combining character U+0362 "͢ ".

npm install arrow-code
import Arrow from "arrow-code";

const arrow = new Arrow({ encodeBigInt: true });
const json = arrow.encodeJSON([NaN, 9007199254740997n, new Date(0)]);
// ["͢NaN","͢n:9007199254740997","͢Date:1970-01-01T00:00:00.000Z"]

const [nan, bigint, date] = arrow.decodeJSON(json);

CommonJS can load the same API with const {default: Arrow} = require('arrow-code').

API

Method Behavior
encodeJSON(value, space?, sortKeys?) Encodes JSON. Pass true for sortKeys to sort object keys recursively.
decodeJSON(text) Parses JSON and decodes tagged values.
encode(value) Encodes a single value as a string. Plain strings pass through, with the prefix doubled if they start with U+0362.
decode(text) Decodes a single value.

All four methods are available on the class and on instances. Use an instance to configure built-in codecs or register custom types.

The JSON format reserves U+0362 at the start of string values. Unknown tags decode to undefined. When parsing an object or array, the JSON reviver removes those properties or leaves holes at those array indices. This also applies to encoded undefined array entries.

Constructor options

Option Default Behavior
encodeBinary true Encodes Uint8Array as Base93. Use 'base64' to write Base64. Both modes read both formats. Use false to disable both codecs.
encodeDate true Enables the Date codec.
encodeBigInt false Set to true to encode and decode BigInt values. With the codec disabled, JSON encoding of a BigInt throws and direct encoding returns null.

Custom types

Register a codec with an instance. The encoder returns the payload, and the decoder receives that payload.

import Arrow from "arrow-code";

class Message {
  constructor(text) {
    this.text = text;
  }
}

const arrow = new Arrow();
arrow.register(
  "Msg",
  Message,
  (message) => message.text,
  (text) => new Message(text),
);

const json = arrow.encodeJSON(new Message("hello")); // "͢Msg:hello"
arrow.decodeJSON(json); // Message { text: 'hello' }

Use registerRaw when the callbacks work with the full tagged string.

Development

Run pnpm install --frozen-lockfile and pnpm test with Node.js 22.18 or later. Node runs the TypeScript tests directly, and the tests load the built ESM and CommonJS entry points. Run pnpm benchmark to measure JSON encoding, sorted encoding, JSON decoding, and Base64 decoding on the current Node.js version.

About

store additional data types in JSON with ASCII escape character

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages