@awsless/json 0.0.2 → 0.0.4

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
@@ -6,13 +6,14 @@ The `@awsless/json` package adds support for more JavaScript native types to JSO
6
6
  ## The Problem
7
7
 
8
8
  JSON doesn't have support for types like:
9
- - Set
10
- - Map
11
- - Date
12
- - BigInt
13
- - BigFloat - npm @awsless/bit-float
9
+ - `undefined`
10
+ - `Set`
11
+ - `Map`
12
+ - `Date`
13
+ - `BigInt`
14
+ - `BigFloat` - npm package @awsless/bit-float
14
15
 
15
- Having to decode & encode these type of values can get quite annoying.
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.
16
17
 
17
18
  ## Basic Usage
18
19
 
@@ -20,13 +21,29 @@ Having to decode & encode these type of values can get quite annoying.
20
21
  import { parse, stringify } from '@awsless/json';
21
22
 
22
23
  // Stringify a bigint.
23
- // The output will be {"$bigint": "1"}
24
+ // The output will be {"$bigint":"1"}
24
25
  const json = stringify(1n)
25
26
 
26
27
  // Parse the json with a bigint inside.
27
28
  const value = parse(json)
28
29
  ```
29
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
+
30
47
  ## Extending Supported Types
31
48
 
32
49
  We let you extend JSON to support your own custom types.
@@ -54,3 +71,18 @@ const json = stringify(new Custom('example'), { $custom })
54
71
  // Parse the json with your custom type
55
72
  const value = parse(json, { $custom })
56
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,6 +23,7 @@ __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);
@@ -63,14 +64,24 @@ var $set = {
63
64
  stringify: (v) => Array.from(v)
64
65
  };
65
66
 
66
- // src/index.ts
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
67
75
  var baseTypes = {
76
+ $undefined,
68
77
  $bigfloat,
69
78
  $bigint,
70
79
  $date,
71
80
  $set,
72
81
  $map
73
82
  };
83
+
84
+ // src/parse.ts
74
85
  var parse = (json, types = {}) => {
75
86
  return JSON.parse(json, createReviver(types));
76
87
  };
@@ -94,6 +105,8 @@ var createReviver = (types = {}) => {
94
105
  return value;
95
106
  };
96
107
  };
108
+
109
+ // src/stringify.ts
97
110
  var stringify = (value, types = {}) => {
98
111
  return JSON.stringify(value, createReplacer(types));
99
112
  };
@@ -114,10 +127,16 @@ var createReplacer = (types = {}) => {
114
127
  return value;
115
128
  };
116
129
  };
130
+
131
+ // src/patch.ts
132
+ var patch = (value, types = {}) => {
133
+ return parse(stringify(value, types), types);
134
+ };
117
135
  // Annotate the CommonJS export names for ESM import in node:
118
136
  0 && (module.exports = {
119
137
  createReplacer,
120
138
  createReviver,
121
139
  parse,
140
+ patch,
122
141
  stringify
123
142
  });
package/dist/index.d.cts CHANGED
@@ -4,11 +4,15 @@ type Serializable<I, O> = {
4
4
  stringify: (value: I) => O;
5
5
  };
6
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
@@ -4,11 +4,15 @@ type Serializable<I, O> = {
4
4
  stringify: (value: I) => O;
5
5
  };
6
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
@@ -34,14 +34,24 @@ var $set = {
34
34
  stringify: (v) => Array.from(v)
35
35
  };
36
36
 
37
- // src/index.ts
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
38
45
  var baseTypes = {
46
+ $undefined,
39
47
  $bigfloat,
40
48
  $bigint,
41
49
  $date,
42
50
  $set,
43
51
  $map
44
52
  };
53
+
54
+ // src/parse.ts
45
55
  var parse = (json, types = {}) => {
46
56
  return JSON.parse(json, createReviver(types));
47
57
  };
@@ -65,6 +75,8 @@ var createReviver = (types = {}) => {
65
75
  return value;
66
76
  };
67
77
  };
78
+
79
+ // src/stringify.ts
68
80
  var stringify = (value, types = {}) => {
69
81
  return JSON.stringify(value, createReplacer(types));
70
82
  };
@@ -85,9 +97,15 @@ var createReplacer = (types = {}) => {
85
97
  return value;
86
98
  };
87
99
  };
100
+
101
+ // src/patch.ts
102
+ var patch = (value, types = {}) => {
103
+ return parse(stringify(value, types), types);
104
+ };
88
105
  export {
89
106
  createReplacer,
90
107
  createReviver,
91
108
  parse,
109
+ patch,
92
110
  stringify
93
111
  };
package/package.json CHANGED
@@ -1,8 +1,19 @@
1
1
  {
2
2
  "name": "@awsless/json",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
+ "keywords": [
7
+ "JSON",
8
+ "stringify",
9
+ "parse",
10
+ "BigInt",
11
+ "BigFloat",
12
+ "Map",
13
+ "Set",
14
+ "Date",
15
+ "undefined"
16
+ ],
6
17
  "repository": {
7
18
  "type": "git",
8
19
  "url": "https://github.com/awsless/awsless.git"