Socket
Socket
Sign inDemoInstall

fast-json-stringify

Package Overview
Dependencies
Maintainers
2
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-json-stringify - npm Package Compare versions

Comparing version 1.4.0 to 1.5.1

test/if-then-else.test.js

17

bench.js

@@ -38,8 +38,9 @@ 'use strict'

const stringify = require('.')(schema)
const stringifyUgly = require('.')(schema, { uglify: true })
const stringifyArray = require('.')(arraySchema)
const stringifyArrayUgly = require('.')(arraySchema, { uglify: true })
const stringifyString = require('.')({ type: 'string' })
const stringifyStringUgly = require('.')({ type: 'string', uglify: true })
const FJS = require('.')
const stringify = FJS(schema)
const stringifyUgly = FJS(schema, { uglify: true })
const stringifyArray = FJS(arraySchema)
const stringifyArrayUgly = FJS(arraySchema, { uglify: true })
const stringifyString = FJS({ type: 'string' })
const stringifyStringUgly = FJS({ type: 'string', uglify: true })
var str = ''

@@ -60,2 +61,6 @@

suite.add('FJS creation', function () {
FJS(schema)
})
suite.add('JSON.stringify array', function () {

@@ -62,0 +67,0 @@ JSON.stringify(multiArray)

'use strict'
var Ajv = require('ajv')
var merge = require('deepmerge')
// This Ajv instance is used to validate that the passed schema
// is valid json schema. We reuse the instance to avoid having to
// pay the ajv creation cost more than once.
var ajv = new Ajv()
var uglify = null

@@ -89,3 +95,3 @@ var isLong

var dependenciesName = []
if (hasAnyOf(schema) || hasArrayOfTypes(schema)) {
if (hasAnyOf(schema) || hasArrayOfTypes(schema) || hasIf(schema)) {
dependencies.push(new Ajv())

@@ -147,2 +153,7 @@ dependenciesName.push('ajv')

function hasIf (schema) {
const str = JSON.stringify(schema)
return /"if":{/.test(str) && /"then":{/.test(str)
}
function $asNull () {

@@ -480,9 +491,5 @@ return 'null'

function buildObject (schema, code, name, externalSchema, fullSchema) {
code += `
function ${name} (obj) {
var json = '{'
var addComma = false
`
function buildInnerObject (schema, name, externalSchema, fullSchema) {
var laterCode = ''
var code = ''
if (schema.patternProperties) {

@@ -494,4 +501,2 @@ code += addPatternProperties(schema, externalSchema, fullSchema)

var laterCode = ''
if (schema.allOf) {

@@ -511,2 +516,56 @@ schema.allOf.forEach((ss) => {

return { code: code, laterCode: laterCode }
}
function buildObject (schema, code, name, externalSchema, fullSchema) {
code += `
function ${name} (obj) {
var json = '{'
var addComma = false
`
var laterCode = ''
var r, merged
if (schema.if && schema.then) {
merged = merge(schema, schema.then)
delete merged.if
delete merged.then
delete merged.else
code += `
var valid = ajv.validate(${require('util').inspect(schema.if, {depth: null})}, obj)
if (valid) {
`
r = buildInnerObject(merged, name, externalSchema, fullSchema)
code += r.code
laterCode = r.laterCode
code += `
}
`
if (schema.else) {
merged = merge(schema, schema.else)
delete merged.if
delete merged.then
delete merged.else
code += `
else {
`
r = buildInnerObject(merged, name, externalSchema, fullSchema)
code += r.code
laterCode = r.laterCode
code += `
}
`
}
} else {
r = buildInnerObject(schema, name, externalSchema, fullSchema)
code += r.code
laterCode = r.laterCode
}
// Removes the comma if is the last element of the string (in case there are not properties)

@@ -745,3 +804,2 @@ code += `

function isValidSchema (schema, externalSchema) {
const ajv = new Ajv()
if (externalSchema) {

@@ -753,4 +811,9 @@ Object.keys(externalSchema).forEach(key => {

ajv.compile(schema)
if (externalSchema) {
Object.keys(externalSchema).forEach(key => {
ajv.removeSchema(key)
})
}
}
module.exports = build
{
"name": "fast-json-stringify",
"version": "1.4.0",
"version": "1.5.1",
"description": "Stringify your JSON at max speed",

@@ -38,4 +38,5 @@ "main": "index.js",

"dependencies": {
"ajv": "^6.4.0"
"ajv": "^6.4.0",
"deepmerge": "^2.1.0"
}
}

@@ -289,4 +289,48 @@ # fast-json-stringify  [![Build Status](https://travis-ci.org/fastify/fast-json-stringify.svg?branch=master)](https://travis-ci.org/fastify/fast-json-stringify)

This schema will accept a string or a boolean for the property `undecidedType`. If no schema matches the data, it will be stringified as `null`.
<a name="if-then-else"></a>
#### If/then/else
`fast-json-stringify` supports `if/then/else` jsonschema feature. See [ajv documentation](https://ajv.js.org/keywords.html#ifthenelse).
Example:
```javascript
const stringify = fastJson({
'type': 'object',
'properties': {
'kind': { 'type': 'string', 'enum': ['foobar', 'greeting'] }
},
'if': {
'properties': {
'kind': { 'type': 'string', 'enum': ['foobar'] }
}
},
'then': {
'properties': {
'foo': { 'type': 'string' },
'bar': { 'type': 'number' }
}
},
'else': {
'properties': {
'hi': { 'type': 'string' },
'hello': { 'type': 'number' }
}
}
})
console.log(stringify({
kind: 'greeting',
foo: 'FOO',
bar: 42,
hi: 'HI',
hello: 45
})) // {"kind":"greeting","hi":"HI","hello":45}
console.log(stringify({
kind: 'foobar',
foo: 'FOO',
bar: 42,
hi: 'HI',
hello: 45
})) // {"kind":"greeting","foo":"FOO","bar":42}
```
<a name="ref"></a>

@@ -293,0 +337,0 @@ #### Reuse - $ref

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc