我有许多不可变的值类型类,例如EmailAddress
,它确保任何非null实例有效.我希望"123@abc.com"
在使用MongoDB C#Driver持久化时,将这些类型的对象的序列化控制为标准字符串表示().
我尝试过实现IBsonSerilizer
它,但它只允许根级别的对象或数组.我能够用Json.NET实现适当的Json Serilization,我应该采用不同的方法吗?
我假设你的意思是这样的EmailAddress类:
[BsonSerializer(typeof(EmailAddressSerializer))] public class EmailAddress { private string _value; public EmailAddress(string value) { _value = value; } public string Value { get { return _value; } } }
我使用了一个属性将EmailAddress类链接到自定义序列化程序,可以像这样实现:
public class EmailAddressSerializer : BsonBaseSerializer { public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return null; } else { var value = bsonReader.ReadString(); return new EmailAddress(value); } } public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options) { if (value == null) { bsonWriter.WriteNull(); } else { var emailAddress = (EmailAddress)value; bsonWriter.WriteString(emailAddress.Value); } } }
您不能将EmailAddress序列化为根文档(因为它不是文档...).但您可以使用嵌入在其他文档中的EmailAddress.例如:
public class Person { public int Id { get; set; } public EmailAddress EmailAddress { get; set; } }
您可以使用以下代码进行测试:
var person = new Person { Id = 1, EmailAddress = new EmailAddress("joe@xyz.com") }; var json = person.ToJson(); var rehyrdated = BsonSerializer.Deserialize(json);
生成的JSON/BSON文档是:
{ "_id" : 1, "EmailAddress" : "joe@xyz.com" }