skip to Main Content

Trying to convert an old ASP.NET Core project from Newtonsoft.Json to System.Text.Json but it changes the behavior of nullable reference type conversion. System.Text.Json serializes/deserialize nullable reference types to "{}" as opposed to "null". How do I fix it? I want to get rid of Newtonsoft.Json.

services.AddMvc().AddNewtonsoftJson();

Here is the type for that List public IList<IDictionary<string, object>> List { get; set; }. The actual value type is DBNull.

System.Text.Json

{
    "data": {
        "list": [
            {
                "headers": [],
                "list": [
                    {
                        "id": "3faab29e-982d-ccb9-197b-08db24ef09e7",
                        "contractType": "Basis",
                        "tEvent": "Edit",
                        "Basis": 0.0000,
                        "fees": {},
                        "FlatPrice": {},
                        "theirContract": {}
                    },

Newtonsoft.Json

{
    "data": {
        "list": [
            {
                "headers": [],
                "list": [
                    {
                        "id": "3faab29e-982d-ccb9-197b-08db24ef09e7",
                        "contractType": "Basis",
                        "tEvent": "Edit",
                        "Basis": 0.0000,
                        "fees": null,
                        "FlatPrice": null,
                        "theirContract": null
                    },

2

Answers


  1. As stated here there is not built-in support for DBNull.

    You have to use a custom converter:

    class DBNullConverter : System.Text.Json.Serialization.JsonConverter<DBNull>
    {
        public override DBNull Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            throw new NotSupportedException();           
        }
    
        public override void Write(Utf8JsonWriter writer, DBNull value, JsonSerializerOptions options)
        {
            writer.WriteNullValue();
        }
    }
    

    Then add it to the options:

    var serializeOptions = new JsonSerializerOptions
    {
        Converters =
        {
            new DBNullConverter()
        }
    }
    
    Login or Signup to reply.
  2. As at the time of writing, System.Text.Json does not have built-in support for DBNull.

    Instead, you can write a custom JsonConverter to handle DBNull values, by overriding the converter’s Write() method to print out the JSON literal null instead:

    public class DBNullJsonConverter : JsonConverter<DBNull> 
    { 
        public override bool CanConvert(Type typeToConvert) 
        { 
            return typeToConvert == typeof(DBNull); 
        } 
     
        public override DBNull Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) 
        { 
            throw new NotImplementedException(); 
        } 
     
        public override void Write(Utf8JsonWriter writer, DBNull value, JsonSerializerOptions options) 
        { 
            // write the JSON literal null
            writer.WriteNullValue(); 
        } 
    }
    

    Then, pass this converter to the JsonSerializerOptions of your JsonSerializer:

    var options = new JsonSerializerOptions();
    options.Converters.Add(new DBNullJsonConverter());
    
    // you should get null for fields of type DBNull here
    var json = JsonSerializer.Serialize(someObject, options);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search