Skip to content

Instantly share code, notes, and snippets.

@clarkezone
Created April 2, 2020 20:06
Show Gist options
  • Save clarkezone/ec8f8d5cad6d33160eb82638b75b6537 to your computer and use it in GitHub Desktop.
Save clarkezone/ec8f8d5cad6d33160eb82638b75b6537 to your computer and use it in GitHub Desktop.
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