@@ -35,20 +35,64 @@ console.log(typeof value.date, value.date.constructor.name); // => object Date
35
35
console .log (typeof value .amount ); // => bigint
36
36
console .log (typeof value .debt , isNaN (value .debt )); // => number true
37
37
```
38
- ## Motivation
39
- JSON format is good enough for everyday usage. There are some libraries trying to introduce syntax to make JSON closer
40
- to modern JavaScript, some libraries trying to introduce functions serialization. All that is not important and is not
41
- required for everyday usage. However, there is one thing annoying me always - date values.
42
38
43
- We are serializing dates a lot and each time we parse it back we are getting a string. As a result we have to deal with
44
- the Date constructor manually each time. Even if we are no need date as an object, date formatter will have to make date
45
- object in order to make user-friendly text representation. Otherwords we are forced to care about dates additionally.
46
- It produces bulky solutions or tons of inline type conversions.
39
+ ## Features
40
+ * Can parse standard ` JSON ` format
41
+ * Support for ` BigInt ` values
42
+ * Support for ` NaN ` values
43
+ * Support for ` Infinity ` /` -Infinity ` values
44
+ * Support for typed serialization/deserialization, work with ` Date ` class out of the box
45
+ * Allow using trailing commas
46
+ * Zero-dependency npm-package
47
+ * Both CJS/ESM modules support
48
+
49
+ ## Installation
50
+ ``` shell
51
+ npm install json22
52
+ ```
53
+ In your code
54
+ ``` javascript
55
+ import { JSON22 } from ' json22'
47
56
48
- But I'm lazy developer, I'll do everything to get rid of any additional careness.
57
+ const data = { date: new Date () };
58
+ const s = JSON22 .stringify (data);
59
+ ```
60
+
61
+ For old-fashioned applications
62
+ ``` javascript
63
+ const { JSON22 } = require (' json22' );
64
+
65
+ const data = { date: new Date () };
66
+ const s = JSON22 .stringify (data);
67
+ ```
68
+
69
+ ## Integration
70
+ ### Using with Express
71
+ There is library [ json22-express] ( https://github.com/dancecoder/json22-express ) providing JSON22 support for expressjs applications
72
+ ``` javascript
73
+ import express from ' express' ;
74
+ import { json22express } from ' json22-express'
75
+
76
+ const app = express ();
77
+ app .use (json22express ());
78
+
79
+ app .get (' /date' , (req , res , next ) => {
80
+ res .status (200 ).json22 ({ date: new Date () });
81
+ next ();
82
+ });
83
+ ```
84
+ ### Using with Axios
85
+ There is library [ json22-axios] ( https://github.com/dancecoder/json22-axios ) providing JSON22 support for client applications
86
+ ``` javascript
87
+ import axios from ' axios' ;
88
+ import { Json22RequestInterceptor } from ' json22-axios' ;
89
+
90
+ axios .interceptors .request .use (Json22RequestInterceptor ());
91
+ // interceptor allow you to send and receive JSON22
92
+ ```
49
93
50
94
## API
51
- Note: JSON22 cannot be used as drop in JSON object replacement due to ` parse ` and ` stringify ` methods
95
+ Note: JSON22 cannot be used as drop in JSON object replacement due to ` parse ` and ` stringify ` methods
52
96
arguments incompatibility. But you may not be worried in case you are using first arguments only.
53
97
``` typescript
54
98
class JSON22 {
@@ -165,3 +209,15 @@ The JSON22 support for `toJSON` method of an object as well as JSON. In some cas
165
209
and ` toJSON` methods. Typical example is the Date class. The JSON22 at first is a solution to serialize/deserialize
166
210
date values, so __` valueOf` have higher priority over ` toJSON` __. This is also true for any object implementing ` valueOf`
167
211
and ` toJSON` both.
212
+
213
+ ## Motivation
214
+ JSON format is good enough for everyday usage. There are some libraries trying to introduce syntax to make JSON closer
215
+ to modern JavaScript, some libraries trying to introduce functions serialization. All that is not important and is not
216
+ required for everyday usage. However, there is one thing annoying me always - date values.
217
+
218
+ We are serializing dates a lot and each time we parse it back we are getting a string. As a result we have to deal with
219
+ the Date constructor manually each time. Even if we are no need date as an object, date formatter will have to make date
220
+ object in order to make user-friendly text representation. Otherwords we are forced to care about dates additionally.
221
+ It produces bulky solutions or tons of inline type conversions.
222
+
223
+ But I'm lazy developer, I'll do everything to get rid of any additional careness.
0 commit comments