Skip to content

Commit aa70c5b

Browse files
authored
Added text specific to Dapr Actors serialization about polymorphic attributes necessary for System.Text.Json support (#1498)
Signed-off-by: Whit Waldo <[email protected]>
1 parent c0d70ff commit aa70c5b

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

daprdocs/content/en/dotnet-sdk-docs/dotnet-actors/dotnet-actors-serialization.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ type: docs
33
title: "Actor serialization in the .NET SDK"
44
linkTitle: "Actor serialization"
55
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
77
---
88
# Actor Serialization
99

@@ -254,6 +254,36 @@ a complex versioning scheme for our existing enum values in the state.
254254
{"event": "Conference", "season": "fall"}
255255
```
256256

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+
public abstract class SampleValueBase
270+
{
271+
public string CommonProperty { get; set; }
272+
}
273+
274+
public class DerivedSampleValue : SampleValueBase
275+
{
276+
public string SpecificProperty { 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+
257287
## Strongly-typed Dapr Actor client
258288
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.
259289

0 commit comments

Comments
 (0)