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.