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

Conversation

shivamkj
Copy link

@shivamkj shivamkj commented Jul 26, 2024

Checklist

Added the option to remove keys with null value, this makes serialized output significantly small, if you have lots of null in the schema. By default, it will work as expected, as it is working now. This behavior to remove unnecessary null keys from serialized output can be configured via options, if someone needs it.

Example to configure for removing unnecessary nulls:

const fastJson = require('fast-json-stringify')
const stringify = fastJson(mySchema, { stripNull: true })

Example code:

const fastJson = require('fast-json-stringify')
const stringify = fastJson({
 title: "Example Schema",
 type: "object",
 properties: {
   firstName: { type: "string" },
   lastName: { type: "string" },
   age: { description: "Age in years", type: "integer" },
   reg: { type: "string" },
 },
}, { stripNull: true });

console.log(
 stringify({
   firstName: "Matteo",
   lastName: null,
   age: null,
   reg: null,
 })
);

// Default Output: {"firstName":"Matteo","lastName":"","age":0,"reg":""}
// Output with stripNull enabled: {"firstName":"Matteo"}

Passes all the Tests

Benchmarks

FJS creation x 20,318 ops/sec ±0.49% (100 runs sampled)
CJS creation x 259,293 ops/sec ±3.12% (95 runs sampled)
AJV Serialize creation x 120,278,763 ops/sec ±2.47% (85 runs sampled)
JSON.stringify array x 13,608 ops/sec ±0.13% (99 runs sampled)
fast-json-stringify array default x 13,954 ops/sec ±0.17% (101 runs sampled)
fast-json-stringify array json-stringify x 14,091 ops/sec ±0.12% (101 runs sampled)
compile-json-stringify array x 13,019 ops/sec ±0.25% (96 runs sampled)
AJV Serialize array x 12,963 ops/sec ±0.29% (96 runs sampled)
JSON.stringify large array x 427 ops/sec ±1.88% (74 runs sampled)
fast-json-stringify large array default x 406 ops/sec ±0.15% (95 runs sampled)
fast-json-stringify large array json-stringify x 538 ops/sec ±2.54% (95 runs sampled)
compile-json-stringify large array x 598 ops/sec ±0.26% (91 runs sampled)
AJV Serialize large array x 204 ops/sec ±0.20% (88 runs sampled)
JSON.stringify long string x 14,807 ops/sec ±0.15% (98 runs sampled)
fast-json-stringify long string x 14,871 ops/sec ±0.10% (97 runs sampled)
compile-json-stringify long string x 14,867 ops/sec ±0.13% (98 runs sampled)
AJV Serialize long string x 33,026 ops/sec ±0.29% (99 runs sampled)
JSON.stringify short string x 19,658,361 ops/sec ±0.58% (98 runs sampled)
fast-json-stringify short string x 69,757,320 ops/sec ±1.50% (93 runs sampled)
compile-json-stringify short string x 44,706,903 ops/sec ±1.58% (87 runs sampled)
AJV Serialize short string x 33,010,212 ops/sec ±0.66% (94 runs sampled)
JSON.stringify obj x 7,650,782 ops/sec ±0.30% (98 runs sampled)
fast-json-stringify obj x 15,008,195 ops/sec ±0.48% (99 runs sampled)
compile-json-stringify obj x 30,258,985 ops/sec ±0.69% (95 runs sampled)
AJV Serialize obj x 16,646,933 ops/sec ±0.43% (96 runs sampled)
JSON stringify date x 1,267,891 ops/sec ±1.23% (98 runs sampled)
fast-json-stringify date format x 2,274,942 ops/sec ±0.21% (101 runs sampled)
compile-json-stringify date format x 1,279,989 ops/sec ±0.10% (100 runs sampled)

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Thanks for opening a PR! Can you please add a unit test?

@shivamkj
Copy link
Author

shivamkj commented Jul 27, 2024

@mcollina Have added the tests. Also updated README with this option details and updated typescript options definition.

Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@shivamkj
Copy link
Author

Hi, @ivan-tymoshenko , If you can take a look at this PR ?

*
* @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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants