Skip to content

Commit

Permalink
Merge pull request #2696 from dmichon-msft/json-file-undefined
Browse files Browse the repository at this point in the history
[node-core-library] Add `ignoreUndefinedValues` option to JsonFile serialization APIs
  • Loading branch information
iclanton authored May 18, 2021
2 parents cca81b7 + 51e203d commit 3e9aa33
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@rushstack/node-core-library",
"comment": "Add `ignoreUndefinedValues` option to JsonFile to discard keys with undefined values during serialization; this is the standard behavior of `JSON.stringify()` and other JSON serializers.",
"type": "minor"
}
],
"packageName": "@rushstack/node-core-library",
"email": "[email protected]"
}
1 change: 1 addition & 0 deletions common/reviews/api/node-core-library.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ export interface IJsonFileSaveOptions extends IJsonFileStringifyOptions {
// @public
export interface IJsonFileStringifyOptions {
headerComment?: string;
ignoreUndefinedValues?: boolean;
newlineConversion?: NewlineKind;
prettyFormatting?: boolean;
}
Expand Down
11 changes: 10 additions & 1 deletion libraries/node-core-library/src/JsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export interface IJsonFileStringifyOptions {
*/
newlineConversion?: NewlineKind;

/**
* If true, conforms to the standard behavior of JSON.stringify() when a property has the value `undefined`.
* Specifically, the key will be dropped from the emitted object.
*/
ignoreUndefinedValues?: boolean;

/**
* If true, then the "jju" library will be used to improve the text formatting.
* Note that this is slightly slower than the native JSON.stringify() implementation.
Expand Down Expand Up @@ -230,7 +236,10 @@ export class JsonFile {
options = {};
}

JsonFile.validateNoUndefinedMembers(newJsonObject);
if (!options.ignoreUndefinedValues) {
// Standard handling of `undefined` in JSON stringification is to discard the key.
JsonFile.validateNoUndefinedMembers(newJsonObject);
}

let stringified: string;

Expand Down
20 changes: 20 additions & 0 deletions libraries/node-core-library/src/test/JsonFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,24 @@ describe('JsonFile tests', () => {
)
).toMatchSnapshot();
});
it('allows undefined values when asked', () => {
expect(
JsonFile.stringify(
{ abc: undefined },
{
ignoreUndefinedValues: true
}
)
).toMatchSnapshot();

expect(
JsonFile.stringify(
{ abc: undefined },
{
ignoreUndefinedValues: true,
prettyFormatting: true
}
)
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ exports[`JsonFile tests adds an empty header comment 1`] = `
}
"
`;

exports[`JsonFile tests allows undefined values when asked 1`] = `
"{}
"
`;

exports[`JsonFile tests allows undefined values when asked 2`] = `
"{}
"
`;

0 comments on commit 3e9aa33

Please sign in to comment.