Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

Can we include DateParseHandling in JsonPropertyAttribute?

Open justinyoo opened this issue 7 years ago • 2 comments

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.None and receive the datetime value as a string, and
  • To write an additional property and put JsonIgnoreAttribute on 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?

justinyoo avatar Feb 03 '18 06:02 justinyoo

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;
   }
}

michaeltdaniels avatar Mar 23 '20 15:03 michaeltdaniels

+1

Ettores88 avatar May 20 '22 14:05 Ettores88