Skip to content

Commit 2919b12

Browse files
author
Dmitry Dutikov
committed
Readme updated
1 parent 10643d7 commit 2919b12

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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
3839
JSON format is good enough for everyday usage. There is some libraries trying to introduce syntax to make JSON closer
3940
to 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

4648
But 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
5054
class JSON22 {
5155
static parse(text: string, options?: Json22ParseOptions): any;
@@ -87,7 +91,7 @@ JSON22.parse('{ "bigint": 123n }'); // => { bigint: 123n }
8791

8892
### Trailing commas
8993
It 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
9397
JSON.parse('[1, 2, 3, ]'); // => Uncaught SyntaxError: Unexpected token ] in JSON at position 9
@@ -109,22 +113,20 @@ JSON22.stringify(date); // => Date(1641513600000)
109113
const date = JSON22.parse('Date(1641513600000)');
110114
console.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.
113117
In case JSON22 find the `valueOf` method return a value which is not equal of the object itself then it will produce
114118
constructor 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
118122
To match this behavior you may implement you own `valueOf` method at you custom class.
119123

120124
Let's define a model class for demonstration
121125
```javascript
122126
class 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`
165167
and `toJSON` both.

0 commit comments

Comments
 (0)