Click or drag to resize
Json.NETDataContract and DataMember Attributes
 

This sample shows how .NET Framework attributes such as DataContractAttribute, DataMemberAttribute and NonSerializedAttribute can be used with Json.NET instead of Json.NET's own attributes.

Sample
Types
 1[DataContract]
 2public class File
 3{
 4    // excluded from serialization
 5    // does not have DataMemberAttribute
 6    public Guid Id { get; set; }
 7
 8    [DataMember]
 9    public string Name { get; set; }
10
11    [DataMember]
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// }