Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into chore-readm
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Sep 30, 2023
2 parents 95662d2 + 10ea044 commit e7fa8a5
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@


# [0.6.0](https://github.com/KATT/tupleson/compare/0.5.0...0.6.0) (2023-09-30)


### Features

* add custom serializer example ([#8](https://github.com/KATT/tupleson/issues/8)) ([58eab05](https://github.com/KATT/tupleson/commit/58eab05baff0dd07803b5aa01c402fcd02df5b09))

# [0.5.0](https://github.com/KATT/tupleson/compare/0.4.0...0.5.0) (2023-09-30)

### Features
Expand Down Expand Up @@ -33,4 +42,4 @@
### Features

- initial version ([#1](https://github.com/KATT/tupleson/issues/1)) ([ccce25b](https://github.com/KATT/tupleson/commit/ccce25b6a039cf2e5c1a774c1ab022f0946ca8d5))
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
- initialized repo ✨ ([c9e92a4](https://github.com/KATT/tupleson/commit/c9e92a42c97a8bc1ee3a9214f65626425c8598e3))
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,36 @@ type Obj = typeof obj;
// type Obj = { foo: string; set: Set<number>; }
```

### Extend with a custom serializer

#### `Temporal`

> See test reference in [`./src/extend/temporal.test.ts`](./src/extend/temporal.test.ts)
```ts
/* eslint-disable eslint-comments/disable-enable-pair, @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.toString(),
test: (v) => v instanceof Temporal.PlainDate,
};

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

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

**Footnotes**:

[^1]: We don't support circular references as we don't think it's very desireable, but if you wanna contribute with adding opt-in support for that, you are very welcome!
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tupleson",
"version": "0.5.0",
"version": "0.6.0",
"description": "A hackable JSON serializer/deserializer",
"repository": {
"type": "git",
Expand Down Expand Up @@ -32,6 +32,7 @@
"tsc": "tsc"
},
"devDependencies": {
"@js-temporal/polyfill": "^0.4.4",
"@release-it/conventional-changelog": "^7.0.2",
"@tsconfig/strictest": "^2.0.2",
"@types/eslint": "^8.44.3",
Expand Down
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions src/extend/temporal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Temporal } from "@js-temporal/polyfill";
import { expect, test } from "vitest";

import { TsonType, createTson } from "../index.js";

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

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

const tson = createTson({
types: [plainDate, instant],
});
test("PlainDate", () => {
const expected = Temporal.PlainDate.from("2021-01-01");

const serialized = tson.serialize(expected);

expect(serialized).toMatchInlineSnapshot(`
{
"json": [
"PlainDate",
"2021-01-01",
"__tson",
],
"nonce": "__tson",
}
`);
const deserialized = tson.deserialize(serialized);

expect(deserialized).toEqual(expected);
});

test("Instant", () => {
const expected = Temporal.Instant.from("2021-01-01T00:00:00Z");

const serialized = tson.serialize(expected);

expect(serialized).toMatchInlineSnapshot(`
{
"json": [
"Instant",
"2021-01-01T00:00:00Z",
"__tson",
],
"nonce": "__tson",
}
`);
const deserialized = tson.deserialize(serialized);

expect(deserialized).toEqual(expected);
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export {
} from "./tson.js";

export * from "./handlers/index.js";

export type { TsonType } from "./types.js";

0 comments on commit e7fa8a5

Please sign in to comment.