ANR Upgraded Version
ANR Upgraded Version

Reputation: 949

Error converting value "null" to type 'System.Nullable`1[System.Int64]'

I have a JSON Object which contains a null as string ,which cannot be avoided , when i'm sending this object to my API , im getting this error saying

Error converting value "null" to type 'System.Nullable`1[System.Int64]'

My Sample JSON

{ 
  Name:'Test',
  Id:'null'
}

My Model / DTO

class Class1{
  public string Name {get;set;}
  public long? Id {get;set;}
}

Upvotes: 1

Views: 9783

Answers (2)

marwaha.ks
marwaha.ks

Reputation: 549

Get the json string. Do string.Replace("'null'", "null") then map it to your dto

Upvotes: 0

ColinM
ColinM

Reputation: 2681

You can achieve this by using a JsonConverter to handle the logic of parsing the string as a long? which can be null.

First example using Newtonsoft.Json

public class NewtonsoftStringToLongJsonConverter : Newtonsoft.Json.JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
    {
        var value = (string)reader.Value;
        if (string.Equals("null", value, StringComparison.InvariantCultureIgnoreCase))
        {
            return null;
        }

        if (!long.TryParse(value, out var parsedValue))
        {
            return null;
        }

        return parsedValue;
    }

    public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

Your model updates to

public class Class1
{
    public string Name { get; set; }

    [Newtonsoft.Json.JsonConverter(typeof(NewtonsoftStringToLongJsonConverter))]
    public long? Id { get; set; }
}

The second option using the System.Text.Json implementation.

public class SystemTextStringToLongJsonConverter : System.Text.Json.Serialization.JsonConverter<long?>
{
    public override bool CanConvert(Type typeToConvert)
        => typeToConvert == typeof(long?);

    public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        var value = reader.GetString();
        if(string.Equals("null", value, StringComparison.InvariantCultureIgnoreCase))
        {
            return null;
        }

        if(!long.TryParse(value, out var parsedValue))
        {
            return null;
        }

        return parsedValue;
    }

    public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }
}

Finally, your model updates to

public class Class1
{
    public string Name { get; set; }

    [Newtonsoft.Json.JsonConverter(typeof(NewtonsoftStringToLongJsonConverter))]
    public long? Id { get; set; }
}

Upvotes: 2

Related Questions