-
-
Notifications
You must be signed in to change notification settings - Fork 208
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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?
@mcollina Have added the tests. Also updated README with this option details and updated typescript options definition. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Hi, @ivan-tymoshenko , If you can take a look at this PR ? |
* | ||
* @default 'false' | ||
*/ | ||
stripNull?: boolean |
There was a problem hiding this comment.
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
}
Checklist
npm run test
andnpm run benchmark
and the Code of conduct
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:
Example code:
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)