fast-json-stringify
Advanced tools
Comparing version 0.17.0 to 1.0.0
83
index.js
'use strict' | ||
var fastSafeStringify = require('fast-safe-stringify') | ||
var Ajv = require('ajv') | ||
@@ -94,6 +93,2 @@ | ||
var dependenciesName = [] | ||
if (hasAdditionalPropertiesTrue(schema)) { | ||
dependencies.push(fastSafeStringify) | ||
dependenciesName.push('fastSafeStringify') | ||
} | ||
if (hasAnyOf(schema)) { | ||
@@ -108,16 +103,2 @@ dependencies.push(new Ajv()) | ||
function hasAdditionalPropertiesTrue (schema) { | ||
if (schema.additionalProperties === true) { return true } | ||
var objectKeys = Object.keys(schema) | ||
for (var i = 0; i < objectKeys.length; i++) { | ||
var value = schema[objectKeys[i]] | ||
if (typeof value === 'object') { | ||
if (hasAdditionalPropertiesTrue(value)) { return true } | ||
} | ||
} | ||
return false | ||
} | ||
function hasAnyOf (schema) { | ||
@@ -285,3 +266,3 @@ if ('anyOf' in schema) { return true } | ||
${addComma} | ||
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) | ||
json += $asString(keys[i]) + ':' + JSON.stringify(obj[keys[i]]) | ||
} | ||
@@ -320,8 +301,14 @@ ` | ||
${addComma} | ||
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) | ||
var t = Number(obj[keys[i]]) | ||
if (isLong && isLong(obj[keys[i]]) || !isNaN(t)) { | ||
json += $asString(keys[i]) + ':' + $asInteger(obj[keys[i]]) | ||
} | ||
` | ||
} else if (type === 'number') { | ||
code += ` | ||
${addComma} | ||
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) | ||
var t = Number(obj[keys[i]]) | ||
if (!isNaN(t)) { | ||
${addComma} | ||
json += $asString(keys[i]) + ':' + t | ||
} | ||
` | ||
@@ -371,19 +358,47 @@ } else if (type === 'boolean') { | ||
Object.keys(schema.properties || {}).forEach((key, i, a) => { | ||
if (schema.properties[key]['$ref']) { | ||
schema.properties[key] = refFinder(schema.properties[key]['$ref'], fullSchema, externalSchema) | ||
} | ||
// Using obj['key'] !== undefined instead of obj.hasOwnProperty(prop) for perf reasons, | ||
// see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion. | ||
code += ` | ||
if (obj['${key}'] !== undefined) { | ||
${addComma} | ||
json += '${$asString(key)}:' | ||
var type = schema.properties[key].type | ||
if (type === 'number') { | ||
code += ` | ||
var t = Number(obj['${key}']) | ||
if (!isNaN(t)) { | ||
${addComma} | ||
json += '${$asString(key)}:' + t | ||
` | ||
} else if (type === 'integer') { | ||
code += ` | ||
var rendered = false | ||
if (isLong && isLong(obj['${key}'])) { | ||
${addComma} | ||
json += '${$asString(key)}:' + obj['${key}'].toString() | ||
rendered = true | ||
} else { | ||
var t = Number(obj['${key}']) | ||
if (!isNaN(t)) { | ||
${addComma} | ||
json += '${$asString(key)}:' + t | ||
rendered = true | ||
} | ||
} | ||
if (schema.properties[key]['$ref']) { | ||
schema.properties[key] = refFinder(schema.properties[key]['$ref'], fullSchema, externalSchema) | ||
if (rendered) { | ||
` | ||
} else { | ||
code += ` | ||
if (obj['${key}'] !== undefined) { | ||
${addComma} | ||
json += '${$asString(key)}:' | ||
` | ||
var result = nested(laterCode, name, key, schema.properties[key], externalSchema, fullSchema) | ||
code += result.code | ||
laterCode = result.laterCode | ||
} | ||
var result = nested(laterCode, name, key, schema.properties[key], externalSchema, fullSchema) | ||
code += result.code | ||
laterCode = result.laterCode | ||
if (schema.required && schema.required.indexOf(key) !== -1) { | ||
@@ -390,0 +405,0 @@ code += ` |
{ | ||
"name": "fast-json-stringify", | ||
"version": "0.17.0", | ||
"version": "1.0.0", | ||
"description": "Stringify your JSON at max speed", | ||
@@ -37,5 +37,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"ajv": "^6.0.0", | ||
"fast-safe-stringify": "^1.2.1" | ||
"ajv": "^6.0.0" | ||
} | ||
} |
@@ -206,3 +206,3 @@ # fast-json-stringify [![Build Status](https://travis-ci.org/fastify/fast-json-stringify.svg?branch=master)](https://travis-ci.org/fastify/fast-json-stringify) | ||
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>. | ||
If *additionalProperties* is set to `true`, it will be used by `fast-safe-stringify` to stringify the additional properties. If you want to achieve maximum performance, we strongly encourage you to use a fixed schema where possible. | ||
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. | ||
Example: | ||
@@ -209,0 +209,0 @@ ```javascript |
@@ -96,3 +96,3 @@ 'use strict' | ||
test('additionalProperties - number coerce', (t) => { | ||
test('additionalProperties - number skip', (t) => { | ||
t.plan(1) | ||
@@ -109,3 +109,3 @@ const stringify = build({ | ||
const obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } } | ||
t.equal('{"foo":1,"ofoo":42,"xfoo":null,"arrfoo":null,"objfoo":null}', stringify(obj)) | ||
t.equal(stringify(obj), '{"foo":1,"ofoo":42}') | ||
}) | ||
@@ -125,3 +125,3 @@ | ||
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } } | ||
t.equal('{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}', stringify(obj)) | ||
t.equal(stringify(obj), '{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}') | ||
}) | ||
@@ -128,0 +128,0 @@ |
@@ -249,2 +249,32 @@ 'use strict' | ||
test('skip or coerce numbers and integers that are not numbers', (t) => { | ||
const stringify = build({ | ||
'title': 'basic', | ||
'type': 'object', | ||
'properties': { | ||
'age': { | ||
'type': 'number' | ||
}, | ||
'distance': { | ||
'type': 'integer' | ||
} | ||
} | ||
}) | ||
var result = stringify({ | ||
age: 'hello ', | ||
distance: 'long' | ||
}) | ||
t.deepEqual(JSON.parse(result), {}) | ||
result = stringify({ | ||
age: '42', | ||
distance: true | ||
}) | ||
t.deepEqual(JSON.parse(result), { age: 42, distance: 1 }) | ||
t.end() | ||
}) | ||
test('Should throw on invalid schema', t => { | ||
@@ -251,0 +281,0 @@ t.plan(1) |
@@ -47,3 +47,3 @@ 'use strict' | ||
test(`render aan array with long as JSON`, (t) => { | ||
test(`render an array with long as JSON`, (t) => { | ||
t.plan(2) | ||
@@ -66,1 +66,22 @@ | ||
}) | ||
test(`render an object with a long additionalProperty as JSON`, (t) => { | ||
t.plan(2) | ||
const schema = { | ||
title: 'object with long', | ||
type: 'object', | ||
additionalProperties: { | ||
type: 'integer' | ||
} | ||
} | ||
const validate = validator(schema) | ||
const stringify = build(schema) | ||
const output = stringify({ | ||
num: Long.fromString('18446744073709551615', true) | ||
}) | ||
t.equal(output, '{"num":18446744073709551615}') | ||
t.ok(validate(JSON.parse(output)), 'valid schema') | ||
}) |
@@ -43,1 +43,39 @@ 'use strict' | ||
}) | ||
test('required numbers', (t) => { | ||
t.plan(3) | ||
const schema = { | ||
title: 'object with required field', | ||
type: 'object', | ||
properties: { | ||
str: { | ||
type: 'string' | ||
}, | ||
num: { | ||
type: 'integer' | ||
} | ||
}, | ||
required: ['num'] | ||
} | ||
const stringify = build(schema) | ||
try { | ||
stringify({ | ||
num: 42 | ||
}) | ||
t.pass() | ||
} catch (e) { | ||
t.fail() | ||
} | ||
try { | ||
stringify({ | ||
num: 'aaa' | ||
}) | ||
t.fail() | ||
} catch (e) { | ||
t.is(e.message, 'num is required!') | ||
t.pass() | ||
} | ||
}) |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
65340
1
2309
0
- Removedfast-safe-stringify@^1.2.1
- Removedfast-safe-stringify@1.2.3(transitive)