fast-json-stringify 2.7.13 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +7 -10
- package/.github/workflows/ci.yml +6 -2
- package/README.md +37 -63
- package/bench.js +44 -0
- package/build-schema-validator.js +14 -9
- package/index.js +44 -75
- package/package.json +4 -4
- package/schema-validator.js +1132 -1748
- package/test/allof.test.js +1 -1
- package/test/bigint.test.js +76 -0
- package/test/debug-mode.test.js +51 -14
- package/test/if-then-else.test.js +16 -1
- package/test/integer.test.js +1 -10
- package/test/nullable.test.js +9 -4
- package/test/side-effect.test.js +39 -0
- package/test/typebox.test.js +36 -0
- package/test/types/tsconfig.json +3 -1
- package/test/typesArray.test.js +2 -2
- package/test/bigint.js +0 -74
- package/test/long.test.js +0 -85
package/.github/dependabot.yml
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
version: 2
|
|
2
2
|
updates:
|
|
3
|
-
- package-ecosystem: github-actions
|
|
4
|
-
directory:
|
|
5
|
-
ignore:
|
|
6
|
-
- dependency-name: "actions/*"
|
|
7
|
-
update-types:
|
|
8
|
-
["version-update:semver-minor", "version-update:semver-patch"]
|
|
3
|
+
- package-ecosystem: "github-actions"
|
|
4
|
+
directory: "/"
|
|
9
5
|
schedule:
|
|
10
|
-
interval:
|
|
6
|
+
interval: "monthly"
|
|
11
7
|
open-pull-requests-limit: 10
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
|
|
9
|
+
- package-ecosystem: "npm"
|
|
10
|
+
directory: "/"
|
|
14
11
|
schedule:
|
|
15
|
-
interval:
|
|
12
|
+
interval: "weekly"
|
|
16
13
|
open-pull-requests-limit: 10
|
package/.github/workflows/ci.yml
CHANGED
|
@@ -15,7 +15,7 @@ jobs:
|
|
|
15
15
|
runs-on: ${{ matrix.os }}
|
|
16
16
|
strategy:
|
|
17
17
|
matrix:
|
|
18
|
-
node-version: [
|
|
18
|
+
node-version: [14, 16, 17]
|
|
19
19
|
os: [macos-latest, ubuntu-latest, windows-latest]
|
|
20
20
|
|
|
21
21
|
steps:
|
|
@@ -37,7 +37,11 @@ jobs:
|
|
|
37
37
|
automerge:
|
|
38
38
|
needs: test
|
|
39
39
|
runs-on: ubuntu-latest
|
|
40
|
+
permissions:
|
|
41
|
+
pull-requests: write
|
|
42
|
+
contents: write
|
|
40
43
|
steps:
|
|
41
|
-
- uses: fastify/github-action-merge-dependabot@
|
|
44
|
+
- uses: fastify/github-action-merge-dependabot@v3
|
|
42
45
|
with:
|
|
43
46
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
|
47
|
+
target: minor
|
package/README.md
CHANGED
|
@@ -7,7 +7,14 @@
|
|
|
7
7
|
[](https://www.npmjs.com/package/fast-json-stringify)
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads.
|
|
10
|
+
__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads.
|
|
11
|
+
Its performance advantage shrinks as your payload grows.
|
|
12
|
+
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`.
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### How it works
|
|
16
|
+
|
|
17
|
+
fast-json-stringify requires a [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7) input to generate a fast `stringify` function.
|
|
11
18
|
|
|
12
19
|
##### Benchmarks
|
|
13
20
|
|
|
@@ -15,22 +22,28 @@ __fast-json-stringify__ is significantly faster than `JSON.stringify()` for smal
|
|
|
15
22
|
- Node.js `v16.9.1`
|
|
16
23
|
|
|
17
24
|
```
|
|
18
|
-
FJS creation x
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
JSON.stringify
|
|
30
|
-
fast-json-stringify
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
25
|
+
FJS creation x 8,443 ops/sec ±1.01% (90 runs sampled)
|
|
26
|
+
CJS creation x 183,219 ops/sec ±0.13% (96 runs sampled)
|
|
27
|
+
AJV Serialize creation x 83,541,848 ops/sec ±0.24% (98 runs sampled)
|
|
28
|
+
JSON.stringify array x 5,363 ops/sec ±0.11% (100 runs sampled)
|
|
29
|
+
fast-json-stringify array x 6,747 ops/sec ±0.13% (98 runs sampled)
|
|
30
|
+
compile-json-stringify array x 7,121 ops/sec ±0.42% (98 runs sampled)
|
|
31
|
+
AJV Serialize array x 7,533 ops/sec ±0.13% (98 runs sampled)
|
|
32
|
+
JSON.stringify long string x 16,461 ops/sec ±0.12% (98 runs sampled)
|
|
33
|
+
fast-json-stringify long string x 16,443 ops/sec ±0.37% (99 runs sampled)
|
|
34
|
+
compile-json-stringify long string x 16,458 ops/sec ±0.09% (98 runs sampled)
|
|
35
|
+
AJV Serialize long string x 21,433 ops/sec ±0.08% (95 runs sampled)
|
|
36
|
+
JSON.stringify short string x 12,035,664 ops/sec ±0.62% (96 runs sampled)
|
|
37
|
+
fast-json-stringify short string x 38,281,060 ops/sec ±0.24% (98 runs sampled)
|
|
38
|
+
compile-json-stringify short string x 32,388,037 ops/sec ±0.27% (97 runs sampled)
|
|
39
|
+
AJV Serialize short string x 32,288,612 ops/sec ±0.32% (95 runs sampled)
|
|
40
|
+
JSON.stringify obj x 3,068,185 ops/sec ±0.16% (98 runs sampled)
|
|
41
|
+
fast-json-stringify obj x 10,082,694 ops/sec ±0.10% (97 runs sampled)
|
|
42
|
+
compile-json-stringify obj x 17,037,963 ops/sec ±1.17% (97 runs sampled)
|
|
43
|
+
AJV Serialize obj x 9,660,041 ops/sec ±0.11% (97 runs sampled)
|
|
44
|
+
JSON stringify date x 1,084,008 ops/sec ±0.16% (98 runs sampled)
|
|
45
|
+
fast-json-stringify date format x 1,781,044 ops/sec ±0.48% (99 runs sampled)
|
|
46
|
+
compile-json-stringify date format x 1,086,187 ops/sec ±0.16% (99 runs sampled)
|
|
34
47
|
```
|
|
35
48
|
|
|
36
49
|
#### Table of contents:
|
|
@@ -43,7 +56,7 @@ fast-json-stringify date format x 1,674,498 ops/sec ±0.12% (99 runs sampled)
|
|
|
43
56
|
- <a href="#missingFields">`Missing fields`</a>
|
|
44
57
|
- <a href="#patternProperties">`Pattern Properties`</a>
|
|
45
58
|
- <a href="#additionalProperties">`Additional Properties`</a>
|
|
46
|
-
- <a href="#anyof">`AnyOf`</a>
|
|
59
|
+
- <a href="#anyof">`AnyOf` and `OneOf`</a>
|
|
47
60
|
- <a href="#ref">`Reuse - $ref`</a>
|
|
48
61
|
- <a href="#long">`Long integers`</a>
|
|
49
62
|
- <a href="#integer">`Integers`</a>
|
|
@@ -102,7 +115,7 @@ const stringify = fastJson(mySchema, {
|
|
|
102
115
|
```
|
|
103
116
|
|
|
104
117
|
- `schema`: external schemas references by $ref property. [More details](#ref)
|
|
105
|
-
- `ajv`: ajv instance's settings for those properties that require `ajv`. [More details](#anyof)
|
|
118
|
+
- `ajv`: [ajv v8 instance's settings](https://ajv.js.org/options.html) for those properties that require `ajv`. [More details](#anyof)
|
|
106
119
|
- `rounding`: setup how the `integer` types will be rounded when not integers. [More details](#integer)
|
|
107
120
|
|
|
108
121
|
|
|
@@ -111,7 +124,7 @@ const stringify = fastJson(mySchema, {
|
|
|
111
124
|
<a name="fastJsonStringify"></a>
|
|
112
125
|
### fastJsonStringify(schema)
|
|
113
126
|
|
|
114
|
-
Build a `stringify()` function based on [jsonschema](https://json-schema.org/).
|
|
127
|
+
Build a `stringify()` function based on [jsonschema draft 7 spec](https://json-schema.org/specification-links.html#draft-7).
|
|
115
128
|
|
|
116
129
|
Supported types:
|
|
117
130
|
|
|
@@ -307,11 +320,11 @@ const obj = {
|
|
|
307
320
|
console.log(stringify(obj)) // '{"nickname":"nick","matchfoo":"42","otherfoo":"str","matchnum":3,"nomatchstr":"valar morghulis",nomatchint:"313"}'
|
|
308
321
|
```
|
|
309
322
|
|
|
310
|
-
#### AnyOf
|
|
323
|
+
#### AnyOf and OneOf
|
|
311
324
|
|
|
312
|
-
`fast-json-stringify` supports the anyOf
|
|
325
|
+
`fast-json-stringify` supports the **anyOf** and **oneOf** keywords as defined by JSON schema. Both must be an array of valid JSON schemas. The different schemas will be tested in the specified order. The more schemas `stringify` has to try before finding a match, the slower it will be.
|
|
313
326
|
|
|
314
|
-
*anyOf*
|
|
327
|
+
*anyOf* and *oneOf* use [ajv](https://www.npmjs.com/package/ajv) as a JSON schema validator to find the schema that matches the data. This has an impact on performance—only use it as a last resort.
|
|
315
328
|
|
|
316
329
|
Example:
|
|
317
330
|
```javascript
|
|
@@ -521,46 +534,7 @@ const stringify = fastJson(schema, { schema: externalSchema })
|
|
|
521
534
|
|
|
522
535
|
<a name="long"></a>
|
|
523
536
|
#### Long integers
|
|
524
|
-
By default the library will automatically
|
|
525
|
-
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.
|
|
526
|
-
Example:
|
|
527
|
-
```javascript
|
|
528
|
-
// => using native BigInt
|
|
529
|
-
const stringify = fastJson({
|
|
530
|
-
title: 'Example Schema',
|
|
531
|
-
type: 'object',
|
|
532
|
-
properties: {
|
|
533
|
-
id: {
|
|
534
|
-
type: 'integer'
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
})
|
|
538
|
-
|
|
539
|
-
const obj = {
|
|
540
|
-
id: 18446744073709551615n
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
console.log(stringify(obj)) // '{"id":18446744073709551615}'
|
|
544
|
-
|
|
545
|
-
// => using the long library
|
|
546
|
-
const Long = require('long')
|
|
547
|
-
|
|
548
|
-
const stringify = fastJson({
|
|
549
|
-
title: 'Example Schema',
|
|
550
|
-
type: 'object',
|
|
551
|
-
properties: {
|
|
552
|
-
id: {
|
|
553
|
-
type: 'integer'
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
})
|
|
557
|
-
|
|
558
|
-
const obj = {
|
|
559
|
-
id: Long.fromString('18446744073709551615', true)
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
console.log(stringify(obj)) // '{"id":18446744073709551615}'
|
|
563
|
-
```
|
|
537
|
+
By default the library will handle automatically [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
|
|
564
538
|
|
|
565
539
|
<a name="integer"></a>
|
|
566
540
|
#### Integers
|
package/bench.js
CHANGED
|
@@ -38,6 +38,21 @@ const schemaCJS = {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
const schemaAJVJTD = {
|
|
42
|
+
properties: {
|
|
43
|
+
firstName: {
|
|
44
|
+
type: 'string'
|
|
45
|
+
},
|
|
46
|
+
lastName: {
|
|
47
|
+
type: 'string',
|
|
48
|
+
nullable: true
|
|
49
|
+
},
|
|
50
|
+
age: {
|
|
51
|
+
type: 'uint8'
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
41
56
|
const arraySchema = {
|
|
42
57
|
title: 'array schema',
|
|
43
58
|
type: 'array',
|
|
@@ -50,6 +65,10 @@ const arraySchemaCJS = {
|
|
|
50
65
|
items: schemaCJS
|
|
51
66
|
}
|
|
52
67
|
|
|
68
|
+
const arraySchemaAJVJTD = {
|
|
69
|
+
elements: schemaAJVJTD
|
|
70
|
+
}
|
|
71
|
+
|
|
53
72
|
const dateFormatSchema = {
|
|
54
73
|
description: 'Date of birth',
|
|
55
74
|
type: 'string',
|
|
@@ -85,6 +104,12 @@ const stringifyDate = FJS(dateFormatSchema)
|
|
|
85
104
|
const stringifyString = FJS({ type: 'string' })
|
|
86
105
|
let str = ''
|
|
87
106
|
|
|
107
|
+
const Ajv = require('ajv/dist/jtd')
|
|
108
|
+
const ajv = new Ajv()
|
|
109
|
+
const ajvSerialize = ajv.compileSerializer(schemaAJVJTD)
|
|
110
|
+
const ajvSerializeArray = ajv.compileSerializer(arraySchemaAJVJTD)
|
|
111
|
+
const ajvSerializeString = ajv.compileSerializer({ type: 'string' })
|
|
112
|
+
|
|
88
113
|
// eslint-disable-next-line
|
|
89
114
|
for (var i = 0; i < 10000; i++) {
|
|
90
115
|
str += i
|
|
@@ -105,6 +130,9 @@ suite.add('FJS creation', function () {
|
|
|
105
130
|
suite.add('CJS creation', function () {
|
|
106
131
|
CJS(schemaCJS)
|
|
107
132
|
})
|
|
133
|
+
suite.add('AJV Serialize creation', function () {
|
|
134
|
+
ajv.compileSerializer(schemaAJVJTD)
|
|
135
|
+
})
|
|
108
136
|
|
|
109
137
|
suite.add('JSON.stringify array', function () {
|
|
110
138
|
JSON.stringify(multiArray)
|
|
@@ -118,6 +146,10 @@ suite.add('compile-json-stringify array', function () {
|
|
|
118
146
|
CJSStringifyArray(multiArray)
|
|
119
147
|
})
|
|
120
148
|
|
|
149
|
+
suite.add('AJV Serialize array', function () {
|
|
150
|
+
ajvSerializeArray(multiArray)
|
|
151
|
+
})
|
|
152
|
+
|
|
121
153
|
suite.add('JSON.stringify long string', function () {
|
|
122
154
|
JSON.stringify(str)
|
|
123
155
|
})
|
|
@@ -130,6 +162,10 @@ suite.add('compile-json-stringify long string', function () {
|
|
|
130
162
|
CJSStringifyString(str)
|
|
131
163
|
})
|
|
132
164
|
|
|
165
|
+
suite.add('AJV Serialize long string', function () {
|
|
166
|
+
ajvSerializeString(str)
|
|
167
|
+
})
|
|
168
|
+
|
|
133
169
|
suite.add('JSON.stringify short string', function () {
|
|
134
170
|
JSON.stringify('hello world')
|
|
135
171
|
})
|
|
@@ -142,6 +178,10 @@ suite.add('compile-json-stringify short string', function () {
|
|
|
142
178
|
CJSStringifyString('hello world')
|
|
143
179
|
})
|
|
144
180
|
|
|
181
|
+
suite.add('AJV Serialize short string', function () {
|
|
182
|
+
ajvSerializeString('hello world')
|
|
183
|
+
})
|
|
184
|
+
|
|
145
185
|
suite.add('JSON.stringify obj', function () {
|
|
146
186
|
JSON.stringify(obj)
|
|
147
187
|
})
|
|
@@ -154,6 +194,10 @@ suite.add('compile-json-stringify obj', function () {
|
|
|
154
194
|
CJSStringify(obj)
|
|
155
195
|
})
|
|
156
196
|
|
|
197
|
+
suite.add('AJV Serialize obj', function () {
|
|
198
|
+
ajvSerialize(obj)
|
|
199
|
+
})
|
|
200
|
+
|
|
157
201
|
suite.add('JSON stringify date', function () {
|
|
158
202
|
JSON.stringify(date)
|
|
159
203
|
})
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const Ajv = require('ajv')
|
|
4
|
+
const standaloneCode = require('ajv/dist/standalone').default
|
|
5
|
+
const ajvFormats = require('ajv-formats')
|
|
4
6
|
const fs = require('fs')
|
|
5
7
|
const path = require('path')
|
|
6
|
-
const pack = require('ajv-pack')
|
|
7
8
|
|
|
8
9
|
const ajv = new Ajv({
|
|
9
|
-
|
|
10
|
+
addUsedSchema: false,
|
|
11
|
+
allowUnionTypes: true,
|
|
12
|
+
code: {
|
|
13
|
+
source: true,
|
|
14
|
+
lines: true,
|
|
15
|
+
optimize: 3
|
|
16
|
+
}
|
|
10
17
|
})
|
|
18
|
+
ajvFormats(ajv)
|
|
11
19
|
|
|
12
|
-
const
|
|
20
|
+
const schema = require('ajv/lib/refs/json-schema-draft-07.json')
|
|
21
|
+
const validate = ajv.compile(schema)
|
|
22
|
+
const validationCode = standaloneCode(ajv, validate)
|
|
13
23
|
|
|
14
|
-
const moduleCode =
|
|
15
|
-
|
|
16
|
-
function nop () { return true }
|
|
17
|
-
|
|
18
|
-
${pack(ajv, validate).replace(/root\.refVal\[0\]/gm, 'nop')}
|
|
19
|
-
`
|
|
24
|
+
const moduleCode = `/* CODE GENERATED BY '${path.basename(__filename)}' DO NOT EDIT! */\n${validationCode}`
|
|
20
25
|
|
|
21
26
|
fs.writeFileSync(path.join(__dirname, 'schema-validator.js'), moduleCode)
|
package/index.js
CHANGED
|
@@ -3,20 +3,15 @@
|
|
|
3
3
|
/* eslint no-prototype-builtins: 0 */
|
|
4
4
|
|
|
5
5
|
const Ajv = require('ajv')
|
|
6
|
+
const ajvFormats = require('ajv-formats')
|
|
6
7
|
const merge = require('deepmerge')
|
|
7
8
|
const clone = require('rfdc')({ proto: true })
|
|
8
9
|
const fjsCloned = Symbol('fast-json-stringify.cloned')
|
|
10
|
+
const { randomUUID } = require('crypto')
|
|
9
11
|
|
|
10
12
|
const validate = require('./schema-validator')
|
|
11
13
|
let stringSimilarity = null
|
|
12
14
|
|
|
13
|
-
let isLong
|
|
14
|
-
try {
|
|
15
|
-
isLong = require('long').isLong
|
|
16
|
-
} catch (e) {
|
|
17
|
-
isLong = null
|
|
18
|
-
}
|
|
19
|
-
|
|
20
15
|
const addComma = `
|
|
21
16
|
if (addComma) {
|
|
22
17
|
json += ','
|
|
@@ -33,7 +28,7 @@ function isValidSchema (schema, name) {
|
|
|
33
28
|
name = ''
|
|
34
29
|
}
|
|
35
30
|
const first = validate.errors[0]
|
|
36
|
-
const err = new Error(`${name}schema is invalid: data${first.
|
|
31
|
+
const err = new Error(`${name}schema is invalid: data${first.instancePath} ${first.message}`)
|
|
37
32
|
err.errors = isValidSchema.errors
|
|
38
33
|
throw err
|
|
39
34
|
}
|
|
@@ -49,11 +44,17 @@ function mergeLocation (source, dest) {
|
|
|
49
44
|
|
|
50
45
|
const arrayItemsReferenceSerializersMap = new Map()
|
|
51
46
|
const objectReferenceSerializersMap = new Map()
|
|
47
|
+
let ajvInstance = null
|
|
52
48
|
|
|
53
49
|
function build (schema, options) {
|
|
54
50
|
arrayItemsReferenceSerializersMap.clear()
|
|
55
51
|
objectReferenceSerializersMap.clear()
|
|
52
|
+
|
|
56
53
|
options = options || {}
|
|
54
|
+
|
|
55
|
+
ajvInstance = new Ajv({ ...options.ajv, strictSchema: false })
|
|
56
|
+
ajvFormats(ajvInstance)
|
|
57
|
+
|
|
57
58
|
isValidSchema(schema)
|
|
58
59
|
if (options.schema) {
|
|
59
60
|
// eslint-disable-next-line
|
|
@@ -78,9 +79,6 @@ function build (schema, options) {
|
|
|
78
79
|
|
|
79
80
|
code += `
|
|
80
81
|
${asFunctions}
|
|
81
|
-
|
|
82
|
-
var isLong = ${isLong ? isLong.toString() : false}
|
|
83
|
-
|
|
84
82
|
function parseInteger(int) { return Math.${intParseFunctionName}(int) }
|
|
85
83
|
`
|
|
86
84
|
|
|
@@ -137,16 +135,17 @@ function build (schema, options) {
|
|
|
137
135
|
;
|
|
138
136
|
return ${main}
|
|
139
137
|
`
|
|
140
|
-
|
|
141
|
-
const dependencies = [new Ajv(options.ajv)]
|
|
138
|
+
const dependencies = [ajvInstance]
|
|
142
139
|
const dependenciesName = ['ajv']
|
|
140
|
+
ajvInstance = null
|
|
141
|
+
|
|
143
142
|
dependenciesName.push(code)
|
|
144
143
|
|
|
145
144
|
if (options.debugMode) {
|
|
146
|
-
|
|
147
|
-
|
|
145
|
+
return {
|
|
146
|
+
code: dependenciesName.join('\n'),
|
|
147
|
+
ajv: dependencies[0]
|
|
148
148
|
}
|
|
149
|
-
return dependenciesName
|
|
150
149
|
}
|
|
151
150
|
|
|
152
151
|
arrayItemsReferenceSerializersMap.clear()
|
|
@@ -242,9 +241,7 @@ function $asNull () {
|
|
|
242
241
|
}
|
|
243
242
|
|
|
244
243
|
function $asInteger (i) {
|
|
245
|
-
if (
|
|
246
|
-
return i.toString()
|
|
247
|
-
} else if (typeof i === 'bigint') {
|
|
244
|
+
if (typeof i === 'bigint') {
|
|
248
245
|
return i.toString()
|
|
249
246
|
} else if (Number.isInteger(i)) {
|
|
250
247
|
return $asNumber(i)
|
|
@@ -518,22 +515,11 @@ function additionalProperty (location) {
|
|
|
518
515
|
} else if (type === 'integer') {
|
|
519
516
|
code += `
|
|
520
517
|
var t = Number(obj[keys[i]])
|
|
518
|
+
if (!isNaN(t)) {
|
|
519
|
+
${addComma}
|
|
520
|
+
json += $asString(keys[i]) + ':' + t
|
|
521
|
+
}
|
|
521
522
|
`
|
|
522
|
-
if (isLong) {
|
|
523
|
-
code += `
|
|
524
|
-
if (isLong(obj[keys[i]]) || !isNaN(t)) {
|
|
525
|
-
${addComma}
|
|
526
|
-
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
|
|
527
|
-
}
|
|
528
|
-
`
|
|
529
|
-
} else {
|
|
530
|
-
code += `
|
|
531
|
-
if (!isNaN(t)) {
|
|
532
|
-
${addComma}
|
|
533
|
-
json += $asString(keys[i]) + ':' + t
|
|
534
|
-
}
|
|
535
|
-
`
|
|
536
|
-
}
|
|
537
523
|
} else if (type === 'number') {
|
|
538
524
|
code += `
|
|
539
525
|
var t = Number(obj[keys[i]])
|
|
@@ -735,33 +721,12 @@ function buildCode (location, code, laterCode, name) {
|
|
|
735
721
|
} else if (type === 'integer') {
|
|
736
722
|
code += `
|
|
737
723
|
var rendered = false
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
rendered = true
|
|
745
|
-
} else {
|
|
746
|
-
var t = Number(obj[${sanitized}])
|
|
747
|
-
if (!isNaN(t)) {
|
|
748
|
-
${addComma}
|
|
749
|
-
json += ${asString} + ':' + t
|
|
750
|
-
rendered = true
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
`
|
|
754
|
-
} else {
|
|
755
|
-
code += `
|
|
756
|
-
var t = $asInteger(obj[${sanitized}])
|
|
757
|
-
if (!isNaN(t)) {
|
|
758
|
-
${addComma}
|
|
759
|
-
json += ${asString} + ':' + t
|
|
760
|
-
rendered = true
|
|
761
|
-
}
|
|
762
|
-
`
|
|
763
|
-
}
|
|
764
|
-
code += `
|
|
724
|
+
var t = $asInteger(obj[${sanitized}])
|
|
725
|
+
if (!isNaN(t)) {
|
|
726
|
+
${addComma}
|
|
727
|
+
json += ${asString} + ':' + t
|
|
728
|
+
rendered = true
|
|
729
|
+
}
|
|
765
730
|
if (rendered) {
|
|
766
731
|
`
|
|
767
732
|
} else {
|
|
@@ -880,8 +845,11 @@ function addIfThenElse (location, name) {
|
|
|
880
845
|
let merged = merge(copy, then)
|
|
881
846
|
let mergedLocation = mergeLocation(location, { schema: merged })
|
|
882
847
|
|
|
848
|
+
const schemaKey = i.$id || randomUUID()
|
|
849
|
+
ajvInstance.addSchema(i, schemaKey)
|
|
850
|
+
|
|
883
851
|
code += `
|
|
884
|
-
valid = ajv.validate(${
|
|
852
|
+
valid = ajv.validate("${schemaKey}", obj)
|
|
885
853
|
if (valid) {
|
|
886
854
|
`
|
|
887
855
|
if (merged.if && merged.then) {
|
|
@@ -941,7 +909,7 @@ function buildObject (location, code, name) {
|
|
|
941
909
|
`
|
|
942
910
|
}
|
|
943
911
|
|
|
944
|
-
if (objectReferenceSerializersMap.has(schema)) {
|
|
912
|
+
if (objectReferenceSerializersMap.has(schema) && objectReferenceSerializersMap.get(schema) !== name) {
|
|
945
913
|
code += `
|
|
946
914
|
return ${objectReferenceSerializersMap.get(schema)}(input)
|
|
947
915
|
}
|
|
@@ -1230,8 +1198,12 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
|
|
1230
1198
|
// with the actual schema
|
|
1231
1199
|
// 2. `nested`, through `buildCode`, replaces any reference in object properties with the actual schema
|
|
1232
1200
|
// (see https://github.com/fastify/fast-json-stringify/blob/6da3b3e8ac24b1ca5578223adedb4083b7adf8db/index.js#L631)
|
|
1201
|
+
|
|
1202
|
+
const schemaKey = location.schema.$id || randomUUID()
|
|
1203
|
+
ajvInstance.addSchema(location.schema, schemaKey)
|
|
1204
|
+
|
|
1233
1205
|
code += `
|
|
1234
|
-
${index === 0 ? 'if' : 'else if'}(ajv.validate(${
|
|
1206
|
+
${index === 0 ? 'if' : 'else if'}(ajv.validate("${schemaKey}", ${testValue}))
|
|
1235
1207
|
${nestedResult.code}
|
|
1236
1208
|
`
|
|
1237
1209
|
laterCode = nestedResult.laterCode
|
|
@@ -1247,8 +1219,12 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
|
|
1247
1219
|
const testSerializer = getTestSerializer(location.schema.format)
|
|
1248
1220
|
const testValue = testSerializer !== undefined ? `${testSerializer}(obj${accessor}, true)` : `obj${accessor}`
|
|
1249
1221
|
// see comment on anyOf about dereferencing the schema before calling ajv.validate
|
|
1222
|
+
|
|
1223
|
+
const schemaKey = location.schema.$id || randomUUID()
|
|
1224
|
+
ajvInstance.addSchema(location.schema, schemaKey)
|
|
1225
|
+
|
|
1250
1226
|
code += `
|
|
1251
|
-
${index === 0 ? 'if' : 'else if'}(ajv.validate(${
|
|
1227
|
+
${index === 0 ? 'if' : 'else if'}(ajv.validate("${schemaKey}", ${testValue}))
|
|
1252
1228
|
${nestedResult.code}
|
|
1253
1229
|
`
|
|
1254
1230
|
laterCode = nestedResult.laterCode
|
|
@@ -1358,15 +1334,8 @@ function isEmpty (schema) {
|
|
|
1358
1334
|
|
|
1359
1335
|
module.exports = build
|
|
1360
1336
|
|
|
1361
|
-
module.exports.restore = function (
|
|
1362
|
-
const dependencies = [debugModeStr]
|
|
1363
|
-
const args = []
|
|
1364
|
-
if (debugModeStr.startsWith('ajv')) {
|
|
1365
|
-
dependencies.unshift('ajv')
|
|
1366
|
-
args.push(new Ajv(options.ajv))
|
|
1367
|
-
}
|
|
1368
|
-
|
|
1337
|
+
module.exports.restore = function ({ code, ajv }) {
|
|
1369
1338
|
// eslint-disable-next-line
|
|
1370
|
-
return (Function.apply(null, ['ajv',
|
|
1371
|
-
.apply(null,
|
|
1339
|
+
return (Function.apply(null, ['ajv', code])
|
|
1340
|
+
.apply(null, [ajv]))
|
|
1372
1341
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fast-json-stringify",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": "Stringify your JSON at max speed",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -30,11 +30,10 @@
|
|
|
30
30
|
},
|
|
31
31
|
"homepage": "https://github.com/fastify/fast-json-stringify#readme",
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"
|
|
33
|
+
"@sinclair/typebox": "^0.23.3",
|
|
34
34
|
"benchmark": "^2.1.4",
|
|
35
35
|
"compile-json-stringify": "^0.1.2",
|
|
36
36
|
"is-my-json-valid": "^2.20.0",
|
|
37
|
-
"long": "^4.0.0",
|
|
38
37
|
"moment": "^2.24.0",
|
|
39
38
|
"pre-commit": "^1.2.2",
|
|
40
39
|
"proxyquire": "^2.1.3",
|
|
@@ -45,7 +44,8 @@
|
|
|
45
44
|
"webpack": "^5.40.0"
|
|
46
45
|
},
|
|
47
46
|
"dependencies": {
|
|
48
|
-
"ajv": "^6.
|
|
47
|
+
"ajv": "^8.6.2",
|
|
48
|
+
"ajv-formats": "^2.1.1",
|
|
49
49
|
"deepmerge": "^4.2.2",
|
|
50
50
|
"rfdc": "^1.2.0",
|
|
51
51
|
"string-similarity": "^4.0.1"
|