@lindorm/json-kit 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +126 -0
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [0.5.2](https://github.com/lindorm-io/monorepo/compare/@lindorm/json-kit@0.5.1...@lindorm/json-kit@0.5.2) (2025-07-19)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @lindorm/json-kit
|
|
9
|
+
|
|
10
|
+
## [0.5.1](https://github.com/lindorm-io/monorepo/compare/@lindorm/json-kit@0.5.0...@lindorm/json-kit@0.5.1) (2025-07-10)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @lindorm/json-kit
|
|
13
|
+
|
|
6
14
|
# [0.5.0](https://github.com/lindorm-io/monorepo/compare/@lindorm/json-kit@0.4.2...@lindorm/json-kit@0.5.0) (2025-06-17)
|
|
7
15
|
|
|
8
16
|
### Features
|
package/README.md
CHANGED
|
@@ -1 +1,127 @@
|
|
|
1
1
|
# @lindorm/json-kit
|
|
2
|
+
|
|
3
|
+
Loss-less serialisation helpers for JavaScript values that don’t normally
|
|
4
|
+
survive `JSON.stringify` – such as **Date**, **Buffer**, `undefined`, etc.
|
|
5
|
+
`@lindorm/json-kit` wraps your data in a tiny metadata envelope so it can be
|
|
6
|
+
round-tripped _back to its original types_ after transport or storage.
|
|
7
|
+
|
|
8
|
+
The library is framework-agnostic, depends only on `@lindorm/is` and embraces
|
|
9
|
+
plain objects. It is a perfect companion when you need to squeeze complex
|
|
10
|
+
state into a database column, message bus or cache.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @lindorm/json-kit
|
|
18
|
+
# or
|
|
19
|
+
yarn add @lindorm/json-kit
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Quick glance
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { JsonKit } from '@lindorm/json-kit';
|
|
28
|
+
|
|
29
|
+
const original = {
|
|
30
|
+
now: new Date(),
|
|
31
|
+
payload: Buffer.from('secret'),
|
|
32
|
+
counter: 123,
|
|
33
|
+
maybe: undefined,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// 1. Stringify (meta is embedded automatically)
|
|
37
|
+
const str = JsonKit.stringify(original);
|
|
38
|
+
|
|
39
|
+
// 2. Persist / send over the wire …
|
|
40
|
+
|
|
41
|
+
// 3. Parse – voilà, the Date and Buffer are restored
|
|
42
|
+
const clone = JsonKit.parse<typeof original>(str);
|
|
43
|
+
|
|
44
|
+
clone.now instanceof Date; // true
|
|
45
|
+
Buffer.isBuffer(clone.payload); // true
|
|
46
|
+
clone.maybe === undefined; // true
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The generated JSON looks like this (shortened for readability):
|
|
50
|
+
|
|
51
|
+
```jsonc
|
|
52
|
+
{
|
|
53
|
+
"__meta__": { // type map
|
|
54
|
+
"now": "D", // D = Date
|
|
55
|
+
"payload": "F", // F = Buffer (File)
|
|
56
|
+
"maybe": "U" // U = Undefined
|
|
57
|
+
},
|
|
58
|
+
"__record__": {
|
|
59
|
+
"now": "2024-06-25T12:00:00.000Z",
|
|
60
|
+
"payload": "c2VjcmV0", // base64-encoded Buffer
|
|
61
|
+
"counter": 123
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## API
|
|
69
|
+
|
|
70
|
+
### `JsonKit.stringify(data)` → `string`
|
|
71
|
+
Turns an object or array into a JSON‐compatible string while preserving
|
|
72
|
+
non-standard primitives.
|
|
73
|
+
|
|
74
|
+
### `JsonKit.parse(str | Buffer)` → `T`
|
|
75
|
+
Reverses `stringify`. Accepts either string or `Buffer`.
|
|
76
|
+
|
|
77
|
+
### `JsonKit.buffer(data)` → `Buffer`
|
|
78
|
+
Same as `stringify` but returns a Node `Buffer` instead of string.
|
|
79
|
+
|
|
80
|
+
### `JsonKit.primitive(data)` → `Primitive<T>`
|
|
81
|
+
Returns the low-level `Primitive` wrapper that exposes the raw metadata and
|
|
82
|
+
helper methods (`toJSON()`, `toString()`, `toBuffer()`). Useful if you want to
|
|
83
|
+
manipulate the envelope yourself.
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## What is stored in `__meta__`?
|
|
88
|
+
|
|
89
|
+
Each value is tagged with a single-character **MetaType** code:
|
|
90
|
+
|
|
91
|
+
| Code | Type |
|
|
92
|
+
|------|------------|
|
|
93
|
+
| `A` | Array |
|
|
94
|
+
| `B` | Boolean |
|
|
95
|
+
| `D` | Date |
|
|
96
|
+
| `F` | Buffer |
|
|
97
|
+
| `L` | Null |
|
|
98
|
+
| `N` | Number |
|
|
99
|
+
| `S` | String |
|
|
100
|
+
| `U` | Undefined |
|
|
101
|
+
| `X` | Unknown |
|
|
102
|
+
|
|
103
|
+
During `parse` Json-Kit walks the structure and casts every value back to its
|
|
104
|
+
original representation.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Caveats
|
|
109
|
+
|
|
110
|
+
• Functions and class instances are **not** serialised – they fall back to
|
|
111
|
+
`MetaType.Unknown` and are returned untouched.
|
|
112
|
+
• Circular references are **not** supported.
|
|
113
|
+
• For security reasons the `Buffer` content is base64-encoded, not hex.
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
## Contributing
|
|
118
|
+
|
|
119
|
+
1. `cd packages/json-kit && npm ci`
|
|
120
|
+
2. Make your changes & add tests (`npm test`)
|
|
121
|
+
3. Send a pull request – maintain 100 % test coverage ☂️
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
AGPL-3.0-or-later – © Lindorm, 2024
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lindorm/json-kit",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"license": "AGPL-3.0-or-later",
|
|
5
5
|
"author": "Jonn Nilsson",
|
|
6
6
|
"repository": {
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"update:auto": "ncu -u"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@lindorm/is": "^0.1.
|
|
32
|
+
"@lindorm/is": "^0.1.10"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@lindorm/types": "^0.3.
|
|
35
|
+
"@lindorm/types": "^0.3.2"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "b4d4cb9d15779ec6703c758e90ceba48f74d64d9"
|
|
38
38
|
}
|