New option added: stripNull, which removes nulls in serialized output#731
Open
shivamkj wants to merge 3 commits intofastify:mainfrom
Open
New option added: stripNull, which removes nulls in serialized output#731shivamkj wants to merge 3 commits intofastify:mainfrom
stripNull, which removes nulls in serialized output#731shivamkj wants to merge 3 commits intofastify:mainfrom
Conversation
mcollina
requested changes
Jul 26, 2024
Member
mcollina
left a comment
There was a problem hiding this comment.
Thanks for opening a PR! Can you please add a unit test?
Author
|
@mcollina Have added the tests. Also updated README with this option details and updated typescript options definition. |
Author
|
Hi, @ivan-tymoshenko , If you can take a look at this PR ? |
climba03003
reviewed
Aug 16, 2024
| * | ||
| * @default 'false' | ||
| */ | ||
| stripNull?: boolean |
Member
There was a problem hiding this comment.
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
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist
npm run testandnpm run benchmarkand 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)