-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add property JsonNamingPolicy.ConvertName() overload with Type parameter #48396
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsBackground and MotivationDuring migration from We had special naming policy for properties of specific types. In our case we were adding type-specific prefix to property names of some types (they are complex types with custom converters) which were used by consumer (javascript app in our case) to apply deserialization logic based on property prefix. Problem with System.Text.Json is that Only workaround I found is manually annotate all affected properties with Proposed APInamespace System.Text.Json
{
public abstract class JsonNamingPolicy {
+ public virtual string ConvertName(string name, Type type);
}
} Proposed method implementation (should be called instead of current public virtual string ConvertName(string name, Type type) => ConvertName(name); Usage Examplespublic class MyCustomNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name, Type type)
{
var name = ConvertName(name);
// or
//var name = JsonNamingPolicy.CamelCase.ConvertName(name);
return type == typeof(PerfixedType) : "prefix_" + name : name;
}
public override string ConvertName(string name) => JsonNamingPolicy.CamelCase.ConvertName(name);
} Alternative DesignsI think it is most straightforward one. And much-much cleaner than implementation we used with RisksNone? Minor performance hit on extra call with extra parameter shouldn't be a problem as results of property name generation are cached and reused.
|
I'm not very familiar with the Json.NET feature, but wouldn't there also need to be a corresponding construct for deserialization? |
This proposal is about richer context for json property name generation logic. Don't think it has anything with de/serialization. |
Out of curiosity, how were you able to achieve the same result in Json.NET? At quick glance the equivalent construct seems to be the |
|
In .NET 6 we are providing an
|
Background and Motivation
During migration from
Newtonsoft.Json
we discovered following unsupported scenario.We had special naming policy for properties of specific types. In our case we were adding type-specific prefix to property names of some types (they are complex types with custom converters) which were used by consumer (javascript app in our case) to apply deserialization logic based on property prefix.
Problem with System.Text.Json is that
JsonNamingPolicy.ConvertName
method accepts only property name.I suggest to add additional overload with
Type
parameter (all call sites already have type information for it).Only workaround I found is manually annotate all affected properties with
JsonPropertyName
attribute, which is quite unpleasant way to fix it 😞Proposed API
namespace System.Text.Json { public abstract class JsonNamingPolicy { + public virtual string ConvertName(string name, Type type); } }
Proposed method implementation (should be called instead of current
ConvertName
by consumers):Usage Examples
Alternative Designs
I think it is most straightforward one. And much-much cleaner than implementation we used with
Newtonsoft.Json
.Risks
None? Minor performance hit on extra call with extra parameter shouldn't be a problem as results of property name generation are cached and reused.
The text was updated successfully, but these errors were encountered: