Nenad
Nenad

Reputation: 26727

ASP.NET Core MVC - empty string to null when sending JSON to server

When posting input data as a FormData to the ASP.NET Core MVC controller, by default empty string values are coerced to null values.

However, when sending input data as JSON to the controller, empty string values remain as they are. This leads to different behavior when validating string properties. For example, description field is not bound to null, but to empty string on the server:

{
    value: 1,
    description: ""
}

This in turn makes following model invalid, even though Description is not required:

public class Item
{
    public int Value { get; set; }

    [StringLength(50, MinimumLength = 3)]
    public string Description { get; set; }
}

This is contrary to the behavior when same data is submitted via form.

Is there a way to make model binding of JSON behave in the same way as model binding of form data (empty string coerce to null by default)?

Upvotes: 3

Views: 4231

Answers (1)

Nenad
Nenad

Reputation: 26727

After going through source code of ASP.NET Core MVC (v2.1) and source code of Newtonsoft.Json (v11.0.2), I came up with following solution.

First, create custom JsonConverter:

public class EmptyStringToNullJsonConverter : JsonConverter
{
    public override bool CanRead => true;
    public override bool CanWrite => false;

    public override bool CanConvert(Type objectType)
    {
        return typeof(string) == objectType;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        string value = (string)reader.Value;
        return string.IsNullOrWhiteSpace(value) ? null : value.Trim();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException("Unnecessary because CanWrite is false. The type will skip the converter.");
    }
}

Then, register custom converter globally:

services
    .AddMvc(.....)
    .AddJsonOptions(options => options.SerializerSettings.Converters.Add(new EmptyStringToNullJsonConverter()))

Or, use it on per-property bases via JsonConverterAttribute. For example:

public class Item
{
    public int Value { get; set; }

    [StringLength(50, MinimumLength = 3)]
    [JsonConverter(typeof(EmptyStringToNullJsonConverter))]
    public string Description { get; set; }
}

Upvotes: 7

Related Questions