Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. 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)

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.0
    • Affects Version/s: 1.8.3
    • Component/s: Serialization
    • Environment:
      Windows 8.1

      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();
      conventions.Add(new MemberSerializationOptionsConvention(typeof(decimal), new RepresentationSerializationOptions(BsonType.Double)));
      ConventionRegistry.Register("Serialize decimal as double", conventions, t => true);

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            nikkilocke Nikki Locke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: