Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 1.51 KB

JsonObjectAttributeOptIn.md

File metadata and controls

42 lines (35 loc) · 1.51 KB

JsonObjectAttribute opt-in serialization

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

[JsonObject(MemberSerialization.OptIn)]
public class File
{
    // excluded from serialization
    // does not have JsonPropertyAttribute
    public Guid Id { get; set; }

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

    [JsonProperty] public int Size { get; set; }
}

snippet source | anchor

var file = new File
{
    Id = Guid.NewGuid(),
    Name = "ImportantLegalDocuments.docx",
    Size = 50 * 1024
};

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

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

snippet source | anchor