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: daprdocs/content/en/dotnet-sdk-docs/dotnet-actors/dotnet-actors-serialization.md
+31-1Lines changed: 31 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ type: docs
3
3
title: "Actor serialization in the .NET SDK"
4
4
linkTitle: "Actor serialization"
5
5
weight: 300000
6
-
description: Necessary steps to serialize your types using remoted Actors in .NET
6
+
description: Necessary steps to serialize your types remoted and non-remoted Actors in .NET
7
7
---
8
8
# Actor Serialization
9
9
@@ -254,6 +254,36 @@ a complex versioning scheme for our existing enum values in the state.
254
254
{"event": "Conference", "season": "fall"}
255
255
```
256
256
257
+
### Polymorphic Serialization
258
+
When working with polymorphic types in Dapr Actor clients, it is essential to handle serialization and deserialization correctly to ensure that the appropriate
259
+
derived types are instantiated. Polymorphic serialization allows you to serialize objects of a base type while preserving the specific derived type information.
260
+
261
+
To enable polymorphic deserialization, you must use the `[JsonPolymorphic]` attribute on your base type. Additionally,
262
+
it is crucial to include the `[AllowOutOfOrderMetadataProperties]` attribute to ensure that metadata properties, such as `$type`
263
+
can be processed correctly by System.Text.Json even if they are not the first properties in the JSON object.
264
+
265
+
#### Example
266
+
```cs
267
+
[JsonPolymorphic]
268
+
[AllowOutOfOrderMetadataProperties]
269
+
publicabstractclassSampleValueBase
270
+
{
271
+
publicstringCommonProperty { get; set; }
272
+
}
273
+
274
+
publicclassDerivedSampleValue : SampleValueBase
275
+
{
276
+
publicstringSpecificProperty { get; set; }
277
+
}
278
+
```
279
+
In this example, the `SampleValueBase` class is marked with both `[JsonPolymorphic]` and `[AllowOutOfOrderMetadataProperties]`
280
+
attributes. This setup ensures that the `$type` metadata property can be correctly identified and processed during
281
+
deserialization, regardless of its position in the JSON object.
282
+
283
+
By following this approach, you can effectively manage polymorphic serialization and deserialization in your Dapr Actor
284
+
clients, ensuring that the correct derived types are instantiated and used.
285
+
286
+
257
287
## Strongly-typed Dapr Actor client
258
288
In this section, you will learn how to configure your classes and records so they are properly serialized and deserialized at runtime when using a strongly-typed actor client. These clients are implemented using .NET interfaces and are <u>not</u> compatible with Dapr Actors written using other languages.
0 commit comments