-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Open
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Jsonpartner-impactThis issue impacts a partner who needs to be kept updatedThis issue impacts a partner who needs to be kept updated
Milestone
Description
Motivation
System.Text.Json supports user-defined naming policies for properties/fields which can be specified via the JsonSerializerOptions.PropertyNamingPolicy. Today such policies can only be specified globally which removes the ability to apply granular policies on the individual type/property/field level.
API Proposal
namespace System.Text.Json.Serialization;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class JsonNamingPolicyAttribute : JsonAttribute
{
public JsonNamingPolicy(JsonKnownNamingPolicy namingPolicy);
protected JsonNamingPolicy(JsonNamingPolicy namingPolicy); // protected ctor for user-defined extensibility
public JsonNamingPolicy NamingPolicy { get; }
}API Usage
JsonSerializerOptions options = new() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower };
JsonSerializer.Serialize(new MyPoco(), options); // { "myFirstProperty" : "first", "my-second-property": "second" }
[JsonNamingPolicy(JsonKnownNamingPolicy.CamelCase)]
public class MyPoco
{
public string MyFirstProperty { get; set; } = "first";
[JsonNamingPolicy(JsonKnownNamingPolicy.KebabCaseLower)]
public string MySecondProperty { get; set; } = "second";
}cc @stephentoub
martincostello, basvanharten, cvocvo, ms-mayya, cplankl and 8 more
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Jsonpartner-impactThis issue impacts a partner who needs to be kept updatedThis issue impacts a partner who needs to be kept updated