1

I receive a JSON text from an API call, and one of the properties can have a string value that sometimes equals to false, this is where the problem begins. Since the value type must be ExternalDownload, when I get false value I need to make the ExternalDownload equals to null.
JSON example
http://coub.com/api/v2/coubs/20971754
Data model

public partial class ExternalDownload
{
    [JsonProperty("type")]
    public DownloadType Type { get; set; }

    [JsonProperty("url")]
    public Uri Url { get; set; }
}

Enum

public enum DownloadType
{
    Youtube,
    Vimeo,
    Vk,
    Instagram,
    Vine,
    Wimp,
    Facebook,
    Odnoklassniki,
    Funnyordie,
    Carambatv,
    CollegeHumor,
    LiveLeak,
    Dailymotion,
    TetTv
}

Exception

Newtonsoft.Json.JsonSerializationException: 'Error converting value False to type 'Coub.Net.Objects.ExternalDownload'. Path 'external_download', line 1, position 3093.'

ArgumentException: Could not cast or convert from System.Boolean to Coub.Net.Objects.ExternalDownload.

Noel Nemeth
  • 646
  • 11
  • 21
  • Have you tried a [custom Json converter](https://www.newtonsoft.com/json/help/html/CustomJsonConverterGeneric.htm)? – Anderson Matos Oct 05 '18 at 17:38
  • @AndersonMatos Not yet, but I try now. – Noel Nemeth Oct 05 '18 at 17:42
  • Can you share a [mcve] that includes the JSON you are trying to deserialize, and your target c# model? Is the JSON value `"False"` or `false` (without quotes)? If it's a string then `EnumMember` should just work, but if it's a JSON Boolean primitive (see http://json.org/ for details on the difference) then you will need to do something manual. – dbc Oct 05 '18 at 17:43
  • @dbc I checked it again and it is `false` and not `"false"`, now I understand the problem. – Noel Nemeth Oct 05 '18 at 17:51
  • I was wrong, not the `enum` has the `false` value but the `ExternalDownload`. – Noel Nemeth Oct 05 '18 at 18:21
  • @AndersonMatos If you post your comment as an answer I can accept it. – Noel Nemeth Oct 05 '18 at 18:27
  • @NoelNemeth - I'm not sure I understand your problem any more. Are you saying that the value of `ExternalDownload.Type` is `false`, or are you saying that the value of `ExternalDownload` itself is `false`? A sample of the JSON in question would sure help to clarify. – dbc Oct 05 '18 at 18:59
  • @dbc When I first looked at it I thought that `ExternalDownload.Type` would get a false value but later I realized that `ExternalDownload` got a false value. This is why I edited my question. By the way my problem was solved by Custom Converter. – Noel Nemeth Oct 05 '18 at 19:37

2 Answers2

0
public enum Test
{
    [EnumMember(Value = "True")]
    @true,
    @false
}


class Program
{
    static void Main()
    {
        Test a = JsonConvert.DeserializeObject<Test>("\"true\"");
        Console.WriteLine(a); //true
    }
}

It will work if the value is a string. I.E. "false" instead of false.

If its later case then you might have to serialize it as string and then do the conversion

Steve
  • 11,696
  • 7
  • 43
  • 81
0

You can use a custom converter to work with specific types and as a workaround this issue. This will also enable you to provide custom conversion to other types, not just your enum.

Take a look into Newtonsoft.Json docs about this here: https://www.newtonsoft.com/json/help/html/CustomJsonConverterGeneric.htm

Bellow is a sample (from their website):

public class VersionConverter : JsonConverter<Version>
{
    public override void WriteJson(JsonWriter writer, Version value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }

    public override Version ReadJson(JsonReader reader, Type objectType, Version existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        string s = (string)reader.Value;

        return new Version(s);
    }
}

You might use it directly on your code, on JsonConvert.SerializeObject and JsonConvert.DeserializeObject overrides that receive the custom converter, or you can register it globally, as shown on this other SO answer: Registering a custom JsonConverter globally in Json.Net

Anderson Matos
  • 3,132
  • 1
  • 23
  • 33