Newtonsoft.Json
Newtonsoft.Json copied to clipboard
Can we include DateParseHandling in JsonPropertyAttribute?
Hi,
I found that DateParseHandling is only globally applied in JsonSerializerSettings, rather than applied to individual property. Therefore, if there is a need to handle both converted DateTime/DateTimeOffset value and string value itself, I need:
- To set
DateParseHandling = DateParseHandling.Noneand receive the datetime value as a string, and - To write an additional property and put
JsonIgnoreAttributeon the property and parse the datetime string value to DateTime/DateTimeOffset
However, if JsonPropertyAttribute includes the DataParseHandling property, it'll be remarkably reduce the number of coding lines.
Do you have a plan to implement this feature?
I ran into a similar need to do this, and ended up implementing a converter (since you can apply a converter via an attribute). Our use case is primarily for pass-through data that we don't care about, and just gets deserialized as a JObject.
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
var savedDateParseSettings = reader.DateParseHandling;
reader.DateParseHandling = DateParseHandling.None;
try {
return JObject.Load(reader);
} finally {
reader.DateParseHandling = savedDateParseSettings;
}
}
+1