Reputation: 2590
Is there any way to serialize only private fields of my object which has DataMember attributes in MongoDB?
string json =
item.ToJson(
new MongoDB.Bson.IO.JsonWriterSettings()
{
GuidRepresentation = GuidRepresentation.Standard,
Indent = false,
OutputMode = MongoDB.Bson.IO.JsonOutputMode.JavaScript
}
);
Upvotes: 1
Views: 1002
Reputation: 14595
To prevent a public field from being serialized use the BsonIgnore attribute:
public class Car
{
public string Brand;
public string Model;
[BsonIgnore]
public double Price;
}
In the sample code above the price field will be ignored when the class is serialized.
Upvotes: 1