Click or drag to resize
Json.NETJsonObjectAttribute opt-in serialization
 

This sample uses JsonObjectAttribute and MemberSerialization to specify that only properties that have been explicitly specified with JsonPropertyAttribute should be serialized.

Sample
Types
 1[JsonObject(MemberSerialization.OptIn)]
 2public class File
 3{
 4    // excluded from serialization
 5    // does not have JsonPropertyAttribute
 6    public Guid Id { get; set; }
 7
 8    [JsonProperty]
 9    public string Name { get; set; }
10
11    [JsonProperty]
12    public int Size { get; set; }
13}
Usage
 1File file = new File
 2{
 3    Id = Guid.NewGuid(),
 4    Name = "ImportantLegalDocuments.docx",
 5    Size = 50 * 1024
 6};
 7
 8string json = JsonConvert.SerializeObject(file, Formatting.Indented);
 9
10Console.WriteLine(json);
11// {
12//   "Name": "ImportantLegalDocuments.docx",
13//   "Size": 51200
14// }