11# JSON22 - JSON with types
2- The JSON22 is a superset of [ JSON] ( https://tools.ietf.org/html/rfc7159 ) with an ability to serialize/deserialize classes and extended support for number variables
2+ The JSON22 is a superset of [ JSON] ( https://tools.ietf.org/html/rfc7159 ) with an ability to serialize/deserialize
3+ classes and extended support for number variables.
34
45## TL;DR
56### To there ...
@@ -37,15 +38,18 @@ console.log(typeof value.debt, isNaN(value.debt)); // => number true
3738## Motivation
3839JSON format is good enough for everyday usage. There is some libraries trying to introduce syntax to make JSON closer
3940to modern JavaScript, some libraries trying to introduce functions serialization. All that is not important and do not
40- required for everyday usage. However, there is one think annoing me always - date values.
41+ required for everyday usage. However, there is one thing annoing me always - date values.
4142
42- We are serializing dates a lot and each time we parse it back we get a string. As a result we have to deal with the Date
43- constructor manually each time. Even we are no need date as object we will have to format it out to make more user friendly.
44- Otherwords we should care about dates additionally.
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.
4547
4648But I'm lazy developer, I'll do everything to get rid of any additional careness.
4749
4850## API
51+ Note: JSON22 cannot be used as drop in JSON object replacement due to ` parse ` and ` stringify ` methods
52+ arguments incompatibility. But you may not be worried in case you are using first arguments only.
4953``` typescript
5054class JSON22 {
5155 static parse(text : string , options ? : Json22ParseOptions ): any ;
@@ -87,7 +91,7 @@ JSON22.parse('{ "bigint": 123n }'); // => { bigint: 123n }
8791
8892### Trailing commas
8993It was not planned, but parser implementation work well with trailing commas.
90- We are not going to complicate parser code to avoid it. It looks useful.
94+ There is no sense to complicate the parser code to avoid it. It looks useful.
9195
9296``` javascript
9397JSON .parse (' [1, 2, 3, ]' ); // => Uncaught SyntaxError: Unexpected token ] in JSON at position 9
@@ -109,22 +113,20 @@ JSON22.stringify(date); // => Date(1641513600000)
109113const date = JSON22 .parse (' Date(1641513600000)' );
110114console .log (typeof date, date instanceof Date ); // => object true
111115```
112- This behavior is based on the ` valueOf ` method which is defined at the Object class.
116+ This behavior is based on the ` valueOf ` method which is defined at the Object class.
113117In case JSON22 find the ` valueOf ` method return a value which is not equal of the object itself then it will produce
114118constructor literal. The ` valueOf ` of the Date class return numeric date representation.
115- It you'll call Date constructor with that value date will be sort of 'restored'.
119+ If you'll call the Date constructor with that value then date will be sort of 'restored'.
116120
117121#### Custom valueOf implementation
118122To match this behavior you may implement you own ` valueOf ` method at you custom class.
119123
120124Let's define a model class for demonstration
121125``` javascript
122126class TypedModel {
123- a;
124- b;
125127 constructor (data ) {
126- this .a = data .a ;
127- this .b = data .b ;
128+ this .a = data? .a ;
129+ this .b = data? .b ;
128130 }
129131 valueOf () {
130132 return { a: this .a , b: this .b };
@@ -159,7 +161,7 @@ console.log(value instanceof TypedModel); // => true
159161` ` `
160162
161163#### The ` valueOf` method priority
162- The ` JSON22 ` support for ` toJSON ` method of an object as well as ` JSON ` . In some cases an object may have both ` valueOf `
163- and ` toJSON ` methods. Typical example is the Date class. The ` JSON22 ` at first is a solution to serialize/deserialize
164- date values, so ` valueOf ` have higher priority then ` toJSON ` . This is also true for any object implementing ` valueOf `
164+ The JSON22 support for ` toJSON` method of an object as well as JSON. In some cases an object may have both ` valueOf`
165+ and ` toJSON` methods. Typical example is the Date class. The JSON22 at first is a solution to serialize/deserialize
166+ date values, so __ ` valueOf` have higher priority over ` toJSON` __ . This is also true for any object implementing ` valueOf`
165167and ` toJSON` both.
0 commit comments