It happens, third parties can neglect type safety in their JSON. I recommend you contact them. I had a scenario where a property was either a string array or "false". Json.NET didn't like this so as a temporary hack, I created this custom converter to ignore the deserialization exception:
public class IgnoreDataTypeConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try { return JToken.Load(reader).ToObject(objectType); }
catch { }
return objectType.IsValueType ? Activator.CreateInstance(objectType) : null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
This "TryConvert" approach is not advised. Use it as a temporary solution after you send your thoughts to the designers of the originating JSON you are consuming.