@awsless/json 0.0.5 → 0.0.7

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 CHANGED
@@ -3,24 +3,37 @@
3
3
 
4
4
  The `@awsless/json` package adds support for more JavaScript native types to JSON.
5
5
 
6
+ Features:
7
+ - Lightweight / Using the JS native JSON parser.
8
+ - JSON backwards compatible.
9
+ - No precision loss.
10
+ - Includes support for basic JS types.
11
+ - Extendable.
12
+
6
13
  ## The Problem
7
14
 
8
15
  JSON doesn't have support for types like:
9
16
  - `undefined`
17
+ - `NaN`
18
+ - `Infinity`
19
+ - `Uint8Array`
20
+ - `RegExp`
10
21
  - `Set`
11
22
  - `Map`
23
+ - `URL`
12
24
  - `Date`
13
25
  - `BigInt`
14
26
  - `BigFloat` - npm package @awsless/bit-float
15
27
 
16
- Having to decode & encode these type of values can get quite annoying. We try to solve this problem by encoding these types using valid JSON syntax.
28
+ Having to encode & decode these type of values can get quite annoying. We try to solve this problem by encoding these types using valid JSON syntax.
29
+
30
+ Additionally, the native `JSON.parse/stringify` functions do not address the potential loss of precision problem.
17
31
 
18
32
  ## Basic Usage
19
33
 
20
34
  ```ts
21
35
  import { parse, stringify } from '@awsless/json';
22
36
 
23
- // Stringify a bigint.
24
37
  // The output will be {"$bigint":"1"}
25
38
  const json = stringify(1n)
26
39
 
@@ -30,7 +43,7 @@ const value = parse(json)
30
43
 
31
44
  ## Patching incorrectly parsed JSON that was parsed with a different JSON parser
32
45
 
33
- In some cases you might not have control over the JSON parser that is being used. In these cases your JSON will still be able to parse, but the output will be incorrect. We can patch the incorrect output by using the `patch` function.
46
+ In some situations, you may not have control over the JSON parser being used. In these instances, your JSON can still be parsed, but the output may be incorrect. We can correct the inaccurate output by using the `patch` function.
34
47
 
35
48
  ```ts
36
49
  import { stringify, patch } from '@awsless/json';
@@ -72,9 +85,36 @@ const json = stringify(new Custom('example'), { $custom })
72
85
  const value = parse(json, { $custom })
