I'm using Newtonsoft.Json to serialize values for caching and I'm wondering if there is a way to exclude executable properties in the same way that methods are excluded from serialization? Preferably a way to set it globally so I don't need to mark hundreds of properties with an attribute (that I would first have to find among many thousands of properties).
The code:
[Serializable]
public class Item
{
public long Id { get; set; }
public string Name { get; set; }
public List<Image> Images { get; set; }
public Image PrimaryImage { get { return Images[0]; } }
}
[Serializable]
public class Image
{
public long Id { get; set; }
public string FileName { get; set; }
}
The error:
Error getting value from 'PrimaryImage' on 'Item'.
at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
I'll also mention that the logic in PrimaryImage has been simplified so the solution is not to skip using the property. This seems like a pretty common use of properties so I find it strange that it does not handle it already.