question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot deserialize a 'DateTimeOffset' from BsonType 'DateTime'

See original GitHub issue

If you provide “DateTimeOffsetSupportingBsonDateTimeSerializer” , it’s will much better .

System.FormatException\r\nMessage: Cannot deserialize a ‘DateTimeOffset’ from BsonType ‘DateTime’.\r\nDataJson: {}\r\nStacktrace:\r\n at MongoDB.Bson.Serialization.Serializers.DateTimeOffsetSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)\r\n at MongoDB.Bson.Serialization.Serializers.SerializerBase1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)\r\n at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)\r\n at MongoDB.Bson.Serialization.BsonClassMapSerializer1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)\r\n",“Data”:null}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Turnerjcommented, Jul 31, 2021

Hey @fcsamuel - unless you were to write your own DateTimeOffset serializer to handle it while you’re reading/writing to the data adhoc, I doubt there is a way.

MongoDB otherwise can’t interpret a BsonType DateTime as a .NET DateTimeOffset as they are just not compatible types.

0reactions
fcsamuelcommented, Jul 21, 2021

Hey @Turnerj,

What you’re seeing is that the data in the database is of type DateTime which isn’t compatible with DateTimeOffset (as there is no offset). The serializer stores DateTimeOffset in either an [array, a document or a string]

You may need to look at the BsonRepresentation attribute for your properties (part of the official driver).

Even using the BsonRepresentation with the types BsonType.Array, BsonType.Document, BsonType.String, I still get the error. Is there a way to convert it without having to ‘manually’ do the serialization?

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB C#: Cannot deserialize a 'String' from BsonType ...
Cannot deserialize a 'String' from BsonType 'DateTime'. I have tried doing this: [BsonElement("mydate")] [BsonRepresentation(BsonType.
Read more >
MongoDB C#: Serializer for DateTimeOffset to BsonType ...
Serializing DateTimeOffset to DateTime in MongoDB is currently not supported. This solution will show you how it can work.
Read more >
Add DateTime representation to DateTimeOffsetSerializer
Hi all, I was wondering why the decision was made to not allow a BsonType.DateTime to be (de)serialized as DateTimeOffset?
Read more >
BsonSerializer issue - Drivers & ODMs
The issue all came from the datatype mapping from DateTimeOffset to BsonType.DateTime (BsonDateTime) . I have a simplified class like:
Read more >
MongoDB, C# and DateTime's - danielwertheim
Lets first have a look at a sample with DateTime and then have a look at DateTimeOffset to see if it solves the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found