New option added: `stripNull`, which removes nulls in serialized output
Checklist
- [x] run
npm run testandnpm run benchmark - [x] tests and/or benchmarks are included
- [x] documentation is changed or added
- [x] commit message and code follows the Developer's Certification of Origin 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:
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)
@mcollina Have added the tests. Also updated README with this option details and updated typescript options definition.
Hi, @ivan-tymoshenko , If you can take a look at this PR ?