[CSHARP-918] There is no easy way to set a default serialization option for a type (even if it is in an array, dictionary, or derived class) Created: 26/Feb/14 Updated: 22/May/14 Resolved: 21/May/14 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Serialization |
| Affects Version/s: | 1.8.3 |
| Fix Version/s: | 2.0 |
| Type: | New Feature | Priority: | Minor - P4 |
| Reporter: | Nikki Locke | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | deserialization | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 8.1 |
||
| Description |
|
If you want (for example) to serialize all decimals as doubles (yes, I know about the loss of precision), there is no easy way to do so. var conventions = new ConventionPack(); works for plain decimals in a class, but does not apply itself to arrays of decimals, or decimals in a dictionary, or arrays of decimals in a dictionary. It would be simpler to understand if all the Serializers had a static property for the default serialization method, so you could just do DecimalSerializer.DefaultSerializationOptions = BsonType.Double; If that is considered too hacky, the alternative would be to make the array, dictionary, etc. deserializers honour registered conventions for their elements, instead of always using the hard coded default. |
| Comments |
| Comment by Craig Wilson [ 22/May/14 ] |
|
It will be in the next major release of the driver. FixVersion is labelled as 3.0. This will be a backwards breaking release in some regards, although we are trying to keep it relegated to configuration or low level ideas. You can pull successful nightly builds from our build feed. You can also register the serializer globally using: BsonSerializer.RegisterSerializer<decimal>(new DecimalSerializer().WithRepresentation(BsonType.Double)); Note that you could always do the above in relation to registering a serializer, but now you are able to set a representation directly onto the serializer. |
| Comment by Nikki Locke [ 22/May/14 ] |
|
All decimals already are stored as strings by default (or have you changed that?). – |
| Comment by Craig Wilson [ 22/May/14 ] |
|
Sorry, I realize that was pretty cryptic. I think Robert said it best in the comment above mine. We used to store the representation separately from the serializer itself. In addition, it was changeable at runtime preventing someone from saying "All Decimals should be stored as a string". Since these two concepts are now integrated, you would simply register a DecimalSerializer globally with it's representation set to string and all decimals would get stored as a string because the serializer itself was configured that way. |
| Comment by Nikki Locke [ 22/May/14 ] |
|
Please could you explain what you mean, specifically how to set the default serialisation type for all decimals to double, and which version of the library is required for it to work? |
| Comment by Craig Wilson [ 21/May/14 ] |
|
This works based on how we simply refactored the code for serialization. |
| Comment by Robert Stam [ 20/Mar/14 ] |
|
Conventions are only used with classes that will be serialized using a BsonClassMapSerializer. In 3.0 we are planning to make serialization options a property of the serializer itself. So in 3.0 you will be able to instantiate an instance of DecimalSerializer with the Representation set to BsonType.Double and register it as the serializer to use for decimals everywhere. |
| Comment by Nikki Locke [ 26/Feb/14 ] |
|
Example class to illustrate the problem: class Test { public decimal Field = 1; public decimal [] Array; public Dictionary<string, decimal[]> Dict; public SortedDictionary<string, decimal[]> Dict2; public DerivedDictionary Dict3; public Dictionary<string, DerivedDictionary> Dict4; public class DerivedDictionary : Dictionary<string, decimal[]> { |