Skip to content

Commit 3e9aa33

Browse files
authored
Merge pull request #2696 from dmichon-msft/json-file-undefined
[node-core-library] Add `ignoreUndefinedValues` option to JsonFile serialization APIs
2 parents cca81b7 + 51e203d commit 3e9aa33

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/node-core-library",
5+
"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.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/node-core-library",
10+
"email": "[email protected]"
11+
}

common/reviews/api/node-core-library.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ export interface IJsonFileSaveOptions extends IJsonFileStringifyOptions {
425425
// @public
426426
export interface IJsonFileStringifyOptions {
427427
headerComment?: string;
428+
ignoreUndefinedValues?: boolean;
428429
newlineConversion?: NewlineKind;
429430
prettyFormatting?: boolean;
430431
}

libraries/node-core-library/src/JsonFile.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export interface IJsonFileStringifyOptions {
4949
*/
5050
newlineConversion?: NewlineKind;
5151

52+
/**
53+
* If true, conforms to the standard behavior of JSON.stringify() when a property has the value `undefined`.
54+
* Specifically, the key will be dropped from the emitted object.
55+
*/
56+
ignoreUndefinedValues?: boolean;
57+
5258
/**
5359
* If true, then the "jju" library will be used to improve the text formatting.
5460
* Note that this is slightly slower than the native JSON.stringify() implementation.
@@ -230,7 +236,10 @@ export class JsonFile {
230236
options = {};
231237
}
232238

233-
JsonFile.validateNoUndefinedMembers(newJsonObject);
239+
if (!options.ignoreUndefinedValues) {
240+
// Standard handling of `undefined` in JSON stringification is to discard the key.
241+
JsonFile.validateNoUndefinedMembers(newJsonObject);
242+
}
234243

235244
let stringified: string;
236245

libraries/node-core-library/src/test/JsonFile.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,24 @@ describe('JsonFile tests', () => {
2727
)
2828
).toMatchSnapshot();
2929
});
30+
it('allows undefined values when asked', () => {
31+
expect(
32+
JsonFile.stringify(
33+
{ abc: undefined },
34+
{
35+
ignoreUndefinedValues: true
36+
}
37+
)
38+
).toMatchSnapshot();
39+
40+
expect(
41+
JsonFile.stringify(
42+
{ abc: undefined },
43+
{
44+
ignoreUndefinedValues: true,
45+
prettyFormatting: true
46+
}
47+
)
48+
).toMatchSnapshot();
49+
});
3050
});

libraries/node-core-library/src/test/__snapshots__/JsonFile.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,13 @@ exports[`JsonFile tests adds an empty header comment 1`] = `
1515
}
1616
"
1717
`;
18+
19+
exports[`JsonFile tests allows undefined values when asked 1`] = `
20+
"{}
21+
"
22+
`;
23+
24+
exports[`JsonFile tests allows undefined values when asked 2`] = `
25+
"{}
26+
"
27+
`;

0 commit comments

Comments
 (0)