5

While migrating code from newtonsoft json to system.text.json

I need all nullable strings to render as empty string.

I wrote following converter but all null string values are still rendered as null.

And for null string values, Write method is not called. Break point is never hit.

public class EmptyStringConverter : JsonConverter<string>
{
    public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        => Convert.ToString(reader.GetString(), CultureInfo.CurrentCulture);

    public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
    {
        if (writer == null)
            throw new ArgumentNullException(nameof(writer));
        writer.WriteStringValue(value ?? "");
    }
}

Startup code

services.AddControllers()
    .AddJsonOptions(option =>
    {
        option.JsonSerializerOptions.Converters.Add(new EmptyStringConverter());
    });

Console Example

class Program
{
    static void Main(string[] args)
    {
        var jsonSerializerOptions = new JsonSerializerOptions();
        jsonSerializerOptions.Converters.Add(new EmptyStringConverter());
        var json = JsonSerializer.Serialize(new Model() { FirstName = null }, jsonSerializerOptions);
    }
}

public class EmptyStringConverter : JsonConverter<string>
{
    public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        => Convert.ToString(reader.GetString(), CultureInfo.CurrentCulture);

    public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
    {
        if (writer == null)
            throw new ArgumentNullException(nameof(writer));
        writer.WriteStringValue(value ?? "");
    }
}

public class Model
{
    public string FirstName { get; set; }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Khalil
  • 1,047
  • 4
  • 17
  • 34
  • 1
    This seems to be the design intent. In [`System.Text.Json.JsonPropertyInfoNotNullable.OnWrite(WriteStackFrame& current, Utf8JsonWriter writer)`](https://github.com/dotnet/runtime/blob/7c6ec46a637017971d3eaa210fd2b6e4d4a6b737/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoNotNullable.cs#L62), if `value == null`, a `null` is written **and the custom converter is not called**. – dbc Jan 20 '20 at 21:50
  • This is consistent with Json.NET which also never calls `JsonConverter.WriteJson()` with null, see [How to force JsonConverter.WriteJson() to be called for a null value](https://stackoverflow.com/q/52518593). However, with Json.NET you can use a custom contract resolver to replace null values with default values, see [Json Convert empty string instead of null](https://stackoverflow.com/q/23830206). – dbc Jan 20 '20 at 21:52
  • But unfortunately you can't do that in `System.Text.Json`. The equivalent types in `System.Text.Json` -- [`JsonClassInfo`](https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.cs) and [`JsonPropertyInfo`](https://github.com/dotnet/corefx/blob/master/src/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs) -- are **internal**. There is an open enhancement [Equivalent of DefaultContractResolver in System.Text.Json #42001](https://github.com/dotnet/corefx/issues/42001) asking for a public equivalent. – dbc Jan 20 '20 at 21:52
  • Should I make that an answer? – dbc Jan 20 '20 at 21:54
  • Yes, This can be marked as answer. – Khalil Jan 21 '20 at 11:03

2 Answers2

9

In .NET 5.0 this can be done by overriding JsonConverter<T>.HandleNull and returning true:

public class EmptyStringConverter : JsonConverter<string>
{
    public override bool HandleNull => true;
    
    public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        => reader.TokenType == JsonTokenType.Null ? "" : reader.GetString();

    public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) =>
        writer.WriteStringValue(value ?? "");
}

For more, see Handle null values.

Demo fiddle here.

In .NET Core 3.x this is not implemented. From Handle null values in .NET Core 3.x:

Handle null values

By default, the serializer handles null values as follows:

  • For reference types and Nullable<T> types:

    • It does not pass null to custom converters on serialization.
    • It does not pass JsonTokenType.Null to custom converters on deserialization.
    • It returns a null instance on deserialization.
    • It writes null directly with the writer on serialization.
  • For non-nullable value types:

    • It passes JsonTokenType.Null to custom converters on deserialization. (If no custom converter is available, a JsonException exception is thrown by the internal converter for the type.)

This null-handling behavior is primarily to optimize performance by skipping an extra call to the converter. In addition, it avoids forcing converters for nullable types to check for null at the start of every Read and Write method override.

dbc
  • 104,963
  • 20
  • 228
  • 340
0

Try it

/// <summary>
/// Convert empty to null when read data json
/// </summary>
public class EmptyStringToNullConverter : JsonConverter<string>
{
    /// <summary>
    /// Override CanConvert method of JsonConverter
    /// This instance only convert the string type.
    /// </summary>
    /// <returns></returns>
    public override bool CanConvert(Type typeToConvert)
    {
        return typeToConvert == typeof(string);
    }

    /// <summary>
    /// Override ReadJson method of JsonConverter
    /// Convert string null to empty
    /// </summary>
    /// <returns></returns>
    public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        string value = (string)reader.GetString();
        return value ?? String.Empty;
    }

    /// <summary>
    /// Override WriteJson method of JsonConverter
    /// </summary>
    public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
    {
        throw new NotImplementedException("Unnecessary");
    }
}