Skip to content

Commit 1fc7d5a

Browse files
committed
Updated docs
1 parent 9700ebc commit 1fc7d5a

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fast-json-stringify obj x 5,085,148 ops/sec ±1.56% (89 runs sampled)
2626
- <a href="#missingFields">`Missing fields`</a>
2727
- <a href="#patternProperties">`Pattern Properties`</a>
2828
- <a href="#additionalProperties">`Additional Properties`</a>
29+
- <a href="#ref">`Reuse - $ref`</a>
2930
- <a href="#acknowledgements">`Acknowledgements`</a>
3031
- <a href="#license">`License`</a>
3132

@@ -214,6 +215,85 @@ const obj = {
214215
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
215216
```
216217

218+
<a name="ref"></a>
219+
#### Reuse - $ref
220+
If you want to reuse a definition of a value, you can use the property `$ref`.
221+
The value of `$ref` must be a string in [JSON Pointer](https://tools.ietf.org/html/rfc6901) format.
222+
Example:
223+
```javascript
224+
const schema = {
225+
title: 'Example Schema',
226+
definitions: {
227+
num: {
228+
type: 'object',
229+
properties: {
230+
int: {
231+
type: 'integer'
232+
}
233+
}
234+
},
235+
str: {
236+
type: 'string'
237+
}
238+
},
239+
type: 'object',
240+
properties: {
241+
nickname: {
242+
$ref: '#/definitions/str'
243+
}
244+
},
245+
patternProperties: {
246+
'num': {
247+
$ref: '#/definitions/num'
248+
}
249+
},
250+
additionalProperties: {
251+
$ref: '#/definitions/def'
252+
}
253+
}
254+
255+
const stringify = fastJson(schema)
256+
```
257+
If you need to use an external definition, you can pass it as an option to `fast-json-stringify`.
258+
Example:
259+
```javascript
260+
const schema = {
261+
title: 'Example Schema',
262+
type: 'object',
263+
properties: {
264+
nickname: {
265+
$ref: 'strings#/definitions/str'
266+
}
267+
},
268+
patternProperties: {
269+
'num': {
270+
$ref: 'numbers#/definitions/num'
271+
}
272+
},
273+
additionalProperties: {
274+
$ref: 'strings#/definitions/def'
275+
}
276+
}
277+
278+
const externalSchema = {
279+
numbers: {
280+
definitions: {
281+
num: {
282+
type: 'object',
283+
properties: {
284+
int: {
285+
type: 'integer'
286+
}
287+
}
288+
}
289+
}
290+
},
291+
strings: require('./string-def.json')
292+
}
293+
294+
const stringify = fastJson(schema, { schema: externalSchema })
295+
```
296+
217297
<a name="acknowledgements"></a>
218298
## Acknowledgements
219299

0 commit comments

Comments
 (0)