[CSHARP-4767] Automatic known types based on dotnet 7+ JsonDerivedTypeAttribute Created: 22/Aug/23  Updated: 31/Aug/23  Resolved: 31/Aug/23

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

Type: New Feature Priority: Unknown
Reporter: Miroljub Tomic Assignee: James Kovacs
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2023-08-22-17-15-39-777.png    
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Is it possible to implement logic to automatically populate known types based on already defined polymorphism using System.Text.Json.Serialization attributes?

 

Simple example that we are using:



 Comments   
Comment by James Kovacs [ 31/Aug/23 ]

Hi, miroljub.tomic@mydesigns.io,

Thank you for your feature suggestion. As you know, the MongoDB .NET/C# Driver provides the [BsonKnownTypes] attribute as well as BsonClassMap<T>.AddKnownType for specifying derived classes. We do not support attributes from other frameworks because it would necessitate a dependency on those other frameworks and NuGet packages. If we support System.Text.Json (which Microsoft ships as a separate NuGet package), then we should support Newtonsoft.Json, ProtoBuf, DataContractSerializer, XmlSerializer, and the myriad other data serialization frameworks. This would increase the number of dependencies for all users even if they do not use that particular serialization format.

Additionally MongoDB's serialization format is BSON, which is not the same as JSON. BSON uses a binary serialization format and includes richer type support than JSON. Thus examining JSON attributes to configure BSON serialization doesn't make sense. It is equivalent to examining XML attributes to configure JSON serialization. While it might make sense for your application, it doesn't make sense in the general case. As well some users may need to intentionally configure their JSON serialization differently than their BSON serialization.

If using JSON attributes such as JsonDerivedType makes sense for your application, you can include the code snippet that you provided in your application's bootstrap code.

I've closed this feature suggestion as Won't Do, but happy to discuss in further comments if you have additional input.

Sincerely,
James

Comment by PM Bot [ 22/Aug/23 ]

Hi miroljub.tomic@mydesigns.io, thank you for reporting this issue! The team will look into it and get back to you soon.

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