xkcd
xkcd

Reputation: 2590

Serialization settings for MongoDB

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

Answers (1)

Thomas C. G. de Vilhena
Thomas C. G. de Vilhena

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

Related Questions