0

I do have non ISO compliant date string in incoming JSON.

"Thu, 4 Jun 2020 10:37:43 +0000 (UTC)"

To deserialize it I have to use string property then later manually cut it and convert to DateTime.

I deserialize whole JSON by JsonConvert.DeserializeObject<DeserializedClassName>(json) I can specify ISODateTimeConverter but this string is not ISO compliant, so I would like to add some kind of annotation for deserializer to process this value before parsing and assigning to property.

Question: Can you add some kind of annotation or hint to the JSON converter to process a given value corresponding to the property before trying to parse it?

Why it's not a duplicate: I am not asking how to parse a Date, I am asking about how to process any data before converter attempts to parse it.

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 5
    This where a custom `JsonConverter` can help, by overriding the `ReadJson` method. For further information please check the [official documentation](https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm) – Peter Csala Jun 05 '20 at 06:53
  • Does this answer your question? [How to parse given string to DateTime?](https://stackoverflow.com/questions/62194291/how-to-parse-given-string-to-datetime) Didn't you ask the same question already? – Pavel Anikhouski Jun 05 '20 at 08:03
  • @PavelAnikhouski It doesn't, I am not asking how to parse a Date I am asking about how to process data before converter attempts to parse it. – Yoda Jun 05 '20 at 08:21
  • The purpose of a `JsonConverter` is to handle part of the JSON in a custom way. There is not a way to do something custom *before* a converter because a converter is the way to do custom things. So if you want to do special parsing on a string to convert it to a date, you need to make your own `JsonConverter` to do that. That said, Json.Net does attempt to parse certain strings that look like dates before the converter gets them, so you may need to turn that off. See [DateTimeStringConverter gets an already DateTime converted object in ReadJson()](https://stackoverflow.com/q/53508910/10263). – Brian Rogers Jun 05 '20 at 15:27
  • *Can you add some kind of annotation or hint to the JSON converter to process a given value corresponding to the property before trying to parse it?* - yes, you can add `[JsonConverter(typeof(StringConverter))]` to your surrogate `string` property as shown in [this answer](https://stackoverflow.com/a/61140733/3744182) to [How can I deserialize a json object into a json string?](https://stackoverflow.com/q/61138752/3744182). Then add your custom heuristic logic to `StringConverter.ReadJson()`. – dbc Jun 05 '20 at 15:43
  • 2
    Or if you prefer to skip the surrogate string property, you can add a custom converter `MyDateTimeConverter` inheriting from `IsoDateTimeConverter` or `DateTimeConverterBase` by adding `[JsonConverter(typeof(MyDateTimeConverter))]` to the `DateTime` property as shown [here](https://stackoverflow.com/a/44893493/3744182) or [here](https://stackoverflow.com/a/40257902/3744182). – dbc Jun 05 '20 at 15:46

0 Answers0