@awsless/json 0.0.1 → 0.0.3
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 +42 -4
- package/dist/index.cjs +44 -1
- package/dist/index.d.cts +11 -7
- package/dist/index.d.ts +11 -7
- package/dist/index.js +42 -2
- package/package.json +1 -1
package/README.MD
CHANGED
|
@@ -5,8 +5,15 @@ The `@awsless/json` package adds support for more JavaScript native types to JSO
|
|
|
5
5
|
|
|
6
6
|
## The Problem
|
|
7
7
|
|
|
8
|
-
JSON doesn't have support for types like
|
|
9
|
-
|
|
8
|
+
JSON doesn't have support for types like:
|
|
9
|
+
- `undefined`
|
|
10
|
+
- `Set`
|
|
11
|
+
- `Map`
|
|
12
|
+
- `Date`
|
|
13
|
+
- `BigInt`
|
|
14
|
+
- `BigFloat` - npm package @awsless/bit-float
|
|
15
|
+
|
|
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.
|
|
10
17
|
|
|
11
18
|
## Basic Usage
|
|
12
19
|
|
|
@@ -14,13 +21,29 @@ Having to decode & encode these type of values can get quite annoying.
|
|
|
14
21
|
import { parse, stringify } from '@awsless/json';
|
|
15
22
|
|
|
16
23
|
// Stringify a bigint.
|
|
17
|
-
// The output will be {"$bigint":
|
|
24
|
+
// The output will be {"$bigint":"1"}
|
|
18
25
|
const json = stringify(1n)
|
|
19
26
|
|
|
20
27
|
// Parse the json with a bigint inside.
|
|
21
28
|
const value = parse(json)
|
|
22
29
|
```
|
|
23
30
|
|
|
31
|
+
## Patching incorrectly parsed JSON that was parsed with a different JSON parser
|
|
32
|
+
|
|
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.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { stringify, patch } from '@awsless/json';
|
|
37
|
+
|
|
38
|
+
const json = stringify(1n)
|
|
39
|
+
|
|
40
|
+
// The native JSON.parse function will not parse our bigint correctly.
|
|
41
|
+
const broken = JSON.parse(json)
|
|
42
|
+
|
|
43
|
+
// Will fix the broken output.
|
|
44
|
+
const fixed = patch(broken)
|
|
45
|
+
```
|
|
46
|
+
|
|
24
47
|
## Extending Supported Types
|
|
25
48
|
|
|
26
49
|
We let you extend JSON to support your own custom types.
|
|
@@ -36,7 +59,7 @@ class Custom {
|
|
|
36
59
|
}
|
|
37
60
|
}
|
|
38
61
|
|
|
39
|
-
const $custom: Serializable<Custom> = {
|
|
62
|
+
const $custom: Serializable<Custom, string> = {
|
|
40
63
|
is: v => v instanceof Custom,
|
|
41
64
|
parse: v => new Custom(v),
|
|
42
65
|
stringify: v => v.value,
|
|
@@ -48,3 +71,18 @@ const json = stringify(new Custom('example'), { $custom })
|
|
|
48
71
|
// Parse the json with your custom type
|
|
49
72
|
const value = parse(json, { $custom })
|
|
50
73
|
```
|
|
74
|
+
|
|
75
|
+
## Known Issue's
|
|
76
|
+
|
|
77
|
+
Object properties with `undefined` as value type will be stripped away.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
// Will result in an empty object
|
|
81
|
+
const result = parse(stringify({ key: undefined }))
|
|
82
|
+
|
|
83
|
+
// Will log false
|
|
84
|
+
console.log('key' in result)
|
|
85
|
+
|
|
86
|
+
// Will log true
|
|
87
|
+
console.log(result.key === undefined)
|
|
88
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -23,30 +23,65 @@ __export(src_exports, {
|
|
|
23
23
|
createReplacer: () => createReplacer,
|
|
24
24
|
createReviver: () => createReviver,
|
|
25
25
|
parse: () => parse,
|
|
26
|
+
patch: () => patch,
|
|
26
27
|
stringify: () => stringify
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
|
|
31
|
+
// src/type/bigfloat.ts
|
|
29
32
|
var import_big_float = require("@awsless/big-float");
|
|
30
33
|
var $bigfloat = {
|
|
31
34
|
is: (v) => v instanceof import_big_float.BigFloat,
|
|
32
35
|
parse: (v) => new import_big_float.BigFloat(v),
|
|
33
36
|
stringify: (v) => v.toString()
|
|
34
37
|
};
|
|
38
|
+
|
|
39
|
+
// src/type/bigint.ts
|
|
35
40
|
var $bigint = {
|
|
36
41
|
is: (v) => typeof v === "bigint",
|
|
37
42
|
parse: (v) => BigInt(v),
|
|
38
43
|
stringify: (v) => v.toString()
|
|
39
44
|
};
|
|
45
|
+
|
|
46
|
+
// src/type/date.ts
|
|
40
47
|
var $date = {
|
|
41
48
|
is: (v) => v instanceof Date,
|
|
42
49
|
parse: (v) => new Date(v),
|
|
43
50
|
stringify: (v) => v.toISOString()
|
|
44
51
|
};
|
|
52
|
+
|
|
53
|
+
// src/type/map.ts
|
|
54
|
+
var $map = {
|
|
55
|
+
is: (v) => v instanceof Map,
|
|
56
|
+
parse: (v) => new Map(v),
|
|
57
|
+
stringify: (v) => Array.from(v)
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/type/set.ts
|
|
61
|
+
var $set = {
|
|
62
|
+
is: (v) => v instanceof Set,
|
|
63
|
+
parse: (v) => new Set(v),
|
|
64
|
+
stringify: (v) => Array.from(v)
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/type/undefined.ts
|
|
68
|
+
var $undefined = {
|
|
69
|
+
is: (v) => typeof v === "undefined",
|
|
70
|
+
parse: (_) => void 0,
|
|
71
|
+
stringify: (_) => 0
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/type/index.ts
|
|
45
75
|
var baseTypes = {
|
|
76
|
+
$undefined,
|
|
46
77
|
$bigfloat,
|
|
47
78
|
$bigint,
|
|
48
|
-
$date
|
|
79
|
+
$date,
|
|
80
|
+
$set,
|
|
81
|
+
$map
|
|
49
82
|
};
|
|
83
|
+
|
|
84
|
+
// src/parse.ts
|
|
50
85
|
var parse = (json, types = {}) => {
|
|
51
86
|
return JSON.parse(json, createReviver(types));
|
|
52
87
|
};
|
|
@@ -70,6 +105,8 @@ var createReviver = (types = {}) => {
|
|
|
70
105
|
return value;
|
|
71
106
|
};
|
|
72
107
|
};
|
|
108
|
+
|
|
109
|
+
// src/stringify.ts
|
|
73
110
|
var stringify = (value, types = {}) => {
|
|
74
111
|
return JSON.stringify(value, createReplacer(types));
|
|
75
112
|
};
|
|
@@ -90,10 +127,16 @@ var createReplacer = (types = {}) => {
|
|
|
90
127
|
return value;
|
|
91
128
|
};
|
|
92
129
|
};
|
|
130
|
+
|
|
131
|
+
// src/patch.ts
|
|
132
|
+
var patch = (value, types = {}) => {
|
|
133
|
+
return parse(stringify(value, types), types);
|
|
134
|
+
};
|
|
93
135
|
// Annotate the CommonJS export names for ESM import in node:
|
|
94
136
|
0 && (module.exports = {
|
|
95
137
|
createReplacer,
|
|
96
138
|
createReviver,
|
|
97
139
|
parse,
|
|
140
|
+
patch,
|
|
98
141
|
stringify
|
|
99
142
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
type Serializable<
|
|
2
|
-
is: (value: unknown) =>
|
|
3
|
-
parse: (value:
|
|
4
|
-
stringify: (value:
|
|
1
|
+
type Serializable<I, O> = {
|
|
2
|
+
is: (value: unknown) => value is I;
|
|
3
|
+
parse: (value: O) => I;
|
|
4
|
+
stringify: (value: I) => O;
|
|
5
5
|
};
|
|
6
|
-
type SerializableTypes = Record<string, Serializable<any>>;
|
|
6
|
+
type SerializableTypes = Record<string, Serializable<any, any>>;
|
|
7
|
+
|
|
8
|
+
declare const patch: <T>(value: T, types?: SerializableTypes) => T;
|
|
9
|
+
|
|
7
10
|
declare const parse: (json: string, types?: SerializableTypes) => any;
|
|
8
11
|
type Reviver = (this: any, key: string, value: any) => any;
|
|
9
|
-
type Replacer = (this: any, key: string, value: any) => any;
|
|
10
12
|
declare const createReviver: (types?: SerializableTypes) => Reviver;
|
|
13
|
+
|
|
11
14
|
declare const stringify: (value: unknown, types?: SerializableTypes) => string;
|
|
15
|
+
type Replacer = (this: any, key: string, value: any) => any;
|
|
12
16
|
declare const createReplacer: (types?: SerializableTypes) => Replacer;
|
|
13
17
|
|
|
14
|
-
export { type Serializable, createReplacer, createReviver, parse, stringify };
|
|
18
|
+
export { type Serializable, createReplacer, createReviver, parse, patch, stringify };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
type Serializable<
|
|
2
|
-
is: (value: unknown) =>
|
|
3
|
-
parse: (value:
|
|
4
|
-
stringify: (value:
|
|
1
|
+
type Serializable<I, O> = {
|
|
2
|
+
is: (value: unknown) => value is I;
|
|
3
|
+
parse: (value: O) => I;
|
|
4
|
+
stringify: (value: I) => O;
|
|
5
5
|
};
|
|
6
|
-
type SerializableTypes = Record<string, Serializable<any>>;
|
|
6
|
+
type SerializableTypes = Record<string, Serializable<any, any>>;
|
|
7
|
+
|
|
8
|
+
declare const patch: <T>(value: T, types?: SerializableTypes) => T;
|
|
9
|
+
|
|
7
10
|
declare const parse: (json: string, types?: SerializableTypes) => any;
|
|
8
11
|
type Reviver = (this: any, key: string, value: any) => any;
|
|
9
|
-
type Replacer = (this: any, key: string, value: any) => any;
|
|
10
12
|
declare const createReviver: (types?: SerializableTypes) => Reviver;
|
|
13
|
+
|
|
11
14
|
declare const stringify: (value: unknown, types?: SerializableTypes) => string;
|
|
15
|
+
type Replacer = (this: any, key: string, value: any) => any;
|
|
12
16
|
declare const createReplacer: (types?: SerializableTypes) => Replacer;
|
|
13
17
|
|
|
14
|
-
export { type Serializable, createReplacer, createReviver, parse, stringify };
|
|
18
|
+
export { type Serializable, createReplacer, createReviver, parse, patch, stringify };
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,57 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/type/bigfloat.ts
|
|
2
2
|
import { BigFloat } from "@awsless/big-float";
|
|
3
3
|
var $bigfloat = {
|
|
4
4
|
is: (v) => v instanceof BigFloat,
|
|
5
5
|
parse: (v) => new BigFloat(v),
|
|
6
6
|
stringify: (v) => v.toString()
|
|
7
7
|
};
|
|
8
|
+
|
|
9
|
+
// src/type/bigint.ts
|
|
8
10
|
var $bigint = {
|
|
9
11
|
is: (v) => typeof v === "bigint",
|
|
10
12
|
parse: (v) => BigInt(v),
|
|
11
13
|
stringify: (v) => v.toString()
|
|
12
14
|
};
|
|
15
|
+
|
|
16
|
+
// src/type/date.ts
|
|
13
17
|
var $date = {
|
|
14
18
|
is: (v) => v instanceof Date,
|
|
15
19
|
parse: (v) => new Date(v),
|
|
16
20
|
stringify: (v) => v.toISOString()
|
|
17
21
|
};
|
|
22
|
+
|
|
23
|
+
// src/type/map.ts
|
|
24
|
+
var $map = {
|
|
25
|
+
is: (v) => v instanceof Map,
|
|
26
|
+
parse: (v) => new Map(v),
|
|
27
|
+
stringify: (v) => Array.from(v)
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// src/type/set.ts
|
|
31
|
+
var $set = {
|
|
32
|
+
is: (v) => v instanceof Set,
|
|
33
|
+
parse: (v) => new Set(v),
|
|
34
|
+
stringify: (v) => Array.from(v)
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/type/undefined.ts
|
|
38
|
+
var $undefined = {
|
|
39
|
+
is: (v) => typeof v === "undefined",
|
|
40
|
+
parse: (_) => void 0,
|
|
41
|
+
stringify: (_) => 0
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/type/index.ts
|
|
18
45
|
var baseTypes = {
|
|
46
|
+
$undefined,
|
|
19
47
|
$bigfloat,
|
|
20
48
|
$bigint,
|
|
21
|
-
$date
|
|
49
|
+
$date,
|
|
50
|
+
$set,
|
|
51
|
+
$map
|
|
22
52
|
};
|
|
53
|
+
|
|
54
|
+
// src/parse.ts
|
|
23
55
|
var parse = (json, types = {}) => {
|
|
24
56
|
return JSON.parse(json, createReviver(types));
|
|
25
57
|
};
|
|
@@ -43,6 +75,8 @@ var createReviver = (types = {}) => {
|
|
|
43
75
|
return value;
|
|
44
76
|
};
|
|
45
77
|
};
|
|
78
|
+
|
|
79
|
+
// src/stringify.ts
|
|
46
80
|
var stringify = (value, types = {}) => {
|
|
47
81
|
return JSON.stringify(value, createReplacer(types));
|
|
48
82
|
};
|
|
@@ -63,9 +97,15 @@ var createReplacer = (types = {}) => {
|
|
|
63
97
|
return value;
|
|
64
98
|
};
|
|
65
99
|
};
|
|
100
|
+
|
|
101
|
+
// src/patch.ts
|
|
102
|
+
var patch = (value, types = {}) => {
|
|
103
|
+
return parse(stringify(value, types), types);
|
|
104
|
+
};
|
|
66
105
|
export {
|
|
67
106
|
createReplacer,
|
|
68
107
|
createReviver,
|
|
69
108
|
parse,
|
|
109
|
+
patch,
|
|
70
110
|
stringify
|
|
71
111
|
};
|