[CSHARP-1916] Ability to have 2 different serialization configuration within the same process Created: 12/Feb/17  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Configuration, Serialization
Affects Version/s: 2.4
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Lenny Granovsky Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have a POCO object (ClassA) that is configured with certain Bson attributes for class and properties. Of cause when I save an instance of it to the DB, driver honers all attributes.

I have another object that is declared as:
public class EventData
{
public Guid Id

{get; set;}

[BsonElement]
public dynamic Details

{get;set;}

}
I assign ClassA instance to the Details property of the EventData. When I save the EventData to the DB, driver attaches "_t" element as well follows all Bson attributes that ClassA declares, including property name aliases, etc.
I would like that when ClassA is being saved on its own, driver follows all instructions the Bson attributes provide, but when ClassA is being saved as part of EventData, driver should ignore the Bson attributes declared in ClassA.

This is similar to providing multiple Property names resolvers to JSON.NET...

Is this possible to do? If so, how would I do this? Do you plan to add this capability if not yet available?



 Comments   
Comment by Lenny Granovsky [ 20/Apr/17 ]

Since I didn't get response for last comment, I would like to rephrase it: is it possible to provide an option to ignore all Bson attributes by passing special config parameter into serialization context?
The business case I'm trying to address is to be able to save .net class with Bson attributes into DB, and then read the same document as dynamic type in a such a way that object structure would be the same regardless whether class type or dynamic was used for read operation.

Comment by Lenny Granovsky [ 02/Mar/17 ]

Maybe it would be possible from simpler option to "ignore serialization Bson attributes"? This would provide lots of help already.

Comment by Robert Stam [ 02/Mar/17 ]

We have talked about the possibility of supporting multiple simultaneous serialization configurations, but it is difficult to do, so for the foreseeable future it is unlikely that we will be able to do this.

Comment by Lenny Granovsky [ 12/Feb/17 ]

I would like to add that this is essentially equivalent of an option to enforce ignoring all attributes for sub-classes under "dynamic" typed property when saving to DB.

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