fast-json-stringify 2.2.4 → 2.2.9
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/workflows/ci.yml +1 -1
- package/bench.js +0 -24
- package/index.js +28 -33
- package/package.json +1 -2
- package/test/any.test.js +17 -0
- package/test/defaults.test.js +37 -0
- package/test/typesArray.test.js +70 -0
package/.github/workflows/ci.yml
CHANGED
package/bench.js
CHANGED
|
@@ -58,11 +58,6 @@ const obj = {
|
|
|
58
58
|
|
|
59
59
|
const multiArray = []
|
|
60
60
|
|
|
61
|
-
const JSTR = require('json-strify')
|
|
62
|
-
const JSTRStringify = JSTR(schemaCJS)
|
|
63
|
-
const JSTRArray = JSTR(arraySchemaCJS)
|
|
64
|
-
const JSTRInstance = JSTR()
|
|
65
|
-
|
|
66
61
|
const CJS = require('compile-json-stringify')
|
|
67
62
|
const CJSStringify = CJS(schemaCJS)
|
|
68
63
|
const CJSStringifyArray = CJS(arraySchemaCJS)
|
|
@@ -90,9 +85,6 @@ for (i = 0; i < 1000; i++) {
|
|
|
90
85
|
suite.add('FJS creation', function () {
|
|
91
86
|
FJS(schema)
|
|
92
87
|
})
|
|
93
|
-
suite.add('JSTR creation', function () {
|
|
94
|
-
JSTR(schemaCJS)
|
|
95
|
-
})
|
|
96
88
|
suite.add('CJS creation', function () {
|
|
97
89
|
CJS(schemaCJS)
|
|
98
90
|
})
|
|
@@ -105,10 +97,6 @@ suite.add('fast-json-stringify array', function () {
|
|
|
105
97
|
stringifyArray(multiArray)
|
|
106
98
|
})
|
|
107
99
|
|
|
108
|
-
suite.add('json-strify array', function () {
|
|
109
|
-
JSTRArray(multiArray)
|
|
110
|
-
})
|
|
111
|
-
|
|
112
100
|
suite.add('compile-json-stringify array', function () {
|
|
113
101
|
CJSStringifyArray(multiArray)
|
|
114
102
|
})
|
|
@@ -121,10 +109,6 @@ suite.add('fast-json-stringify long string', function () {
|
|
|
121
109
|
stringifyString(str)
|
|
122
110
|
})
|
|
123
111
|
|
|
124
|
-
suite.add('json-strify long string', function () {
|
|
125
|
-
JSTRInstance(str)
|
|
126
|
-
})
|
|
127
|
-
|
|
128
112
|
suite.add('compile-json-stringify long string', function () {
|
|
129
113
|
CJSStringifyString(str)
|
|
130
114
|
})
|
|
@@ -137,10 +121,6 @@ suite.add('fast-json-stringify short string', function () {
|
|
|
137
121
|
stringifyString('hello world')
|
|
138
122
|
})
|
|
139
123
|
|
|
140
|
-
suite.add('json-strify short string', function () {
|
|
141
|
-
JSTRInstance('hello world')
|
|
142
|
-
})
|
|
143
|
-
|
|
144
124
|
suite.add('compile-json-stringify short string', function () {
|
|
145
125
|
CJSStringifyString('hello world')
|
|
146
126
|
})
|
|
@@ -153,10 +133,6 @@ suite.add('fast-json-stringify obj', function () {
|
|
|
153
133
|
stringify(obj)
|
|
154
134
|
})
|
|
155
135
|
|
|
156
|
-
suite.add('json-strify obj', function () {
|
|
157
|
-
JSTRStringify(obj)
|
|
158
|
-
})
|
|
159
|
-
|
|
160
136
|
suite.add('compile-json-stringify obj', function () {
|
|
161
137
|
CJSStringify(obj)
|
|
162
138
|
})
|
package/index.js
CHANGED
|
@@ -405,14 +405,12 @@ function addPatternProperties (location) {
|
|
|
405
405
|
if (/${regex.replace(/\\*\//g, '\\/')}/.test(keys[i])) {
|
|
406
406
|
`
|
|
407
407
|
if (type === 'object') {
|
|
408
|
-
code += buildObject(ppLocation, '', 'buildObjectPP' + index)
|
|
409
|
-
code += `
|
|
408
|
+
code += `${buildObject(ppLocation, '', 'buildObjectPP' + index)}
|
|
410
409
|
${addComma}
|
|
411
410
|
json += $asString(keys[i]) + ':' + buildObjectPP${index}(obj[keys[i]])
|
|
412
411
|
`
|
|
413
412
|
} else if (type === 'array') {
|
|
414
|
-
code += buildArray(ppLocation, '', 'buildArrayPP' + index)
|
|
415
|
-
code += `
|
|
413
|
+
code += `${buildArray(ppLocation, '', 'buildArrayPP' + index)}
|
|
416
414
|
${addComma}
|
|
417
415
|
json += $asString(keys[i]) + ':' + buildArrayPP${index}(obj[keys[i]])
|
|
418
416
|
`
|
|
@@ -483,14 +481,12 @@ function additionalProperty (location) {
|
|
|
483
481
|
var format = ap.format
|
|
484
482
|
var stringSerializer = getStringSerializer(format)
|
|
485
483
|
if (type === 'object') {
|
|
486
|
-
code += buildObject(apLocation, '', 'buildObjectAP')
|
|
487
|
-
code += `
|
|
484
|
+
code += `${buildObject(apLocation, '', 'buildObjectAP')}
|
|
488
485
|
${addComma}
|
|
489
486
|
json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]])
|
|
490
487
|
`
|
|
491
488
|
} else if (type === 'array') {
|
|
492
|
-
code += buildArray(apLocation, '', 'buildArrayAP')
|
|
493
|
-
code += `
|
|
489
|
+
code += `${buildArray(apLocation, '', 'buildArrayAP')}
|
|
494
490
|
${addComma}
|
|
495
491
|
json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]])
|
|
496
492
|
`
|
|
@@ -759,14 +755,14 @@ function buildCode (location, code, laterCode, name) {
|
|
|
759
755
|
|
|
760
756
|
var defaultValue = schema.properties[key].default
|
|
761
757
|
if (defaultValue !== undefined) {
|
|
762
|
-
required = filterRequired(
|
|
758
|
+
required = filterRequired(required, key)
|
|
763
759
|
code += `
|
|
764
760
|
} else {
|
|
765
761
|
${addComma}
|
|
766
762
|
json += ${asString} + ':' + ${JSON.stringify(JSON.stringify(defaultValue))}
|
|
767
763
|
`
|
|
768
|
-
} else if (
|
|
769
|
-
required = filterRequired(
|
|
764
|
+
} else if (required && required.indexOf(key) !== -1) {
|
|
765
|
+
required = filterRequired(required, key)
|
|
770
766
|
code += `
|
|
771
767
|
} else {
|
|
772
768
|
throw new Error('${sanitized} is required!')
|
|
@@ -792,8 +788,7 @@ function buildCode (location, code, laterCode, name) {
|
|
|
792
788
|
}
|
|
793
789
|
code += `${JSON.stringify(required[i])}`
|
|
794
790
|
}
|
|
795
|
-
code +=
|
|
796
|
-
code += `
|
|
791
|
+
code += `]
|
|
797
792
|
for (var i = 0; i < required.length; i++) {
|
|
798
793
|
if (obj[required[i]] === undefined) throw new Error('"' + required[i] + '" is required!')
|
|
799
794
|
}
|
|
@@ -921,7 +916,6 @@ function buildObject (location, code, name) {
|
|
|
921
916
|
var addComma = false
|
|
922
917
|
`
|
|
923
918
|
|
|
924
|
-
var laterCode = ''
|
|
925
919
|
var r
|
|
926
920
|
if (schema.if && schema.then) {
|
|
927
921
|
code += `
|
|
@@ -932,21 +926,18 @@ function buildObject (location, code, name) {
|
|
|
932
926
|
r = buildInnerObject(location, name)
|
|
933
927
|
}
|
|
934
928
|
|
|
935
|
-
code += r.code
|
|
936
|
-
laterCode = r.laterCode
|
|
937
|
-
|
|
938
929
|
// Removes the comma if is the last element of the string (in case there are not properties)
|
|
939
|
-
code +=
|
|
930
|
+
code += `${r.code}
|
|
940
931
|
json += '}'
|
|
941
932
|
return json
|
|
942
933
|
}
|
|
934
|
+
${r.laterCode}
|
|
943
935
|
`
|
|
944
936
|
|
|
945
|
-
code += laterCode
|
|
946
937
|
return code
|
|
947
938
|
}
|
|
948
939
|
|
|
949
|
-
function buildArray (location, code, name) {
|
|
940
|
+
function buildArray (location, code, name, key = null) {
|
|
950
941
|
var schema = location.schema
|
|
951
942
|
code += `
|
|
952
943
|
function ${name} (obj) {
|
|
@@ -997,6 +988,14 @@ function buildArray (location, code, name) {
|
|
|
997
988
|
result = nested(laterCode, name, '[i]', mergeLocation(location, { schema: schema.items }), undefined, true)
|
|
998
989
|
}
|
|
999
990
|
|
|
991
|
+
if (key) {
|
|
992
|
+
code += `
|
|
993
|
+
if(!Array.isArray(obj)) {
|
|
994
|
+
throw new TypeError(\`Property '${key}' should be of type array, received '$\{obj}' instead.\`)
|
|
995
|
+
}
|
|
996
|
+
`
|
|
997
|
+
}
|
|
998
|
+
|
|
1000
999
|
code += `
|
|
1001
1000
|
var l = obj.length
|
|
1002
1001
|
var w = l - 1
|
|
@@ -1006,18 +1005,12 @@ function buildArray (location, code, name) {
|
|
|
1006
1005
|
}
|
|
1007
1006
|
${result.code}
|
|
1008
1007
|
}
|
|
1008
|
+
json += ']'
|
|
1009
|
+
return json
|
|
1010
|
+
}
|
|
1011
|
+
${result.laterCode}
|
|
1009
1012
|
`
|
|
1010
1013
|
|
|
1011
|
-
laterCode = result.laterCode
|
|
1012
|
-
|
|
1013
|
-
code += `
|
|
1014
|
-
json += ']'
|
|
1015
|
-
return json
|
|
1016
|
-
}
|
|
1017
|
-
`
|
|
1018
|
-
|
|
1019
|
-
code += laterCode
|
|
1020
|
-
|
|
1021
1014
|
return code
|
|
1022
1015
|
}
|
|
1023
1016
|
|
|
@@ -1146,7 +1139,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
|
|
1146
1139
|
break
|
|
1147
1140
|
case 'array':
|
|
1148
1141
|
funcName = asFuncName('$arr' + name + key + subKey) // eslint-disable-line
|
|
1149
|
-
laterCode = buildArray(location, laterCode, funcName)
|
|
1142
|
+
laterCode = buildArray(location, laterCode, funcName, key)
|
|
1150
1143
|
code += `
|
|
1151
1144
|
json += ${funcName}(obj${accessor})
|
|
1152
1145
|
`
|
|
@@ -1208,7 +1201,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
|
|
1208
1201
|
|
|
1209
1202
|
if (type === 'string') {
|
|
1210
1203
|
code += `
|
|
1211
|
-
${index === 0 ? 'if' : 'else if'}(obj${accessor} === null || typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp)
|
|
1204
|
+
${index === 0 ? 'if' : 'else if'}(obj${accessor} === null || typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp || (typeof obj${accessor} === "object" && Object.hasOwnProperty.call(obj${accessor}, "toString")))
|
|
1212
1205
|
${nestedResult.code}
|
|
1213
1206
|
`
|
|
1214
1207
|
} else if (type === 'null') {
|
|
@@ -1255,7 +1248,9 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
|
|
1255
1248
|
|
|
1256
1249
|
function isEmpty (schema) {
|
|
1257
1250
|
for (var key in schema) {
|
|
1258
|
-
if (schema.hasOwnProperty(key))
|
|
1251
|
+
if (schema.hasOwnProperty(key) && schema[key] !== undefined) {
|
|
1252
|
+
return false
|
|
1253
|
+
}
|
|
1259
1254
|
}
|
|
1260
1255
|
return true
|
|
1261
1256
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fast-json-stringify",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.9",
|
|
4
4
|
"description": "Stringify your JSON at max speed",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -33,7 +33,6 @@
|
|
|
33
33
|
"benchmark": "^2.1.4",
|
|
34
34
|
"compile-json-stringify": "^0.1.2",
|
|
35
35
|
"is-my-json-valid": "^2.20.0",
|
|
36
|
-
"json-strify": "^0.1.7",
|
|
37
36
|
"long": "^4.0.0",
|
|
38
37
|
"moment": "^2.24.0",
|
|
39
38
|
"pre-commit": "^1.2.2",
|
package/test/any.test.js
CHANGED
|
@@ -33,6 +33,23 @@ test('object with nested random property', (t) => {
|
|
|
33
33
|
}), '{"id":1,"name":["first","last"]}')
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
+
// reference: https://github.com/fastify/fast-json-stringify/issues/259
|
|
37
|
+
test('object with empty schema with $id: undefined set', (t) => {
|
|
38
|
+
t.plan(1)
|
|
39
|
+
|
|
40
|
+
const schema = {
|
|
41
|
+
title: 'empty schema to allow any object with $id: undefined set',
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
name: { $id: undefined }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const stringify = build(schema)
|
|
48
|
+
t.is(stringify({
|
|
49
|
+
name: 'string'
|
|
50
|
+
}), '{"name":"string"}')
|
|
51
|
+
})
|
|
52
|
+
|
|
36
53
|
test('array with random items', (t) => {
|
|
37
54
|
t.plan(1)
|
|
38
55
|
|
package/test/defaults.test.js
CHANGED
|
@@ -337,3 +337,40 @@ buildTest({
|
|
|
337
337
|
}, {
|
|
338
338
|
level1: { level2: { level3: { level4: { } } } }
|
|
339
339
|
})
|
|
340
|
+
|
|
341
|
+
buildTest({
|
|
342
|
+
type: 'object',
|
|
343
|
+
properties: {
|
|
344
|
+
name: {
|
|
345
|
+
type: 'string',
|
|
346
|
+
default: 'foo'
|
|
347
|
+
},
|
|
348
|
+
dev: {
|
|
349
|
+
type: 'boolean',
|
|
350
|
+
default: false
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
required: [
|
|
354
|
+
'name', 'dev'
|
|
355
|
+
]
|
|
356
|
+
}, {}, { name: 'foo', dev: false })
|
|
357
|
+
|
|
358
|
+
buildTest({
|
|
359
|
+
type: 'object',
|
|
360
|
+
properties: {
|
|
361
|
+
name: {
|
|
362
|
+
type: 'string',
|
|
363
|
+
default: 'foo'
|
|
364
|
+
},
|
|
365
|
+
dev: {
|
|
366
|
+
type: 'boolean'
|
|
367
|
+
},
|
|
368
|
+
job: {
|
|
369
|
+
type: 'string',
|
|
370
|
+
default: 'awesome'
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
required: [
|
|
374
|
+
'name', 'dev'
|
|
375
|
+
]
|
|
376
|
+
}, { dev: true }, { name: 'foo', dev: true, job: 'awesome' })
|
package/test/typesArray.test.js
CHANGED
|
@@ -365,3 +365,73 @@ test('string type array can handle dates', (t) => {
|
|
|
365
365
|
})
|
|
366
366
|
t.is(value, '{"date":"2018-04-20T07:52:31.017Z","dateObject":"2018-04-21T07:52:31.017Z"}')
|
|
367
367
|
})
|
|
368
|
+
|
|
369
|
+
test('object that is simultaneously a string and a json', (t) => {
|
|
370
|
+
t.plan(2)
|
|
371
|
+
const schema = {
|
|
372
|
+
type: 'object',
|
|
373
|
+
properties: {
|
|
374
|
+
simultaneously: {
|
|
375
|
+
type: ['string', 'object'],
|
|
376
|
+
properties: {
|
|
377
|
+
foo: { type: 'string' }
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const likeObjectId = {
|
|
384
|
+
toString () { return 'hello' }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const stringify = build(schema)
|
|
388
|
+
const valueStr = stringify({ simultaneously: likeObjectId })
|
|
389
|
+
t.is(valueStr, '{"simultaneously":"hello"}')
|
|
390
|
+
|
|
391
|
+
const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
|
|
392
|
+
t.is(valueObj, '{"simultaneously":{"foo":"hello"}}')
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
test('object that is simultaneously a string and a json switched', (t) => {
|
|
396
|
+
t.plan(2)
|
|
397
|
+
const schema = {
|
|
398
|
+
type: 'object',
|
|
399
|
+
properties: {
|
|
400
|
+
simultaneously: {
|
|
401
|
+
type: ['object', 'string'],
|
|
402
|
+
properties: {
|
|
403
|
+
foo: { type: 'string' }
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const likeObjectId = {
|
|
410
|
+
toString () { return 'hello' }
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const stringify = build(schema)
|
|
414
|
+
const valueStr = stringify({ simultaneously: likeObjectId })
|
|
415
|
+
t.is(valueStr, '{"simultaneously":{}}')
|
|
416
|
+
|
|
417
|
+
const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
|
|
418
|
+
t.is(valueObj, '{"simultaneously":{"foo":"hello"}}')
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
test('should throw an error when type is array and object is null', (t) => {
|
|
422
|
+
t.plan(1)
|
|
423
|
+
const schema = {
|
|
424
|
+
type: 'object',
|
|
425
|
+
properties: {
|
|
426
|
+
arr: {
|
|
427
|
+
type: 'array',
|
|
428
|
+
items: {
|
|
429
|
+
type: 'number'
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const stringify = build(schema)
|
|
436
|
+
t.throws(() => stringify({ arr: null }), new TypeError('Property \'arr\' should be of type array, received \'null\' instead.'))
|
|
437
|
+
})
|