fast-json-stringify
Advanced tools
Comparing version 2.7.11 to 2.7.12
40
index.js
@@ -50,6 +50,8 @@ 'use strict' | ||
const referenceSerializersMap = new Map() | ||
const arrayItemsReferenceSerializersMap = new Map() | ||
const objectReferenceSerializersMap = new Map() | ||
function build (schema, options) { | ||
referenceSerializersMap.clear() | ||
arrayItemsReferenceSerializersMap.clear() | ||
objectReferenceSerializersMap.clear() | ||
options = options || {} | ||
@@ -151,3 +153,4 @@ isValidSchema(schema) | ||
referenceSerializersMap.clear() | ||
arrayItemsReferenceSerializersMap.clear() | ||
objectReferenceSerializersMap.clear() | ||
@@ -404,7 +407,8 @@ return (Function.apply(null, dependenciesName).apply(null, dependencies)) | ||
} | ||
code += ` | ||
if (/${regex.replace(/\\*\//g, '\\/')}/.test(keys[i])) { | ||
` | ||
const ifPpKeyExists = `if (/${regex.replace(/\\*\//g, '\\/')}/.test(keys[i])) {` | ||
if (type === 'object') { | ||
code += `${buildObject(ppLocation, '', 'buildObjectPP' + index)} | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -415,2 +419,3 @@ json += $asString(keys[i]) + ':' + buildObjectPP${index}(obj[keys[i]]) | ||
code += `${buildArray(ppLocation, '', 'buildArrayPP' + index)} | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -421,2 +426,3 @@ json += $asString(keys[i]) + ':' + buildArrayPP${index}(obj[keys[i]]) | ||
code += ` | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -427,2 +433,3 @@ json += $asString(keys[i]) +':null' | ||
code += ` | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -433,2 +440,3 @@ json += $asString(keys[i]) + ':' + ${stringSerializer}(obj[keys[i]]) | ||
code += ` | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -439,2 +447,3 @@ json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) | ||
code += ` | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -445,2 +454,3 @@ json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) | ||
code += ` | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -451,2 +461,3 @@ json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]]) | ||
code += ` | ||
${ifPpKeyExists} | ||
${addComma} | ||
@@ -457,2 +468,3 @@ json += $asString(keys[i]) + ':' + $asAny(obj[keys[i]]) | ||
code += ` | ||
${ifPpKeyExists} | ||
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ' + ${JSON.stringify(type)}) | ||
@@ -936,2 +948,12 @@ ` | ||
} | ||
if (objectReferenceSerializersMap.has(schema)) { | ||
code += ` | ||
return ${objectReferenceSerializersMap.get(schema)}(input) | ||
} | ||
` | ||
return code | ||
} | ||
objectReferenceSerializersMap.set(schema, name) | ||
code += ` | ||
@@ -993,5 +1015,5 @@ var obj = ${toJSON('input')} | ||
if (referenceSerializersMap.has(schema.items)) { | ||
if (arrayItemsReferenceSerializersMap.has(schema.items)) { | ||
code += ` | ||
return ${referenceSerializersMap.get(schema.items)}(obj) | ||
return ${arrayItemsReferenceSerializersMap.get(schema.items)}(obj) | ||
} | ||
@@ -1001,3 +1023,3 @@ ` | ||
} | ||
referenceSerializersMap.set(schema.items, name) | ||
arrayItemsReferenceSerializersMap.set(schema.items, name) | ||
} | ||
@@ -1004,0 +1026,0 @@ |
{ | ||
"name": "fast-json-stringify", | ||
"version": "2.7.11", | ||
"version": "2.7.12", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -138,1 +138,44 @@ 'use strict' | ||
}) | ||
test('can stringify recursive references in object types (issue #365)', t => { | ||
t.plan(1) | ||
const schema = { | ||
type: 'object', | ||
definitions: { | ||
parentCategory: { | ||
type: 'object', | ||
properties: { | ||
parent: { | ||
$ref: '#/definitions/parentCategory' | ||
} | ||
} | ||
} | ||
}, | ||
properties: { | ||
category: { | ||
type: 'object', | ||
properties: { | ||
parent: { | ||
$ref: '#/definitions/parentCategory' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const stringify = build(schema) | ||
const data = { | ||
category: { | ||
parent: { | ||
parent: { | ||
parent: { | ||
parent: {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
const value = stringify(data) | ||
t.equal(value, '{"category":{"parent":{"parent":{"parent":{"parent":{}}}}}}') | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
346099
10431