jsonsuperset 1.0.0

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/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # JsonSuperSet
2
+
3
+ Extended JSON serialization supporting Date, RegExp, Error, undefined, Map, Set, and circular references.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install jsonsuperset
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```javascript
14
+ const jss = require('jsonsuperset')
15
+
16
+ const data = {
17
+ created: new Date(),
18
+ pattern: /hello/gi,
19
+ items: new Set([1, 2, 3]),
20
+ config: new Map([['key', 'value']])
21
+ }
22
+
23
+ const json = jss.stringify(data)
24
+ const restored = jss.parse(json)
25
+
26
+ restored.created // Date object
27
+ restored.pattern // RegExp /hello/gi
28
+ restored.items // Set {1, 2, 3}
29
+ restored.config // Map {'key' => 'value'}
30
+ ```
31
+
32
+ ## Supported Types
33
+
34
+ | Type | Description |
35
+ |------|-------------|
36
+ | `Date` | Preserved as Date objects |
37
+ | `RegExp` | Pattern and flags preserved |
38
+ | `Error` | Type, message, and stack preserved |
39
+ | `undefined` | Preserved (normally lost in JSON) |
40
+ | `Map` | Key-value pairs preserved |
41
+ | `Set` | Unique values preserved |
42
+ | `Circular refs` | Self-references and shared objects maintained |
43
+
44
+ ## API
45
+
46
+ ### stringify(obj)
47
+
48
+ Serializes an object to a JSON string with type information.
49
+
50
+ ```javascript
51
+ jss.stringify({ date: new Date('2025-01-01') })
52
+ // '{"date<!D>":1735689600000}'
53
+ ```
54
+
55
+ ### parse(str)
56
+
57
+ Deserializes a JSON string back to an object with types restored.
58
+
59
+ ```javascript
60
+ jss.parse('{"date<!D>":1735689600000}')
61
+ // { date: Date('2025-01-01') }
62
+ ```
63
+
64
+ ### encode(obj) / decode(obj)
65
+
66
+ Low-level functions for inspecting the tagged format without JSON stringification.
67
+
68
+ ```javascript
69
+ const encoded = jss.encode({ d: new Date(), s: new Set([1, 2]) })
70
+ // { "d<!D>": 1234567890, "s<!S>": [1, 2] }
71
+
72
+ const decoded = jss.decode(encoded)
73
+ // { d: Date, s: Set }
74
+ ```
75
+
76
+ ### custom(tag, config)
77
+
78
+ Register a custom type handler.
79
+
80
+ ```javascript
81
+ jss.custom('B', {
82
+ check: (key, value) => typeof value === 'bigint',
83
+ encode: (path, key, value, context) => value.toString(),
84
+ decode: (value, path, context) => BigInt(value)
85
+ })
86
+
87
+ jss.stringify({ big: 9007199254740993n })
88
+ // '{"big<!B>":"9007199254740993"}'
89
+ ```
90
+
91
+ ## Examples
92
+
93
+ ### Error Preservation
94
+
95
+ ```javascript
96
+ const error = new TypeError('Invalid input')
97
+ error.code = 'ERR_INVALID'
98
+
99
+ const result = jss.parse(jss.stringify({ err: error }))
100
+ result.err instanceof TypeError // true
101
+ result.err.message // 'Invalid input'
102
+ result.err.stack // original stack trace
103
+ ```
104
+
105
+ ### Circular References
106
+
107
+ ```javascript
108
+ const obj = { name: 'root' }
109
+ obj.self = obj
110
+
111
+ const result = jss.parse(jss.stringify(obj))
112
+ result.self === result // true
113
+ ```
114
+
115
+ ### Shared References
116
+
117
+ ```javascript
118
+ const shared = { value: 42 }
119
+ const data = { a: shared, b: shared }
120
+
121
+ const result = jss.parse(jss.stringify(data))
122
+ result.a === result.b // true (same object reference)
123
+ ```
124
+
125
+ ## Wire Format
126
+
127
+ Properties with special types are tagged using `<!TAG>` suffix:
128
+
129
+ ```
130
+ key<!D> → Date (stored as timestamp)
131
+ key<!R> → RegExp (stored as "/pattern/flags")
132
+ key<!E> → Error (stored as [name, message, stack])
133
+ key<!U> → undefined (stored as null)
134
+ key<!M> → Map (stored as object)
135
+ key<!S> → Set (stored as array)
136
+ key<!P> → Pointer (circular reference path)
137
+ ```
138
+
139
+ Arrays with typed elements use compound tags: `arr<![D,D,D]>` or shorthand `arr<![*D]>` for homogeneous arrays.