[CSHARP-2387] EnumRepresentationConvention BsonType.String doesn't serialize enums to strings if the enums are in a list Created: 14/Sep/18  Updated: 09/Oct/18  Resolved: 09/Oct/18

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

Type: New Feature Priority: Major - P3
Reporter: Jeffrey Stapleton Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, 64-bit architecture, .NetFramework 471


Issue Links:
Duplicate
duplicates CSHARP-2096 Make EnumRepresentationConvention als... Backlog

 Description   

Create a serialization convention like so:

 

ConventionRegistry.Register("EnumStringConvention", new ConventionPack { new EnumRepresentationConvention(BsonType.String) }, t => true);

 

Then define an enum like so:

 

[PublicAPI]
public enum JobCategory
{
   Remodel,
   Repair
}

 

Finally add a list of enums to your data model

 

public class JobModel
{
    public List<JobCategory> JobCategories { get; set; } = new List<JobCategory>();
}

 

Write to mongodb then inspect the bson doc and notice that `JobCategory` is a list of int32 and not a list of strings. 

 

 



 Comments   
Comment by Robert Stam [ 09/Oct/18 ]

This is a reasonable request that is scheduled to be addressed in a future version of the driver.

Currently the convention only applies to fields of an enum or nullable enum type.

See CSHARP-2096.

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