fast-json-stringify 2.7.11 → 2.7.12
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/index.js +31 -9
- package/package.json +1 -1
- package/test/recursion.test.js +43 -0
package/index.js
CHANGED
|
@@ -47,10 +47,12 @@ function mergeLocation (source, dest) {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
const
|
|
50
|
+
const arrayItemsReferenceSerializersMap = new Map()
|
|
51
|
+
const objectReferenceSerializersMap = new Map()
|
|
51
52
|
|
|
52
53
|
function build (schema, options) {
|
|
53
|
-
|
|
54
|
+
arrayItemsReferenceSerializersMap.clear()
|
|
55
|
+
objectReferenceSerializersMap.clear()
|
|
54
56
|
options = options || {}
|
|
55
57
|
isValidSchema(schema)
|
|
56
58
|
if (options.schema) {
|
|
@@ -147,7 +149,8 @@ function build (schema, options) {
|
|
|
147
149
|
return dependenciesName
|
|
148
150
|
}
|
|
149
151
|
|
|
150
|
-
|
|
152
|
+
arrayItemsReferenceSerializersMap.clear()
|
|
153
|
+
objectReferenceSerializersMap.clear()
|
|
151
154
|
|
|
152
155
|
return (Function.apply(null, dependenciesName).apply(null, dependencies))
|
|
153
156
|
}
|
|
@@ -399,51 +402,60 @@ function addPatternProperties (location) {
|
|
|
399
402
|
} catch (err) {
|
|
400
403
|
throw new Error(`${err.message}. Found at ${regex} matching ${JSON.stringify(pp[regex])}`)
|
|
401
404
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
+
|
|
406
|
+
const ifPpKeyExists = `if (/${regex.replace(/\\*\//g, '\\/')}/.test(keys[i])) {`
|
|
407
|
+
|
|
405
408
|
if (type === 'object') {
|
|
406
409
|
code += `${buildObject(ppLocation, '', 'buildObjectPP' + index)}
|
|
410
|
+
${ifPpKeyExists}
|
|
407
411
|
${addComma}
|
|
408
412
|
json += $asString(keys[i]) + ':' + buildObjectPP${index}(obj[keys[i]])
|
|
409
413
|
`
|
|
410
414
|
} else if (type === 'array') {
|
|
411
415
|
code += `${buildArray(ppLocation, '', 'buildArrayPP' + index)}
|
|
416
|
+
${ifPpKeyExists}
|
|
412
417
|
${addComma}
|
|
413
418
|
json += $asString(keys[i]) + ':' + buildArrayPP${index}(obj[keys[i]])
|
|
414
419
|
`
|
|
415
420
|
} else if (type === 'null') {
|
|
416
421
|
code += `
|
|
422
|
+
${ifPpKeyExists}
|
|
417
423
|
${addComma}
|
|
418
424
|
json += $asString(keys[i]) +':null'
|
|
419
425
|
`
|
|
420
426
|
} else if (type === 'string') {
|
|
421
427
|
code += `
|
|
428
|
+
${ifPpKeyExists}
|
|
422
429
|
${addComma}
|
|
423
430
|
json += $asString(keys[i]) + ':' + ${stringSerializer}(obj[keys[i]])
|
|
424
431
|
`
|
|
425
432
|
} else if (type === 'integer') {
|
|
426
433
|
code += `
|
|
434
|
+
${ifPpKeyExists}
|
|
427
435
|
${addComma}
|
|
428
436
|
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]])
|
|
429
437
|
`
|
|
430
438
|
} else if (type === 'number') {
|
|
431
439
|
code += `
|
|
440
|
+
${ifPpKeyExists}
|
|
432
441
|
${addComma}
|
|
433
442
|
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]])
|
|
434
443
|
`
|
|
435
444
|
} else if (type === 'boolean') {
|
|
436
445
|
code += `
|
|
446
|
+
${ifPpKeyExists}
|
|
437
447
|
${addComma}
|
|
438
448
|
json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]])
|
|
439
449
|
`
|
|
440
450
|
} else if (type === undefined) {
|
|
441
451
|
code += `
|
|
452
|
+
${ifPpKeyExists}
|
|
442
453
|
${addComma}
|
|
443
454
|
json += $asString(keys[i]) + ':' + $asAny(obj[keys[i]])
|
|
444
455
|
`
|
|
445
456
|
} else {
|
|
446
457
|
code += `
|
|
458
|
+
${ifPpKeyExists}
|
|
447
459
|
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ' + ${JSON.stringify(type)})
|
|
448
460
|
`
|
|
449
461
|
}
|
|
@@ -922,6 +934,16 @@ function buildObject (location, code, name) {
|
|
|
922
934
|
}
|
|
923
935
|
`
|
|
924
936
|
}
|
|
937
|
+
|
|
938
|
+
if (objectReferenceSerializersMap.has(schema)) {
|
|
939
|
+
code += `
|
|
940
|
+
return ${objectReferenceSerializersMap.get(schema)}(input)
|
|
941
|
+
}
|
|
942
|
+
`
|
|
943
|
+
return code
|
|
944
|
+
}
|
|
945
|
+
objectReferenceSerializersMap.set(schema, name)
|
|
946
|
+
|
|
925
947
|
code += `
|
|
926
948
|
var obj = ${toJSON('input')}
|
|
927
949
|
var json = '{'
|
|
@@ -978,14 +1000,14 @@ function buildArray (location, code, name, key = null) {
|
|
|
978
1000
|
location = refFinder(schema.items.$ref, location)
|
|
979
1001
|
schema.items = location.schema
|
|
980
1002
|
|
|
981
|
-
if (
|
|
1003
|
+
if (arrayItemsReferenceSerializersMap.has(schema.items)) {
|
|
982
1004
|
code += `
|
|
983
|
-
return ${
|
|
1005
|
+
return ${arrayItemsReferenceSerializersMap.get(schema.items)}(obj)
|
|
984
1006
|
}
|
|
985
1007
|
`
|
|
986
1008
|
return code
|
|
987
1009
|
}
|
|
988
|
-
|
|
1010
|
+
arrayItemsReferenceSerializersMap.set(schema.items, name)
|
|
989
1011
|
}
|
|
990
1012
|
|
|
991
1013
|
let result = { code: '', laterCode: '' }
|
package/package.json
CHANGED
package/test/recursion.test.js
CHANGED
|
@@ -135,3 +135,46 @@ test('use proper serialize function', t => {
|
|
|
135
135
|
})
|
|
136
136
|
t.equal(value, '{"people":{"name":"Elizabeth","children":[{"name":"Charles","children":[{"name":"William","children":[{"name":"George"},{"name":"Charlotte"}]},{"name":"Harry"}]}]},"directory":{"name":"directory 1","subDirectories":[{"name":"directory 1.1","subDirectories":[]},{"name":"directory 1.2","subDirectories":[{"name":"directory 1.2.1","subDirectories":[]},{"name":"directory 1.2.2","subDirectories":[]}]}]}}')
|
|
137
137
|
})
|
|
138
|
+
|
|
139
|
+
test('can stringify recursive references in object types (issue #365)', t => {
|
|
140
|
+
t.plan(1)
|
|
141
|
+
|
|
142
|
+
const schema = {
|
|
143
|
+
type: 'object',
|
|
144
|
+
definitions: {
|
|
145
|
+
parentCategory: {
|
|
146
|
+
type: 'object',
|
|
147
|
+
properties: {
|
|
148
|
+
parent: {
|
|
149
|
+
$ref: '#/definitions/parentCategory'
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
properties: {
|
|
155
|
+
category: {
|
|
156
|
+
type: 'object',
|
|
157
|
+
properties: {
|
|
158
|
+
parent: {
|
|
159
|
+
$ref: '#/definitions/parentCategory'
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const stringify = build(schema)
|
|
167
|
+
const data = {
|
|
168
|
+
category: {
|
|
169
|
+
parent: {
|
|
170
|
+
parent: {
|
|
171
|
+
parent: {
|
|
172
|
+
parent: {}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const value = stringify(data)
|
|
179
|
+
t.equal(value, '{"category":{"parent":{"parent":{"parent":{"parent":{}}}}}}')
|
|
180
|
+
})
|