Created
April 2, 2020 20:06
-
-
Save clarkezone/ec8f8d5cad6d33160eb82638b75b6537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MongoDB.Bson; | |
using MongoDB.Bson.Serialization.Attributes; | |
namespace BSONSerializationTest | |
{ | |
public class ServiceDescription | |
{ | |
[BsonId] | |
[BsonRepresentation(BsonType.ObjectId)] | |
public string Id { get; set; } //If you leave this blank one will be assigned by Cosmos | |
public string ServiceName { get; set; } | |
public DateTime CreatedAt { get; set; } | |
} | |
public class ServiceDescriptionBad | |
{ | |
[BsonId] | |
[BsonRepresentation(BsonType.ObjectId)] | |
public string Id { get; set; } //If you leave this blank one will be assigned by Cosmos | |
public string ServiceName { get; set; } | |
public DateTime CreatedAt { get; set; } | |
public BsonDocument Doc { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var good = new ServiceDescription() { CreatedAt=DateTime.Now, ServiceName="test" }; | |
var works = System.Text.Json.JsonSerializer.Serialize(good, typeof(ServiceDescription)); | |
var bad = new ServiceDescriptionBad() { CreatedAt = DateTime.Now, ServiceName = "test", Doc = good.ToBsonDocument() }; | |
var broken = System.Text.Json.JsonSerializer.Serialize(bad, typeof(ServiceDescriptionBad)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment