Skip to content

KeyValuePair serialization doesn't follow camel case option in System.Text.Json #44385

@3rdey3

Description

@3rdey3

Description

When serializing object of KeyValuePair the serializer doesn't take JsonNamingPolicy.CamelCase option into consideration. The following example code tries to depict the situation.

class Program
{
    static void Main(string[] args)
    {
        var opt = new JsonSerializerOptions()
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase
        };

        var kvp = new KeyValuePair<string, string>("one", "1");
        Console.WriteLine("Without Option: " + JsonSerializer.Serialize(kvp));
        Console.WriteLine("With Option: " + JsonSerializer.Serialize(kvp, opt));
    }
}

Result:

Without Option: {"Key":"one","Value":"1"}
With Option: {"Key":"one","Value":"1"}    

Expected Result:

Without Option: {"Key":"one","Value":"1"}
With Option: {"key":"one","value":"1"}    

Configuration

.NET Version: Core 3.1
OS: Windows 10 20H2
Architecture: x64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions