[CSHARP-1868] Deserialize discriminated data to ExpandoObject Created: 15/Dec/16  Updated: 27/Oct/23  Resolved: 04/Jan/21

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Anatoliy Rabchevskiy Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I'm trying to deserialize entire document that contains polymorphic collection to dynamic object.
And I expect that my collection will contain dynamic objects but I've got concrete types.
I've created pull request with fix and unit test.

Example data:
var json = "{ \"Shapes\" : [

{ \"_t\" : \"Circle\", \"Radius\" : 5 }

,

{ \"_t\" : \"Square\", \"Side\" : 3 }

] }";

Current behavior:
typeof(Circle) == obj.Shapes[0].GetType()
typeof(Square) == obj.Shapes[1].GetType()

Expected:
typeof(ExpandoObject) == obj.Shapes[0].GetType()
typeof(ExpandoObject) == obj.Shapes[1].GetType()



 Comments   
Comment by Boris Dogadov [ 04/Jan/21 ]

Hi anatoliy.rabchevskiy, thank you for your PR.

Deserializing the discriminated type as a concrete type would be the expected behaviour in most use-cases. Changing this behavior would be a breaking change. However this default behavior can be overridden by a custom serializer. 

Generated at Wed Feb 07 21:40:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.