Click or drag to resize
Json.NET

DataContract 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
[DataContract]
public class File
{
    // excluded from serialization
    // does not have DataMemberAttribute
    public Guid Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Size { get; set; }
}
Usage
File file = new File
{
    Id = Guid.NewGuid(),
    Name = "ImportantLegalDocuments.docx",
    Size = 50 * 1024
};

string json = JsonConvert.SerializeObject(file, Formatting.Indented);

Console.WriteLine(json);
// {
//   "Name": "ImportantLegalDocuments.docx",
//   "Size": 51200
// }