I am using the JSON.Net library. I have some JSON which looks like this:
"Templates": [
{
"Name": "Default Fields 1",
"Fields": [16, 10, 0, 4, 5, 11, 12, 7, 15, 17, 9, 25, 3],
"Formats": [
"string", "datetime", "LeftZeroPaddedString13", "2dp", "2dp", "2dp", "2dp", "int", "int",
"string", "int", "int", "int", "int"
]
},
{
"Name": "Default Fields 2",
"Fields": [3, 25, 9, 17, 15, 7, 12, 11, 5, 4, 0, 10, 16],
"Formats": [
"int", "int", "int", "int", "string", "int", "int", "2dp", "2dp", "2dp", "2dp", "LeftZeroPaddedString13",
"datetime", "string"
]
}]
And I am deserializing it to the Templates
property within the following class:
public class Options
{
public List<FieldTemplate> Templates { get; set; }
}
public class FieldTemplate
{
public string Name { get; set; }
public List<int> Fields { get; set; }
public List<string> Formats { get; set; }
}
This works fine, but under some circumstances (not always) I want to prevent the Fields
and Formats
properties from being included when I serialize the object again (though I do want to keep the Name
property in the serialized output). I have thought about using ShouldSerializeFields()
and ShouldSerializeFormats()
within FieldTemplate
and then looping through the object to set a boolean property which each of these methods can read, but it doesn't seem elegant. Is there a better way? For example, I could set boolean properties in Options
which FieldTemplate
could then use. I don't know how to do that though, or even if its possible.