标签:
class ObjectIdConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { serializer.Serialize(writer, value.ToString()); } /// <summary> // 反序列化时将字符串转换成ObjectId类型 /// </summary> /// <param name="reader"></param> /// <param name="objectType"></param> /// <param name="existingValue"></param> /// <param name="serializer"></param> /// <returns></returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return new ObjectId(serializer.Deserialize(reader).ToString()); } public override bool CanConvert(Type objectType) { return typeof(ObjectId).IsAssignableFrom(objectType); } }
[BsonId] [JsonConverter(typeof(ObjectIdConverter))] public ObjectId Id { get; set; }
使用Rabbitmq.client反序列化包含Mongo.Bson.ObjectId属性实体类时抛异常
标签:
原文地址:http://www.cnblogs.com/gossip/p/4505570.html