Skip to content

Commit 2a5ed41

Browse files
committed
Merge branch 'master' into next
2 parents beb46d6 + 1241c5d commit 2a5ed41

24 files changed

+1656
-134
lines changed

.dependabot/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: 1
2+
update_configs:
3+
- package_manager: "javascript"
4+
directory: "/"
5+
update_schedule: "daily"
6+
ignored_updates:
7+
- match:
8+
dependency_name: "tap"

.github/workflows/ci.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: CI workflow
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix:
8+
node-version: [6.x, 8.x, 10.x, 11.x, 12.x, 13.x]
9+
steps:
10+
- uses: actions/checkout@v2
11+
- name: Use Node.js ${{ matrix.node-version }}
12+
uses: actions/setup-node@v1
13+
with:
14+
node-version: ${{ matrix.node-version }}
15+
- name: Install Dependencies
16+
run: npm install --ignore-scripts
17+
- name: Test
18+
run: npm run test

.taprc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
esm: false
2+
ts: false
3+
jsx: false
4+
coverage: false

.travis.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

README.md

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# fast-json-stringify
22

3-
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Build Status](https://dev.azure.com/fastify/fastify/_apis/build/status/fastify.fast-json-stringify?branchName=master)](https://dev.azure.com/fastify/fastify/_build/latest?definitionId=3&branchName=master) [![Build Status](https://travis-ci.org/fastify/fast-json-stringify.svg?branch=master)](https://travis-ci.org/fastify/fast-json-stringify) [![NPM downloads](https://img.shields.io/npm/dm/fast-json-stringify.svg?style=flat)](https://www.npmjs.com/package/fast-json-stringify)
3+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
4+
![Ci Workflow](https://github.com/fastify/fast-json-stringify/workflows/CI%20workflow/badge.svg)
5+
[![NPM downloads](https://img.shields.io/npm/dm/fast-json-stringify.svg?style=flat)](https://www.npmjs.com/package/fast-json-stringify)
6+
47

58
__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads. Its performance advantage shrinks as your payload grows. It pairs well with [__flatstr__](https://www.npmjs.com/package/flatstr), which triggers a V8 optimization that improves performance when eventually converting the string to a `Buffer`.
69

@@ -97,10 +100,34 @@ And nested ones, too.
97100
<a name="specific"></a>
98101
#### Specific use cases
99102

100-
| Instance | Serialized as |
101-
| -----------|------------------------------|
102-
| `Date` | `string` via `toISOString()` |
103-
| `RegExp` | `string` |
103+
| Instance | Serialized as |
104+
| -------- | ---------------------------- |
105+
| `Date` | `string` via `toISOString()` |
106+
| `RegExp` | `string` |
107+
| `BigInt` | `integer` via `toString` |
108+
109+
[JSON Schema built-in formats](https://json-schema.org/understanding-json-schema/reference/string.html#built-in-formats) for dates are supported and will be serialized as:
110+
111+
| Format | Serialized format example |
112+
| ----------- | -------------------------- |
113+
| `date-time` | `2020-04-03T09:11:08.615Z` |
114+
| `date` | `2020-04-03` |
115+
| `time` | `09:11:08` |
116+
117+
Example with a MomentJS object:
118+
119+
```javascript
120+
const moment = require('moment')
121+
122+
const stringify = fastJson({
123+
title: 'Example Schema with string date-time field',
124+
type: 'string',
125+
format: 'date-time'
126+
}
127+
128+
console.log(stringify(moment())) // '"YYYY-MM-DDTHH:mm:ss.sssZ"'
129+
```
130+
104131
105132
<a name="required"></a>
106133
#### Required
@@ -214,6 +241,7 @@ console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"
214241
If *additionalProperties* is not present or is set to `false`, every property that is not explicitly listed in the *properties* and *patternProperties* objects,will be ignored, as described in <a href="#missingFields">Missing fields</a>.
215242
Missing fields are ignored to avoid having to rewrite objects before serializing. However, other schema rules would throw in similar situations.
216243
If *additionalProperties* is set to `true`, it will be used by `JSON.stringify` to stringify the additional properties. If you want to achieve maximum performance, we strongly encourage you to use a fixed schema where possible.
244+
The additional properties will always be serialzied at the end of the object.
217245
Example:
218246
```javascript
219247
const stringify = fastJson({
@@ -246,7 +274,7 @@ const obj = {
246274
nomatchint: 313
247275
}
248276

249-
console.log(stringify(obj)) // '{"matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313","nickname":"nick"}'
277+
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313"}'
250278
```
251279
252280
#### AnyOf
@@ -397,14 +425,65 @@ const externalSchema = {
397425
strings: require('./string-def.json')
398426
}
399427

428+
const stringify = fastJson(schema, { schema: externalSchema })
429+
```
430+
External definitions can also reference each other.
431+
Example:
432+
```javascript
433+
const schema = {
434+
title: 'Example Schema',
435+
type: 'object',
436+
properties: {
437+
foo: {
438+
$ref: 'strings#/definitions/foo'
439+
}
440+
}
441+
}
442+
443+
const externalSchema = {
444+
strings: {
445+
definitions: {
446+
foo: {
447+
$ref: 'things#/definitions/foo'
448+
}
449+
}
450+
},
451+
things: {
452+
definitions: {
453+
foo: {
454+
type: 'string'
455+
}
456+
}
457+
}
458+
}
459+
400460
const stringify = fastJson(schema, { schema: externalSchema })
401461
```
402462
403463
<a name="long"></a>
404464
#### Long integers
405-
Long integers (64-bit) are supported using the [long](https://github.com/dcodeIO/long.js) module.
465+
By default the library will handle automatically [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) from Node.js v10.3 and above.
466+
If you can't use BigInts in your environment, long integers (64-bit) are also supported using the [long](https://github.com/dcodeIO/long.js) module.
406467
Example:
407468
```javascript
469+
// => using native BigInt
470+
const stringify = fastJson({
471+
title: 'Example Schema',
472+
type: 'object',
473+
properties: {
474+
id: {
475+
type: 'integer'
476+
}
477+
}
478+
})
479+
480+
const obj = {
481+
id: 18446744073709551615n
482+
}
483+
484+
console.log(stringify(obj)) // '{"id":18446744073709551615}'
485+
486+
// => using the long library
408487
const Long = require('long')
409488

410489
const stringify = fastJson({
@@ -470,6 +549,31 @@ allowing user input to directly supply a schema.
470549
It can't be guaranteed that allowing user input for the schema couldn't feasibly expose an attack
471550
vector.
472551
552+
<a name="debug"></a>
553+
### Debug Mode
554+
555+
The debug mode can be activated during your development to understand what is going on when things do not
556+
work as you expect.
557+
558+
```js
559+
const debugCompiled = fastJson({
560+
title: 'default string',
561+
type: 'object',
562+
properties: {
563+
firstName: {
564+
type: 'string'
565+
}
566+
}
567+
}, { debugMode: true })
568+
569+
console.log(debugCompiled) // it is an array of functions that can create your `stringify` function
570+
console.log(debugCompiled.toString()) // print a "ready to read" string function, you can save it to a file
571+
572+
const rawString = debugCompiled.toString()
573+
const stringify = fastJson.restore(rawString) // use the generated string to get back the `stringify` function
574+
console.log(stringify({ firstName: 'Foo', surname: 'bar' })) // '{"firstName":"Foo"}'
575+
```
576+
473577
<a name="acknowledgements"></a>
474578
## Acknowledgements
475579

azure-pipelines.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

example.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const moment = require('moment')
34
const fastJson = require('.')
45
const stringify = fastJson({
56
title: 'Example Schema',
@@ -18,6 +19,10 @@ const stringify = fastJson({
1819
now: {
1920
type: 'string'
2021
},
22+
birthdate: {
23+
type: ['string'],
24+
format: 'date-time'
25+
},
2126
reg: {
2227
type: 'string'
2328
},
@@ -48,6 +53,10 @@ const stringify = fastJson({
4853
},
4954
test: {
5055
type: 'number'
56+
},
57+
date: {
58+
type: 'string',
59+
format: 'date-time'
5160
}
5261
},
5362
additionalProperties: {
@@ -60,10 +69,12 @@ console.log(stringify({
6069
lastName: 'Collina',
6170
age: 32,
6271
now: new Date(),
72+
birthdate: moment(),
6373
reg: /"([^"]|\\")*"/,
6474
foo: 'hello',
6575
numfoo: 42,
6676
test: 42,
77+
date: moment(),
6778
strtest: '23',
6879
arr: [{ str: 'stark' }, { str: 'lannister' }],
6980
obj: { bool: true },

0 commit comments

Comments
 (0)