From 79040855c3a861d6ecdd5dcf10b5d299edf13a87 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Tue, 26 Jan 2021 10:24:46 -0800 Subject: [PATCH 1/4] Restore section that was accidentally deleted --- .../system-text-json-ignore-properties.md | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/standard/serialization/system-text-json-ignore-properties.md b/docs/standard/serialization/system-text-json-ignore-properties.md index f785cac3433c6..6d0e033a4e9ca 100644 --- a/docs/standard/serialization/system-text-json-ignore-properties.md +++ b/docs/standard/serialization/system-text-json-ignore-properties.md @@ -1,7 +1,7 @@ --- title: How to ignore properties with System.Text.Json description: "Learn how to ignore properties when serializing with System.Text.Json in .NET." -ms.date: 11/30/2020 +ms.date: 01/26/2021 no-loc: [System.Text.Json, Newtonsoft.Json] zone_pivot_groups: dotnet-version helpviewer_keywords: @@ -127,6 +127,34 @@ The There is no built-in way to prevent serialization of properties with value type defaults in `System.Text.Json` in .NET Core 3.1. ::: zone-end +## Ignore null when deserializing + +By default, if a property in JSON is null, the corresponding property in the target object is set to null. In some scenarios, the target property might have a default value, and you don't want a null value to override the default. + +For example, suppose the following code represents your target object: + +[!code-csharp[](snippets/system-text-json-how-to/csharp/WeatherForecast.cs?name=SnippetWFWithDefault)] + +And suppose the following JSON is deserialized: + +```json +{ + "Date": "2019-08-01T00:00:00-07:00", + "TemperatureCelsius": 25, + "Summary": null +} +``` + +After deserialization, the `Summary` property of the `WeatherForecastWithDefault` object is null. + +To change this behavior, set to `true`, as shown in the following example: + +[!code-csharp[](snippets/system-text-json-how-to/csharp/DeserializeIgnoreNull.cs?name=SnippetDeserialize)] + +With this option, the `Summary` property of the `WeatherForecastWithDefault` object is the default value "No summary" after deserialization. + +Null values in the JSON are ignored only if they are valid. Null values for non-nullable value types cause exceptions. + ## See also * [System.Text.Json overview](system-text-json-overview.md) From 84f8abe20fd891860e8d6186e67bded817d4eb62 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Tue, 26 Jan 2021 10:41:07 -0800 Subject: [PATCH 2/4] fix snippet id --- .../serialization/system-text-json-ignore-properties.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/standard/serialization/system-text-json-ignore-properties.md b/docs/standard/serialization/system-text-json-ignore-properties.md index 6d0e033a4e9ca..dc5f03edba777 100644 --- a/docs/standard/serialization/system-text-json-ignore-properties.md +++ b/docs/standard/serialization/system-text-json-ignore-properties.md @@ -133,7 +133,7 @@ By default, if a property in JSON is null, the corresponding property in the tar For example, suppose the following code represents your target object: -[!code-csharp[](snippets/system-text-json-how-to/csharp/WeatherForecast.cs?name=SnippetWFWithDefault)] +[!code-csharp[](snippets/system-text-json-how-to/csharp/WeatherForecast.cs?name=WFWithDefault)] And suppose the following JSON is deserialized: @@ -149,7 +149,7 @@ After deserialization, the `Summary` property of the `WeatherForecastWithDefault To change this behavior, set to `true`, as shown in the following example: -[!code-csharp[](snippets/system-text-json-how-to/csharp/DeserializeIgnoreNull.cs?name=SnippetDeserialize)] +[!code-csharp[](snippets/system-text-json-how-to/csharp/DeserializeIgnoreNull.cs?name=Deserialize)] With this option, the `Summary` property of the `WeatherForecastWithDefault` object is the default value "No summary" after deserialization. From 5f852c51357b8238909dcb15c99fea8cb8c9bc73 Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Tue, 26 Jan 2021 10:46:52 -0800 Subject: [PATCH 3/4] add link --- .../serialization/system-text-json-ignore-properties.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/standard/serialization/system-text-json-ignore-properties.md b/docs/standard/serialization/system-text-json-ignore-properties.md index dc5f03edba777..6e5e6db4704a5 100644 --- a/docs/standard/serialization/system-text-json-ignore-properties.md +++ b/docs/standard/serialization/system-text-json-ignore-properties.md @@ -30,6 +30,8 @@ When serializing C# objects to JavaScript Object Notation (JSON), by default, al * [All null-value properties](#ignore-all-null-value-properties) ::: zone-end +This article also includes a section about how to [ignore null when deserializing](#ignore-null-when-deserializing) + ## Ignore individual properties To ignore individual properties, use the [[JsonIgnore]](xref:System.Text.Json.Serialization.JsonIgnoreAttribute) attribute. From f355c7ed151b711abf4879d3c80f23cc07175e0a Mon Sep 17 00:00:00 2001 From: Tom Dykstra Date: Tue, 26 Jan 2021 10:57:59 -0800 Subject: [PATCH 4/4] Update docs/standard/serialization/system-text-json-ignore-properties.md --- .../serialization/system-text-json-ignore-properties.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard/serialization/system-text-json-ignore-properties.md b/docs/standard/serialization/system-text-json-ignore-properties.md index 6e5e6db4704a5..57f43181ea7bf 100644 --- a/docs/standard/serialization/system-text-json-ignore-properties.md +++ b/docs/standard/serialization/system-text-json-ignore-properties.md @@ -30,7 +30,7 @@ When serializing C# objects to JavaScript Object Notation (JSON), by default, al * [All null-value properties](#ignore-all-null-value-properties) ::: zone-end -This article also includes a section about how to [ignore null when deserializing](#ignore-null-when-deserializing) +This article also includes a section about how to [ignore null when deserializing](#ignore-null-when-deserializing). ## Ignore individual properties