Click or drag to resize
Json.NETJsonObjectAttribute force object serialization
 
Sample
Types
 1[JsonObject]
 2public class Directory : IEnumerable<string>
 3{
 4    public string Name { get; set; }
 5    public IList<string> Files { get; set; }
 6
 7    public Directory()
 8    {
 9        Files = new List<string>();
10    }
11
12    public IEnumerator<string> GetEnumerator()
13    {
14        return Files.GetEnumerator();
15    }
16
17    IEnumerator IEnumerable.GetEnumerator()
18    {
19        return GetEnumerator();
20    }
21}
Usage
 1Directory directory = new Directory
 2{
 3    Name = "My Documents",
 4    Files =
 5    {
 6        "ImportantLegalDocuments.docx",
 7        "WiseFinancalAdvice.xlsx"
 8    }
 9};
10
11string json = JsonConvert.SerializeObject(directory, Formatting.Indented);
12
13Console.WriteLine(json);
14// {
15//   "Name": "My Documents",
16//   "Files": [
17//     "ImportantLegalDocuments.docx",
18//     "WiseFinancalAdvice.xlsx"
19//   ]
20// }