73
86
  ```
74
87
 
88
+ ## Precision Loss
89
+
90
+ When using the native `JSON.parse/stringify` functions you could lose precision when parsing native numbers. And you don't always have the ability to extend JSON with your own custom types. For example when you’re communicating with a third-party API. For this reason, we have 2 utility functions that will parse the native JSON number type to your own precision-safe alternative.
91
+
92
+ > [!NOTE]
93
+ > These utility functions will only work in environments that support `JSON.rawJSON`
94
+ > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/rawJSON#browser_compatibility
95
+
96
+ ```ts
97
+ import { safeNumberParse, safeNumberStringify } from '@awsless/json';
98
+ import { BigFloat, eq } from '@awsless/big-float';
99
+
100
+ const value = new BigFloat(1)
101
+ const json = safeNumberStringify(ONE, {
102
+ is: v => v instanceof BigFloat,
103
+ stringify: v => v.toString(),
104
+ })
105
+
106
+ console.log(json) // '1'
107
+
108
+ const result = safeNumberParse('1', {
109
+ parse: v => new BigFloat(v),
110
+ })
111
+
112
+ console.log(eq(value, result)) // true
113
+ ```
114
+
75
115
  ## Known Issue's
76
116
 
77
- ### Don't use the $ character inside your JSON.
117
+ ### Don't use the `$` character inside your JSON.
78
118
 
79
119
  We use the `$` character to encode our special types inside JSON. In order to prevent parsing errors we recommend to avoid using the `$` character inside your object property names.
80
120
 
package/dist/index.cjs CHANGED
@@ -22,8 +22,12 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  createReplacer: () => createReplacer,
24
24
  createReviver: () => createReviver,
25
+ createSafeNumberReplacer: () => createSafeNumberReplacer,
26
+ createSafeNumberReviver: () => createSafeNumberReviver,
25
27
  parse: () => parse,
26
28
  patch: () => patch,
29
+ safeNumberParse: () => safeNumberParse,
30
+ safeNumberStringify: () => safeNumberStringify,
27
31
  stringify: () => stringify,
28
32
  unpatch: () => unpatch
29
33
  });
@@ -51,6 +55,15 @@ var $date = {
51
55
  stringify: (v) => v.toISOString()
52
56
  };
53
57
 
58
+ // src/type/infinity.ts
59
+ var P = Infinity;
60
+ var N = -Infinity;
61
+ var $infinity = {
62
+ is: (v) => v === P || v === N,
63
+ parse: (v) => v === 1 ? P : N,
64
+ stringify: (v) => v > 0 ? 1 : 0
65
+ };
66
+
54
67
  // src/type/map.ts
55
68
  var $map = {
56
69
  is: (v) => v instanceof Map,
@@ -58,6 +71,20 @@ var $map = {
58
71
  stringify: (v) => Array.from(v)
59
72
  };
60
73
 
74
+ // src/type/nan.ts
75
+ var $nan = {
76
+ is: (v) => typeof v === "number" && isNaN(v),
77
+ parse: (_) => NaN,
78
+ stringify: (_) => 0
79
+ };
80
+
81
+ // src/type/regexp.ts
82
+ var $regexp = {
83
+ is: (v) => v instanceof RegExp,
84
+ parse: (v) => new RegExp(v[0], v[1]),
85
+ stringify: (v) => [v.source, v.flags]
86
+ };
87
+
61
88
  // src/type/set.ts
62
89
  var $set = {
63
90
  is: (v) => v instanceof Set,
@@ -65,6 +92,13 @@ var $set = {
65
92
  stringify: (v) => Array.from(v)
66
93
  };
67
94
 
95
+ // src/type/binary.ts
96
+ var $binary = {
97
+ is: (v) => v instanceof Uint8Array,
98
+ parse: (v) => Uint8Array.from(atob(v), (c) => c.charCodeAt(0)),
99
+ stringify: (v) => btoa(String.fromCharCode(...v))
100
+ };
101
+
68
102
  // src/type/undefined.ts
69
103
  var $undefined = {
70
104
  is: (v) => typeof v === "undefined",
@@ -72,14 +106,26 @@ var $undefined = {
72
106
  stringify: (_) => 0
73
107
  };
74
108
 
109
+ // src/type/url.ts
110
+ var $url = {
111
+ is: (v) => v instanceof URL,
112
+ parse: (v) => new URL(v),
113
+ stringify: (v) => v.toString()
114
+ };
115
+
75
116
  // src/type/index.ts
76
117
  var baseTypes = {
77
118
  $undefined,
119
+ $infinity,
78
120
  $bigfloat,
79
121
  $bigint,
122
+ $regexp,
123
+ $binary,
80
124
  $date,
81
125
  $set,
82
- $map
126
+ $map,
127
+ $nan,
128
+ $url
83
129
  };
84
130
 
85
131
  // src/parse.ts
@@ -136,12 +182,47 @@ var patch = (value, types = {}) => {
136
182
  var unpatch = (value, types = {}) => {
137
183
  return JSON.parse(stringify(value, types));
138
184
  };
185
+
186
+ // src/safe-number/parse.ts
187
+ var safeNumberParse = (json, props) => {
188
+ return JSON.parse(
189
+ json,
190
+ // @ts-ignore
191
+ createSafeNumberReviver(props)
192
+ );
193
+ };
194
+ var createSafeNumberReviver = (props) => {
195
+ return (_, value, context) => {
196
+ if (typeof value === "number") {
197
+ return props.parse(context.source);
198
+ }
199
+ return value;
200
+ };
201
+ };
202
+
203
+ // src/safe-number/stringify.ts
204
+ var safeNumberStringify = (value, props) => {
205
+ return JSON.stringify(value, createSafeNumberReplacer(props));
206
+ };
207
+ var createSafeNumberReplacer = (props) => {
208
+ return function(key, value) {
209
+ const original = this[key];
210
+ if (props.is(original)) {
211
+ return JSON.rawJSON(props.stringify(original));
212
+ }
213
+ return value;
214
+ };
215
+ };
139
216
  // Annotate the CommonJS export names for ESM import in node:
140
217
  0 && (module.exports = {
141
218
  createReplacer,
142
219
  createReviver,
220
+ createSafeNumberReplacer,
221
+ createSafeNumberReviver,
143
222
  parse,
144
223
  patch,
224
+ safeNumberParse,
225
+ safeNumberStringify,
145
226
  stringify,
146
227
  unpatch
147
228
  });
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  type Serializable<I, O> = {
2
- is: (value: unknown) => value is I;
2
+ is: (value: unknown) => boolean;
3
3
  parse: (value: O) => I;
4
4
  stringify: (value: I) => O;
5
5
  };
@@ -9,11 +9,28 @@ declare const patch: (value: unknown, types?: SerializableTypes) => any;
9
9
  declare const unpatch: (value: unknown, types?: SerializableTypes) => any;
10
10
 
11
11
  declare const parse: (json: string, types?: SerializableTypes) => any;
12
- type Reviver = (this: any, key: string, value: any) => any;
13
- declare const createReviver: (types?: SerializableTypes) => Reviver;
12
+ type Reviver$1 = (this: any, key: string, value: any) => any;
13
+ declare const createReviver: (types?: SerializableTypes) => Reviver$1;
14
14
 
15
15
  declare const stringify: (value: unknown, types?: SerializableTypes) => string;
16
+ type Replacer$1 = (this: any, key: string, value: any) => any;
17
+ declare const createReplacer: (types?: SerializableTypes) => Replacer$1;
18
+
19
+ type Props$1 = {
20
+ parse: (value: string) => unknown;
21
+ };
22
+ declare const safeNumberParse: (json: string, props: Props$1) => any;
23
+ type Reviver = (this: any, key: string, value: any, context: {
24
+ source: string;
25
+ }) => any;
26
+ declare const createSafeNumberReviver: (props: Props$1) => Reviver;
27
+
28
+ type Props<T> = {
29
+ is: (value: unknown) => value is T;
30
+ stringify: (value: T) => string;
31
+ };
32
+ declare const safeNumberStringify: <T>(value: unknown, props: Props<T>) => string;
16
33
  type Replacer = (this: any, key: string, value: any) => any;
17
- declare const createReplacer: (types?: SerializableTypes) => Replacer;
34
+ declare const createSafeNumberReplacer: <T>(props: Props<T>) => Replacer;
18
35
 
19
- export { type Serializable, createReplacer, createReviver, parse, patch, stringify, unpatch };
36
+ export { type Serializable, createReplacer, createReviver, createSafeNumberReplacer, createSafeNumberReviver, parse, patch, safeNumberParse, safeNumberStringify, stringify, unpatch };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  type Serializable<I, O> = {
2
- is: (value: unknown) => value is I;
2
+ is: (value: unknown) => boolean;
3
3
  parse: (value: O) => I;
4
4
  stringify: (value: I) => O;
5
5
  };
@@ -9,11 +9,28 @@ declare const patch: (value: unknown, types?: SerializableTypes) => any;
9
9
  declare const unpatch: (value: unknown, types?: SerializableTypes) => any;
10
10
 
11
11
  declare const parse: (json: string, types?: SerializableTypes) => any;
12
- type Reviver = (this: any, key: string, value: any) => any;
13
- declare const createReviver: (types?: SerializableTypes) => Reviver;
12
+ type Reviver$1 = (this: any, key: string, value: any) => any;
13
+ declare const createReviver: (types?: SerializableTypes) => Reviver$1;
14
14
 
15
15
  declare const stringify: (value: unknown, types?: SerializableTypes) => string;
16
+ type Replacer$1 = (this: any, key: string, value: any) => any;
17
+ declare const createReplacer: (types?: SerializableTypes) => Replacer$1;
18
+
19
+ type Props$1 = {
20
+ parse: (value: string) => unknown;
21
+ };
22
+ declare const safeNumberParse: (json: string, props: Props$1) => any;
23
+ type Reviver = (this: any, key: string, value: any, context: {
24
+ source: string;
25
+ }) => any;
26
+ declare const createSafeNumberReviver: (props: Props$1) => Reviver;
27
+
28
+ type Props<T> = {
29
+ is: (value: unknown) => value is T;
30
+ stringify: (value: T) => string;
31
+ };
32
+ declare const safeNumberStringify: <T>(value: unknown, props: Props<T>) => string;
16
33
  type Replacer = (this: any, key: string, value: any) => any;
17
- declare const createReplacer: (types?: SerializableTypes) => Replacer;
34
+ declare const createSafeNumberReplacer: <T>(props: Props<T>) => Replacer;
18
35
 
19
- export { type Serializable, createReplacer, createReviver, parse, patch, stringify, unpatch };
36
+ export { type Serializable, createReplacer, createReviver, createSafeNumberReplacer, createSafeNumberReviver, parse, patch, safeNumberParse, safeNumberStringify, stringify, unpatch };
package/dist/index.js CHANGED
@@ -20,6 +20,15 @@ var $date = {
20
20
  stringify: (v) => v.toISOString()
21
21
  };
22
22
 
23
+ // src/type/infinity.ts
24
+ var P = Infinity;
25
+ var N = -Infinity;
26
+ var $infinity = {
27
+ is: (v) => v === P || v === N,
28
+ parse: (v) => v === 1 ? P : N,
29
+ stringify: (v) => v > 0 ? 1 : 0
30
+ };
31
+
23
32
  // src/type/map.ts
24
33
  var $map = {
25
34
  is: (v) => v instanceof Map,
@@ -27,6 +36,20 @@ var $map = {
27
36
  stringify: (v) => Array.from(v)
28
37
  };
29
38
 
39
+ // src/type/nan.ts
40
+ var $nan = {
41
+ is: (v) => typeof v === "number" && isNaN(v),
42
+ parse: (_) => NaN,
43
+ stringify: (_) => 0
44
+ };
45
+
46
+ // src/type/regexp.ts
47
+ var $regexp = {
48
+ is: (v) => v instanceof RegExp,
49
+ parse: (v) => new RegExp(v[0], v[1]),
50
+ stringify: (v) => [v.source, v.flags]
51
+ };
52
+
30
53
  // src/type/set.ts
31
54
  var $set = {
32
55
  is: (v) => v instanceof Set,
@@ -34,6 +57,13 @@ var $set = {
34
57
  stringify: (v) => Array.from(v)
35
58
  };
36
59
 
60
+ // src/type/binary.ts
61
+ var $binary = {
62
+ is: (v) => v instanceof Uint8Array,
63
+ parse: (v) => Uint8Array.from(atob(v), (c) => c.charCodeAt(0)),
64
+ stringify: (v) => btoa(String.fromCharCode(...v))
65
+ };
66
+
37
67
  // src/type/undefined.ts
38
68
  var $undefined = {
39
69
  is: (v) => typeof v === "undefined",
@@ -41,14 +71,26 @@ var $undefined = {
41
71
  stringify: (_) => 0
42
72
  };
43
73
 
74
+ // src/type/url.ts
75
+ var $url = {
76
+ is: (v) => v instanceof URL,
77
+ parse: (v) => new URL(v),
78
+ stringify: (v) => v.toString()
79
+ };
80
+
44
81
  // src/type/index.ts
45
82
  var baseTypes = {
46
83
  $undefined,
84
+ $infinity,
47
85
  $bigfloat,
48
86
  $bigint,
87
+ $regexp,
88
+ $binary,
49
89
  $date,
50
90
  $set,
51
- $map
91
+ $map,
92
+ $nan,
93
+ $url
52
94
  };
53
95
 
54
96
  // src/parse.ts
@@ -105,11 +147,46 @@ var patch = (value, types = {}) => {
105
147
  var unpatch = (value, types = {}) => {
106
148
  return JSON.parse(stringify(value, types));
107
149
  };
150
+
151
+ // src/safe-number/parse.ts
152
+ var safeNumberParse = (json, props) => {
153
+ return JSON.parse(
154
+ json,
155
+ // @ts-ignore
156
+ createSafeNumberReviver(props)
157
+ );
158
+ };
159
+ var createSafeNumberReviver = (props) => {
160
+ return (_, value, context) => {
161
+ if (typeof value === "number") {
162
+ return props.parse(context.source);
163
+ }
164
+ return value;
165
+ };
166
+ };
167
+
168
+ // src/safe-number/stringify.ts
169
+ var safeNumberStringify = (value, props) => {
170
+ return JSON.stringify(value, createSafeNumberReplacer(props));
171
+ };
172
+ var createSafeNumberReplacer = (props) => {
173
+ return function(key, value) {
174
+ const original = this[key];
175
+ if (props.is(original)) {
176
+ return JSON.rawJSON(props.stringify(original));
177
+ }
178
+ return value;
179
+ };
180
+ };
108
181
  export {
109
182
  createReplacer,
110
183
  createReviver,
184
+ createSafeNumberReplacer,
185
+ createSafeNumberReviver,
111
186
  parse,
112
187
  patch,
188
+ safeNumberParse,
189
+ safeNumberStringify,
113
190
  stringify,
114
191
  unpatch
115
192
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsless/json",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -12,6 +12,11 @@
12
12
  "Map",
13
13
  "Set",
14
14
  "Date",
15
+ "NaN",
16
+ "Infinity",
17
+ "RegExp",
18
+ "Uint8Array",
19
+ "Binary",
15
20
  "undefined"
16
21
  ],
17
22
  "repository": {