C# 在MongoDB中保存POCO时忽略属性,但在序列化为JSON时不要忽略它
我在MongoDB中存储了以下模型:C# 在MongoDB中保存POCO时忽略属性,但在序列化为JSON时不要忽略它,c#,json,mongodb,mongodb-.net-driver,bson,C#,Json,Mongodb,Mongodb .net Driver,Bson,我在MongoDB中存储了以下模型: public class Person { public ObjectId Id { get; set; } public Int PersonId { get; set; } public BsonDocument Resume { get; set; } // arbitrary JSON [BsonIgnore] public string FirstName { get; set; } // store
public class Person
{
public ObjectId Id { get; set; }
public Int PersonId { get; set; }
public BsonDocument Resume { get; set; } // arbitrary JSON
[BsonIgnore]
public string FirstName { get; set; } // stored elsewhere,
// populated at runtime
[BsonIgnore]
public string LastName { get; set; } // ditto
}
Resume
是一个BsonDocument
文件,其中我存储了无法标准化为POCO的任意JSON(每次出现都有很大的不同)
我不想存储此人的名字和姓氏,因为这些信息已经存储在SQL数据库中,我不想担心同步更改。所以我用[BsonIgnore]
修饰了这些参数。当我的应用程序代码从MongoDB检索到Person
时,它会在将其序列化为JSON之前填充FirstName
和LastName
参数,如下所示:
public ActionResult GetPerson(int id)
{
var query = New QueryDocument("_id", ObjectId.Parse(id));
// personCollection is Collection<Person>
var person = personCollection.FindOne(query);
var pc = personCache.GetPerson(person.PersonId);
person.FirstName = pc.FirstName;
person.LastName = pc.LastName;
var settings = New JsonWriterSettings() { Outputmode = JsonOutputMode.Strict }
return Json(person.ToJson(settings), JsonRequestBehavior.AllowGet);
}
public ActionResult GetPerson(int-id)
{
var query=newquerydocument(“_id”,ObjectId.Parse(id));
//个人收藏就是收藏
var person=personCollection.FindOne(查询);
var pc=personCache.GetPerson(person.PersonId);
person.FirstName=pc.FirstName;
person.LastName=pc.LastName;
var settings=New JsonWriterSettings(){Outputmode=JsonOutputMode.Strict}
返回Json(person.ToJson(settings),JsonRequestBehavior.AllowGet);
}
但是,生成的JSON缺少FirstName
和LastName
节点,显然是因为它们被[BsonIgnore]
修饰
有没有一种方法可以告诉用户在序列化为JSON时忽略在MongoDB中保存参数,但不要忽略它?对firstname和lastname属性使用[NotMapping]属性,并删除[BsonIgnore]属性。当您使用驱动程序本身将对象序列化为
json
时,我认为这是不可能的
但是您可以(也可能应该)使用来序列化为json
。这样,您的BsonIgnore
对json
序列化没有影响,json
属性(如JsonIgnore
)对您的mongo驱动程序没有影响
public class Person
{
[JsonIgnore]
publis string FullName {get; set;}
[BsonIgnore]
public string FirstName { get; set; }
[BsonIgnore]
public string LastName { get; set; }
}
在我看来,这也是一个更好的设计,因为
bson
用于DB,而json
(大部分)用于web消费,因此可以使用不同的工具来完成。我决定采用I3arnon,并使用json.NET。问题是Json.NET不知道如何处理大多数Mongo类型,因此序列化Resume
属性是有问题的,因为它属于BsonDocument
类型。以下是我的想法:
Json.NET通常可以序列化Mongo查询的结果,但是BsonDocument
给了它一个问题。在我的示例中,如果没有关于如何处理BsonDocument
的特殊说明,Json.NET无法序列化Person
首先,我创建了一个名为BsonDocumentConverter的JsonConverter
:
此转换器使用MongoDB C#驱动程序序列化任何BsonDocuments
要使用转换器,请执行以下操作:
var result = JsonConvert.SerializeObject(person, Formatting.None,
new JsonSerializerSettings() {
NullValueHandling = NullValueHandling.Ignore,
Converters = new List<JsonConverter>() {
new BsonDocumentConverter()
}
});
return Json(result, JsonRequestBehavior.AllowGet);
var result=JsonConvert.SerializeObject(person,Formatting.None,
新的JsonSerializerSettings(){
NullValueHandling=NullValueHandling.Ignore,
转换器=新列表(){
新的BsonDocumentConverter()
}
});
返回Json(结果,JsonRequestBehavior.AllowGet);
我相信[NotMapped]
是一个实体框架属性。它在MongoDB中无效。[NotMapped]是System.ComponentModel.DataAnnotations.Schema属性。请查看此链接并进行尝试。在MongoDB中我不希望使用的两个参数上使用[NotMapped]
无效。参数仍然显示在文档中。MongoDB C#驱动程序可以序列化为JSON。它添加了一个.ToJson
扩展方法。至于使用Json.NET,这是一个好主意,但在序列化Mongo类型时,它不会立即起作用。我想出来了,然后贴出了答案。我把你的答案记下来。@Paperjam是的。当然可以。我的意思是,虽然MongoDB使用bson,但它“帮助”您序列化json。但如果您使用json for web,您可能需要一个健壮的专用框架。无论如何,谢谢您……虽然您可以使用Json.Net输出,但如果您使用子分类,则无法对数据进行往返,因为它不包括Mongo序列化程序所具有的类层次结构。@crashmstr您可以将序列化程序配置为在$type
属性中添加该信息(请参阅)
var result = JsonConvert.SerializeObject(person, Formatting.None,
new JsonSerializerSettings() {
NullValueHandling = NullValueHandling.Ignore,
Converters = new List<JsonConverter>() {
new BsonDocumentConverter()
}
});
return Json(result, JsonRequestBehavior.AllowGet);