0.23.1 • Published 6 months ago

tupleson v0.23.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Customizable serialization & deserialization. Serialize almost^1 anything!

^1: 🌀 Circular references not your thing? We agree & we don't support it. But hey, feel free to make a PR add opt-in support for that if you need it!

🎯 Project Goals

  • 💡 JSON-compatible output
  • 📖 Human-readable output
  • 🔧 Customizable behavior – tailor it to your exact needs
  • 🌊 Serialize & stream things like Promises or async iterators

!IMPORTANT
Though well-tested, this package might undergo big changes and does not follow semver whilst on 0.x.y-version, stay tuned!

👀 Example

/* eslint-disable @typescript-eslint/no-unused-vars, n/no-missing-import */

import {
	// Create serializer / deserializer
	createTson,
	// Serialize `bigint`
	tsonBigint,
	// Serialize `Date`
	tsonDate,
	// Serialize `Map`s
	tsonMap,
	// **throws** when encountering Infinity or NaN
	tsonNumberGuard,
	// Serialize regular expression
	tsonRegExp,
	// Serialize `Set`s
	tsonSet,
	// serialize a Symbol
	tsonSymbol,
	// Serialize `URL`s
	tsonURL,
	// Serialize `undefined`
	tsonUndefined,
	// **throws** when encountering non-registered complex objects (like class instances)
	tsonUnknownObjectGuard,
} from "tupleson";

const tson = createTson({
	/**
	 * The nonce function every time we start serializing a new object
	 * Should return a unique value every time it's called
	 * @default `${crypto.randomUUID} if available, otherwise a random string generated by Math.random`
	 */
	// nonce: () => "__tson",
	types: [
		// Pick which types you want to support
		tsonSet,
	],
});

const myObj = {
	foo: "bar",
	set: new Set([1, 2, 3]),
};

const str = tson.stringify(myObj, 2);
console.log(str);
// (👀 All non-JSON values are replaced with a tuple, hence the name)

// ->
// {
//   "json": {
//     "foo": "bar",
//     "set": [
//       "Set",
//       [
//         1,
//         2,
//         3
//       ],
//       "__tson"
//     ]
//   },
//   "nonce": "__tson"
// }

const obj = tson.parse(str);

// ✨ Retains type integrity
type Obj = typeof obj;
//   ^?
// -> type Obj = { foo: string; set: Set<number>; }

🤯 Streaming Promises and AsyncIterables

🧩 Extend with a custom serializer

!IMPORTANT When defining custom serializers, the array order matters! If a value passes the test for more than one custom serializer, the first wins.

⌚️ Temporal

See test reference in ./src/extend/temporal.test.ts

/* eslint-disable @typescript-eslint/no-unused-vars, n/no-missing-import, n/no-unpublished-import */
import { Temporal } from "@js-temporal/polyfill";
import { TsonType, createTson } from "tupleson";

const plainDate: TsonType<Temporal.PlainDate, string> = {
	deserialize: (v) => Temporal.PlainDate.from(v),
	key: "PlainDate",
	serialize: (v) => v.toJSON(),
	test: (v) => v instanceof Temporal.PlainDate,
};

const instant: TsonType<Temporal.Instant, string> = {
	deserialize: (v) => Temporal.Instant.from(v),
	key: "Instant",
	serialize: (v) => v.toJSON(),
	test: (v) => v instanceof Temporal.Instant,
};

const tson = createTson({
	types: [plainDate, instant],
});

🧮 Decimal.js

See test reference in ./src/extend/decimal.test.ts

/* eslint-disable @typescript-eslint/no-unused-vars, n/no-missing-import, n/no-unpublished-import */
import { Decimal } from "decimal.js";

const decimalJs: TsonType<Decimal, string> = {
	deserialize: (v) => new Decimal(v),
	key: "Decimal",
	serialize: (v) => v.toJSON(),
	test: (v) => v instanceof Decimal,
};

const tson = createTson({
	types: [decimalJs],
});

💙 This package is based on @JoshuaKGoldberg's create-typescript-app.

0.20.1

7 months ago

0.20.0

7 months ago

0.19.1

7 months ago

0.23.1

6 months ago

0.23.0

6 months ago

0.21.0

7 months ago

0.20.4

7 months ago

0.22.1

7 months ago

0.20.3

7 months ago

0.22.0

7 months ago

0.20.2

7 months ago

0.19.0

7 months ago

0.18.0

7 months ago

0.17.4

7 months ago

0.17.3

7 months ago

0.17.2

7 months ago

0.17.1

7 months ago

0.17.0

7 months ago

0.16.9

7 months ago

0.16.8

7 months ago

0.16.7

7 months ago

0.16.6

7 months ago

0.16.5

7 months ago

0.16.4

7 months ago

0.16.3

7 months ago

0.16.2

7 months ago

0.16.1

7 months ago

0.16.0

7 months ago

0.15.0

7 months ago

0.14.0

7 months ago

0.13.1

7 months ago

0.13.0

7 months ago

0.12.0

7 months ago

0.11.1

7 months ago

0.11.0

7 months ago

0.10.0

7 months ago

0.9.0

7 months ago

0.8.0

7 months ago

0.7.0

7 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.5.0

7 months ago

0.4.0

7 months ago

0.3.1

7 months ago

0.3.0

7 months ago

0.2.0

7 months ago

0.1.0

7 months ago