C# Driver
  1. C# Driver
  2. CSHARP-587

Overwrite serializers in BsonSerializer

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Minor - P4 Minor - P4
    • Resolution: Works as Designed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • # Replies:
      2
    • Last comment by Customer:
      true

      Description

      It would be nice if there was a way to override default serializers for primitive types. While it can be worked around by registering serializers before the sctor of BsonSerializer executes or by using Reflection, a more straight-forward way would probably be to include a version of BsonSerializer.RegisterSerializer that allows to overwrite serializers for already covered types.

      This stackoverflow question might be able to provide more context (note, I am not the original poster of said question): http://stackoverflow.com/questions/12201812/mongodb-override-default-serializer-for-a-c-sharp-primitive-type/12636123#comment17048789_12636123

        Issue Links

          Activity

          Hide
          Craig Wilson
          added a comment -

          While I understand what you are saying, it isn't a good solution to enable changing the serializer for a type after it has already been used. For instance, if this was possible, the below sequence of events would be awkward.

          • save an entity with a double
          • change the double's serializer
          • read the entity with a double

          Obviously, what would be read would be different than what was written, making the data un-uniform.

          As Robert mentioned in the StackOverflow question, you can replace the default serializers for primitive types. The idea is that their is an implicit initialization phase for your applications, prior to any communication with MongoDB. In this initialization area, you can do virtually anything. The static ctor of BsonSerializer definitely runs prior to registration of new serializers (otherwise, .NET is broken). The reason Robert's suggestion works is because the defaults are part of a lazy serializer registration system (IBsonSerializabionProvider). Until a serializer for a type is needed, it isn't registered. Therefore, if you register a serializer for a type (e.g. double) before it is used, then you have replaced the defaults.

          Show
          Craig Wilson
          added a comment - While I understand what you are saying, it isn't a good solution to enable changing the serializer for a type after it has already been used. For instance, if this was possible, the below sequence of events would be awkward. save an entity with a double change the double's serializer read the entity with a double Obviously, what would be read would be different than what was written, making the data un-uniform. As Robert mentioned in the StackOverflow question, you can replace the default serializers for primitive types. The idea is that their is an implicit initialization phase for your applications, prior to any communication with MongoDB. In this initialization area, you can do virtually anything. The static ctor of BsonSerializer definitely runs prior to registration of new serializers (otherwise, .NET is broken). The reason Robert's suggestion works is because the defaults are part of a lazy serializer registration system (IBsonSerializabionProvider). Until a serializer for a type is needed, it isn't registered. Therefore, if you register a serializer for a type (e.g. double) before it is used, then you have replaced the defaults.
          Hide
          Craig Wilson
          added a comment -

          This works as designed.

          Show
          Craig Wilson
          added a comment - This works as designed.

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                1 year, 27 weeks, 2 days ago
                Date of 1st Reply: