Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option added: stripNull, which removes nulls in serialized output #731

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ const stringify = fastJson(mySchema, {
- `rounding`: setup how the `integer` types will be rounded when not integers. [More details](#integer)
- `largeArrayMechanism`: set the mechanism that should be used to handle large
(by default `20000` or more items) arrays. [More details](#largearrays)
- `stripNull`: removes keys with null values from serialised output. If you have lots of null values, using this will result in smallerpayload and improved performance.


<a name="api"></a>
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ function buildInnerObject (context, location) {

code += `
value = obj[${sanitizedKey}]
if (value !== undefined) {
if ${context.options.stripNull ? '(value != null)' : '(value !== undefined)'} {
${addComma}
json += ${JSON.stringify(sanitizedKey + ':')}
${buildValue(context, propertyLocation, 'value')}
Expand Down
110 changes: 110 additions & 0 deletions test/stripNull.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const test = require('tap').test
const build = require('..')

test('should handle null string values properly', (t) => {
t.plan(3)

const schema = {
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' }
},
required: ['firstName']
}
const stringify = build(schema, { stripNull: true })

const json1 = { firstName: 'Matteo', lastName: 'Collina' }
t.equal(stringify(json1), '{"firstName":"Matteo","lastName":"Collina"}')

const json2 = { firstName: 'Matteo', lastName: null }
t.equal(stringify(json2), '{"firstName":"Matteo"}')

const json3 = { firstName: null, lastName: 'Collina' }
t.throws(() => stringify(json3)) // throw error when required property is missing
})

test('should handle null int values properly', (t) => {
t.plan(3)

const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer' },
experience: { type: 'integer' }
}
}
const stringify = build(schema, { stripNull: true })

const json1 = { name: 'Matteo', age: 32, experience: 12 }
t.equal(stringify(json1), '{"name":"Matteo","age":32,"experience":12}')

const json2 = { name: 'Matteo', age: null, experience: null }
t.equal(stringify(json2), '{"name":"Matteo"}')

const json3 = { name: 'Matteo', age: 3, experience: null }
t.equal(stringify(json3), '{"name":"Matteo","age":3}')
})

test('should handle arrays properly', (t) => {
t.plan(1)

const schema = {
type: 'object',
properties: {
name: { type: 'string' },
experience: {
type: 'array',
items: {
type: 'object',
properties: {
company: { type: 'string' },
years: { type: 'integer' }
}
}
}
}
}
const stringify = build(schema, { stripNull: true })

const json1 = {
name: 'Matteo',
experience: [
{ company: 'C1', years: 12 },
{ company: 'C2', years: null },
{ company: null, years: 6 },
{ company: null, years: null }
]
}

t.equal(
stringify(json1),
'{"name":"Matteo","experience":[{"company":"C1","years":12},{"company":"C2"},{"years":6},{}]}'
)
})

test('should handle objects and nested objects properly', (t) => {
t.plan(2)

const schema = {
type: 'object',
properties: {
name: { type: 'string' },
address: {
type: 'object',
properties: {
city: { type: 'string' },
country: { type: 'string' }
}
}
}
}
const stringify = build(schema, { stripNull: true })

const json1 = { name: 'Matteo', address: { city: 'Mumbai', country: null } }
t.equal(stringify(json1), '{"name":"Matteo","address":{"city":"Mumbai"}}')

const json2 = { name: null, address: { city: null, country: 'India' } }
t.equal(stringify(json2), '{"address":{"country":"India"}}')
})
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ declare namespace build {
* @default 'default'
*/
largeArrayMechanism?: 'default' | 'json-stringify'

/**
* Specify if null key-value are removed from serialised JSON output, for a smaller payload
*
* @default 'false'
*/
stripNull?: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be better to do in ignoreValue: ('undefined' | 'null')[]?
Then provide a function to build the ignore statement.

function buildIgnoreValue(ignores) {
  let str = ''
  for(let i = 0; i < ignores.length; i++) {
    str += `value !== ${ignore}`
    if (i < ignores.length - 1) str += ' || '
  }
  return str
}

}

export const validLargeArrayMechanisms: string[]
Expand Down