3

I'm using Json.NET in a Web API and I have a request class that looks like this:

public class Model
{
    [JsonProperty("id", Required = Required.Always)]
    public string Id { get; set; }

    [JsonProperty("value", Required = Required.AllowNull)]
    public JRaw Value { get; set; }
}

I want to be able to receive values of different types in the Value property (e.g. string, bool, array[], object, etc.).

I want to manually deserialize the Value property, because I have some custom converters that I want to use for the particular case.

For example, I have a StringDateTimeConverter (which can convert ISO_8601 strings and UNIX timestamps).

Here is how I deserialize the Value property:

object result = JsonConvert.DeserializeObject(model.Value.ToString(), systemType, serailizerSettings);

where systemType is System.DateTime in this particular case and I've added my StringDateTimeConverter to the serializerSettings converters.

The problem is that each time my converter receives a System.DateTime object instead of a string, which means that it still deserializes the Value property.

Does anyone have an idea how to prevent the property from deserializing?

Obviously, I've tried using JRaw, which I guess is primarily used in serialization (not deserialization), and object as types for the Value property.

Note Using a custom converter for the Model class is not an option for me since I need to deserialize the Value property after some server-side logic that chooses the right type of converter to use for the deserialization of the Value property.

stansttr
  • 31
  • 1
  • [JsonIgnoreAttribute](https://www.newtonsoft.com/json/help/html/PropertyJsonIgnore.htm)? – Johnathan Barclay Jan 29 '20 at 15:05
  • [JsonIgnore] would seem the way forwards. Looks similar to https://stackoverflow.com/questions/31731320/serialize-property-but-do-not-deserialize-property-in-json-net – robs Jan 29 '20 at 15:26
  • Possible duplicate of [DateTimeStringConverter gets an already DateTime converted object in ReadJson()](https://stackoverflow.com/q/53508910/10263) – Brian Rogers Jan 29 '20 at 17:07
  • 1
    Dates and times are recognized by `JsonTextReader` not the serializer. To disable this completely set `DateParseHandling = DateParseHandling.None` as shown in [Json.NET Disable the deserialization on DateTime](https://stackoverflow.com/q/11856694/3744182) or [json.net: DateTimeStringConverter gets an already DateTime converted object in ReadJson()](https://stackoverflow.com/a/53509380/3744182). – dbc Jan 29 '20 at 18:05
  • 1
    To disable this at the class level, apply `[JsonConverter(typeof(DateParseHandlingConverter), DateParseHandling.None)]` to `Model` as shown in [How to prevent a single object property from being converted to a DateTime when it is a string](https://stackoverflow.com/a/40644970/3744182). – dbc Jan 29 '20 at 18:05
  • Do either of those options answer your question? – dbc Jan 29 '20 at 18:05
  • @JohnathanBarclay Unfortunately, that's not what I'm looking for. If I mark the property with JsonIgnore it won't receive any value at all (it'll always be null). – stansttr Jan 30 '20 at 08:06
  • @dbc Thank you for your answer, it did solve my problem. I ended up doing a custom converter where I set the DateParseHandling to None, just like in the example you sent. Do you want to post your comment as an answer, so that I can mark it. – stansttr Jan 30 '20 at 08:09
  • 1
    If you used the same converter we could just mark this as a duplicate. You could upvote that answer if it helped. – dbc Jan 30 '20 at 08:12

0 Answers0