You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/standard/serialization/system-text-json/polymorphism.md
+16-7Lines changed: 16 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: How to serialize properties of derived classes with System.Text.Json
3
3
description: "Learn how to serialize polymorphic objects while serializing to and deserializing from JSON in .NET."
4
-
ms.date: 09/30/2022
4
+
ms.date: 10/18/2024
5
5
no-loc: [System.Text.Json, Newtonsoft.Json]
6
6
zone_pivot_groups: dotnet-version
7
7
dev_langs:
@@ -17,7 +17,7 @@ ms.topic: how-to
17
17
18
18
# How to serialize properties of derived classes with System.Text.Json
19
19
20
-
In this article, you will learn how to serialize properties of derived classes with the `System.Text.Json` namespace.
20
+
In this article, you learn how to serialize properties of derived classes with the `System.Text.Json` namespace.
21
21
22
22
## Serialize properties of derived classes
23
23
@@ -148,7 +148,7 @@ The following example shows the JSON that results from the preceding code:
148
148
Beginning with .NET 7, `System.Text.Json` supports polymorphic type hierarchy serialization and deserialization with attribute annotations.
149
149
150
150
| Attribute | Description |
151
-
|--|--|
151
+
|-----------|-------------|
152
152
|<xref:System.Text.Json.Serialization.JsonDerivedTypeAttribute>| When placed on a type declaration, indicates that the specified subtype should be opted into polymorphic serialization. It also exposes the ability to specify a type discriminator. |
153
153
|<xref:System.Text.Json.Serialization.JsonPolymorphicAttribute>| When placed on a type declaration, indicates that the type should be serialized polymorphically. It also exposes various options to configure polymorphic serialization and deserialization for that type. |
154
154
@@ -240,7 +240,7 @@ Public Class WeatherForecastWithCity
240
240
EndClass
241
241
```
242
242
243
-
With the added metadata, specifically, the type discriminator, the serializer can serialize and deserialize the payload as the `WeatherForecastWithCity` type from its base type `WeatherForecastBase`. Serialization will emit JSON along with the type discriminator metadata:
243
+
With the added metadata, specifically, the type discriminator, the serializer can serialize and deserialize the payload as the `WeatherForecastWithCity` type from its base type `WeatherForecastBase`. Serialization emits JSON along with the type discriminator metadata:
> By default, the `$type` discriminator must be placed at the start of the JSON object, grouped together with other metadata properties like `$id` and `$ref`. If you're reading data off an external API that places the `$type` discriminator in the middle of the JSON object, set <xref:System.Text.Json.JsonSerializerOptions.AllowOutOfOrderMetadataProperties?displayProperty=nameWithType> to `true`:
Copy file name to clipboardExpand all lines: docs/standard/serialization/system-text-json/preserve-references.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ The following code illustrates use of the `Preserve` setting.
37
37
38
38
This feature can't be used to preserve value types or immutable types. On deserialization, the instance of an immutable type is created after the entire payload is read. So it would be impossible to deserialize the same instance if a reference to it appears within the JSON payload.
39
39
40
-
For value types, immutable types, and arrays, no reference metadata is serialized. On deserialization, an exception is thrown if `$ref` or `$id` is found. However, value types ignore `$id` (and `$values` in the case of collections) to make it possible to deserialize payloads that were serialized by using Newtonsoft.Json. Newtonsoft.Json does serialize metadata for such types.
40
+
For value types, immutable types, and arrays, no reference metadata is serialized. On deserialization, an exception is thrown if `$ref` or `$id` is found. However, value types ignore `$id` (and `$values` in the case of collections) to make it possible to deserialize payloads that were serialized by using Newtonsoft.Json, which does serialize metadata for such types.
41
41
42
42
To determine if objects are equal, System.Text.Json uses <xref:System.Collections.Generic.ReferenceEqualityComparer.Instance%2A?displayProperty=nameWithType>, which uses reference equality (<xref:System.Object.ReferenceEquals(System.Object,System.Object)?displayProperty=nameWithType>) instead of value equality (<xref:System.Object.Equals(System.Object)?displayProperty=nameWithType>) when comparing two object instances.
43
43
@@ -47,7 +47,7 @@ The <xref:System.Text.Json.Serialization.ReferenceResolver> class defines the be
47
47
48
48
### Persist reference metadata across multiple serialization and deserialization calls
49
49
50
-
By default, reference data is only cached for each call to <xref:System.Text.Json.JsonSerializer.Serialize%2A> or <xref:System.Text.Json.JsonSerializer.Deserialize%2A>. To persist references from one `Serialize`/`Deserialize` call to another one, root the <xref:System.Text.Json.Serialization.ReferenceResolver> instance in the call site of `Serialize`/`Deserialize`. The following code shows an example for this scenario:
50
+
By default, reference data is only cached for each call to <xref:System.Text.Json.JsonSerializer.Serialize%2A> or <xref:System.Text.Json.JsonSerializer.Deserialize%2A>. To persist references from one `Serialize` or `Deserialize` call to another one, root the <xref:System.Text.Json.Serialization.ReferenceResolver> instance in the call site of `Serialize`/`Deserialize`. The following code shows an example for this scenario:
51
51
52
52
* You have a list of `Employee` objects and you have to serialize each one individually.
53
53
* You want to take advantage of the references saved in the resolver for the `ReferenceHandler`.
0 commit comments