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.

8
  • Have you tried a custom Json converter? Commented Oct 5, 2018 at 17:38
  • @AndersonMatos Not yet, but I try now. Commented Oct 5, 2018 at 17:42
  • Can you share a minimal reproducible example 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 json.org for details on the difference) then you will need to do something manual. Commented Oct 5, 2018 at 17:43
  • @dbc I checked it again and it is false and not "false", now I understand the problem. Commented Oct 5, 2018 at 17:51
  • I was wrong, not the enum has the false value but the ExternalDownload. Commented Oct 5, 2018 at 18:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.