Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2333

Custom Serializer returned value not being respected

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.7.0
    • Component/s: Serialization
    • Labels:
      None
    • Environment:
      Cosmos Db

       

      The string returned by my custom serializer, which is overriding how datetime strings are formatted is "2018-07-18T14:35:27.7108283+02:00". However in my database the stored value is "2018-07-18T12:35:27.7108283+00:00".

      My code:

      BsonSerializer.RegisterSerializer(new DateTimeOffsetSerializer(BsonType.Document));   
      
      public class MyDateTimeOffsetSerializer : StructSerializerBase<DateTimeOffset>
          {
              public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args,
                  DateTimeOffset value)
              {
                  IBsonWriter writer = context.Writer;
                  writer.WriteString(ToString(value));
              }        
      public static string ToString(DateTimeOffset value)
              {
                  var x = value.ToString("O");
                  return x; // returns string like "2018-07-18T14:35:27.7108283+02:00"
              }        
          }
      

      The serializer is registered right before I Insert the document. What is happening to the value after I've serialized it and how can I change it?

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            kdog Magnus
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: