Skip to content

[System.Text.Json] Add JsonPascalCaseNamingPolicy. #34114

@leopripos

Description

@leopripos

EDIT We have an implementation provided by #100017, so might as well expedite an API review --@eiriktsarpalis

namespace System.Text.Json;

public abstract class JsonNamingPolicy
{
        public static JsonNamingPolicy CamelCase { get; }
        public static JsonNamingPolicy KebabCaseLower { get; }
        public static JsonNamingPolicy KebabCaseUpper { get; }
        public static JsonNamingPolicy SnakeCaseLower { get; }
        public static JsonNamingPolicy SnakeCaseUpper { get; }
+       public static JsonNamingPolicy PascalCase { get; }
}

namespace System.Text.JsonSerialization;

public enum JsonKnownNamingPolicy
{
    Unspecified = 0,
    CamelCase = 1,
    SnakeCaseLower = 2,
    SnakeCaseUpper = 3,
    KebabCaseLower = 4,
    KebabCaseUpper = 5,
+   PascalCase = 6,
}
Original Proposal Perhaps (correct me if I'm wrong), the recommended naming policy for PascalCase uses default naming policy (set to null) because we write out code in PascalCase.

If the scenario like this: I have data in dictionary, and I want to convert them to pascal case,

var dictionary = new Dictionary<string, string>
{
    { "test", "Test Data" }
};

var json = JsonSerializer.Serialize(dictionary, new JsonSerializerOptions
{
    DictionaryKeyPolicy = null
});
Console.WriteLine(json); // Outputs: {"test":"Test Data"}

Expected: {"Test":"Test Data"}

The output is {"test":"Test Data"}. It will be a bug/ambiguous if we use concept Using default naming policy for PascalCase.

3 scenarios:

  • Just use default (no change) from CamelCase or PascalCase.
  • Convert to PascalCase from CamelCase or PascalCase
  • Convert to CamelCase from CamelCase or PascalCase

So, I think we need new JsonPascalCaseNamingPolicy.

Metadata

Metadata

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsonin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions