@@ -17,6 +17,20 @@ JSON.stringify obj x 1,763,980 ops/sec ±1.30% (88 runs sampled)
17
17
fast-json-stringify obj x 5,085,148 ops/sec ±1.56% (89 runs sampled)
18
18
```
19
19
20
+ #### Table of contents:
21
+ - <a href =" #example " >` Example ` </a >
22
+ - <a href =" #api " >` API ` </a >
23
+ - <a href =" #fastJsonStringify " >` fastJsonStringify ` </a >
24
+ - <a href =" #specific " >` Specific use cases ` </a >
25
+ - <a href =" #required " >` Required ` </a >
26
+ - <a href =" #missingFields " >` Missing fields ` </a >
27
+ - <a href =" #patternProperties " >` Pattern Properties ` </a >
28
+ - <a href =" #additionalProperties " >` Additional Properties ` </a >
29
+ - <a href =" #acknowledgements " >` Acknowledgements ` </a >
30
+ - <a href =" #license " >` License ` </a >
31
+
32
+
33
+ <a name =" example " ></a >
20
34
## Example
21
35
22
36
``` js
@@ -48,9 +62,9 @@ console.log(stringify({
48
62
reg: / "([^ "] | \\ ")* "/
49
63
}))
50
64
```
51
-
65
+ < a name = " api " ></ a >
52
66
## API
53
-
67
+ < a name = " fastJsonStringify " ></ a >
54
68
### fastJsonStringify(schema)
55
69
56
70
Build a ` stringify() ` function based on
@@ -68,13 +82,15 @@ Supported types:
68
82
69
83
And nested ones, too.
70
84
85
+ <a name =" specific " ></a >
71
86
#### Specific use cases
72
87
73
88
| Instance | Serialized as |
74
89
| -----------| ------------------------------|
75
90
| ` Date ` | ` string ` via ` toISOString() ` |
76
91
| ` RegExp ` | ` string ` |
77
92
93
+ <a name =" required " ></a >
78
94
#### Required
79
95
You can set specific fields of an object as required in your schema, by adding the field name inside the ` required ` array in your schema.
80
96
Example:
@@ -95,6 +111,7 @@ const schema = {
95
111
```
96
112
If the object to stringify has not the required field(s), ` fast-json-stringify ` will throw an error.
97
113
114
+ <a name =" missingFields " ></a >
98
115
#### Missing fields
99
116
If a field * is present* in the schema (and is not required) but it * is not present* in the object to stringify, ` fast-json-stringify ` will not write it in the final string.
100
117
Example:
@@ -109,8 +126,7 @@ const stringify = fastJson({
109
126
mail: {
110
127
type: ' string'
111
128
}
112
- },
113
- required: [' mail' ]
129
+ }
114
130
})
115
131
116
132
const obj = {
@@ -120,6 +136,7 @@ const obj = {
120
136
console .log (stringify (obj)) // '{"mail":"mail@example.com"}'
121
137
```
122
138
139
+ <a name =" patternProperties " ></a >
123
140
#### Pattern properties
124
141
` fast-json-stringify ` supports pattern properties as defined inside JSON schema.
125
142
* patternProperties* must be an object, where the key is a valid regex and the value is an object, declared in this way: ` { type: 'type' } ` .
@@ -151,7 +168,9 @@ const obj = {
151
168
matchnum: 3
152
169
}
153
170
154
- console .log (stringify (obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3}'
171
+ console .log (stringify (obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nickname":"nick"}'
172
+ ```
173
+
155
174
<a name =" additionalProperties " ></a >
156
175
#### Additional properties
157
176
` fast-json-stringify ` supports additional properties as defined inside JSON schema.
@@ -193,10 +212,12 @@ const obj = {
193
212
console .log (stringify (obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
194
213
```
195
214
215
+ <a name =" acknowledgements " ></a >
196
216
## Acknowledgements
197
217
198
218
This project was kindly sponsored by [ nearForm] ( http://nearform.com ) .
199
219
220
+ <a name =" license " ></a >
200
221
## License
201
222
202
223
MIT
0 commit comments