|
1 | 1 | ---
|
2 | 2 | title: "How to write custom converters for JSON serialization - .NET"
|
3 | 3 | description: "Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace."
|
4 |
| -ms.date: 12/09/2020 |
| 4 | +ms.date: 12/14/2020 |
5 | 5 | no-loc: [System.Text.Json, Newtonsoft.Json]
|
6 | 6 | zone_pivot_groups: dotnet-version
|
7 | 7 | helpviewer_keywords:
|
@@ -77,7 +77,7 @@ The preceding code is the same as what is shown in the [Support Dictionary with
|
77 | 77 | The following steps explain how to create a converter by following the basic pattern:
|
78 | 78 |
|
79 | 79 | * Create a class that derives from <xref:System.Text.Json.Serialization.JsonConverter%601> where `T` is the type to be serialized and deserialized.
|
80 |
| -* Override the `Read` method to deserialize the incoming JSON and convert it to type `T`. Use the <xref:System.Text.Json.Utf8JsonReader> that is passed to the method to read the JSON. |
| 80 | +* Override the `Read` method to deserialize the incoming JSON and convert it to type `T`. Use the <xref:System.Text.Json.Utf8JsonReader> that is passed to the method to read the JSON. You don't have to worry about handling partial data, as the serializer passes all the data for the current JSON scope. So it isn't necessary to call <xref:System.Text.Json.Utf8JsonReader.Skip%2A> or <xref:System.Text.Json.Utf8JsonReader.TrySkip%2A> or to validate that <xref:System.Text.Json.Utf8JsonReader.Read%2A> returns `true`. |
81 | 81 | * Override the `Write` method to serialize the incoming object of type `T`. Use the <xref:System.Text.Json.Utf8JsonWriter> that is passed to the method to write the JSON.
|
82 | 82 | * Override the `CanConvert` method only if necessary. The default implementation returns `true` when the type to convert is of type `T`. Therefore, converters that support only type `T` don't need to override this method. For an example of a converter that does need to override this method, see the [polymorphic deserialization](#support-polymorphic-deserialization) section later in this article.
|
83 | 83 |
|
@@ -370,7 +370,7 @@ This null-handling behavior is primarily to optimize performance by skipping an
|
370 | 370 | ::: zone pivot="dotnet-5-0"
|
371 | 371 | To enable a custom converter to handle `null` for a reference or value type, override <xref:System.Text.Json.Serialization.JsonConverter%601.HandleNull%2A?displayProperty=nameWithType> to return `true`, as shown in the following example:
|
372 | 372 |
|
373 |
| -:::code language="csharp" source="snippets/system-text-json-how-to-5-0/csharp/CustomConverterHandleNull.cs" highlight="19"::: |
| 373 | +:::code language="csharp" source="snippets/system-text-json-how-to-5-0/csharp/CustomConverterHandleNull.cs" highlight="18"::: |
374 | 374 | ::: zone-end
|
375 | 375 |
|
376 | 376 | ## Other custom converter samples
|
|
0 commit comments