Runtime: System.Text.Json DictionaryKeyPolicy is ignored if entry value is null

Created on 30 Oct 2019  路  7Comments  路  Source: dotnet/runtime

When setting DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;, only non-null value entries have their keys properly converted.

I believe it is because the key is only converted inside the else branch here.

I assume this is a bug, at least I cannot imagine this would be an expected and desired behavior.

area-System.Text.Json

Most helpful comment

@ericstj right there is no official support for ExpandoObject yet. However, I expect either an UnsupportedException.

Or if processed without any error, it should generate homogeneous JSON according to the configuration.

All 7 comments

cc @layomia. Looks like when DictionaryKeyPolicy support was added, it only was added to the non-null value case.

I just ran into this as well. Makes for really inconsistent behaviour when trying to use the a Json result from asp.net core app because the Json object might be the expected case in instance and a different casing in another.

Fixed in 3.1. Thanks for the report!

That was quick! Was considering to make this my own first contribution to the repo, but you've nailed it, people!

unfortunately, this issue still exists in 3.1.101 @ericstj

I have a simple POCO holding a dynamic property. If the most inner property has a value of null, JsonNamingPolicy.CamelCase is ignored:

var foo = new Poco
            {
                Name = "Hello NETCORE",
                Inner = new ExpandoObject()
            };

            foo.Inner.FieldA = null;
            foo.Inner.FieldB = "Field B Valu";
            foo.Inner.FieldC = false;

            var options = new JsonSerializerOptions
            {
                WriteIndented = true,
                DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };
            Console.WriteLine(JsonSerializer.Serialize(foo, options));

Which returns

{
  "name": "Hello NETCORE",
  "inner": {
    "FieldA": null,
    "fieldB": "Field B Valu",
    "fieldC": false
  }
}

in contrast to the expected result

{
  "name": "Hello NETCORE",
  "inner": {
    "fieldA": null,
    "fieldB": "Field B Valu",
    "fieldC": false
  }
}

csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
</Project>

sdk is version 3.1.101

https://github.com/dotnet/corefx/pull/42284 added the fix to this original issue. @ThorstenHans It seems like this issue may be different. Could be related to https://github.com/dotnet/runtime/issues/29690. I don鈥檛 think we have support for ExpandoObject yet. Cc @layomia to double check.

@ericstj right there is no official support for ExpandoObject yet. However, I expect either an UnsupportedException.

Or if processed without any error, it should generate homogeneous JSON according to the configuration.

Was this page helpful?
0 / 5 - 0 ratings