-
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
JsonNamingPolicy.Default internal property should be made public #30986
Comments
the request for change is for |
Note: JsonDefaultNamingPolicy is internal, too. |
I don't understand the question. Can you explain your use case/scenario, preferably with a code sample, where you'd use this to help me understand why the current behavior doesn't work for you? For api change proposals like these, it would be good to follow the template (with rationale and sample usage): If you don't specify a naming policy in the Why does this need to be public? What would you use this for? |
@ahsonkhan I'm not sure if this is @VassilAtanasov's need, but how would you go about selecting a default naming policy? Currently in JSON.NET:
|
Overriding the "default" naming policy with camel case (or a custom naming policy) can be done in the var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
string str = JsonSerializer.Serialize<Foo>(obj, options); If you want the default policy, just don't set one. // Don't set any naming policy in options, but feel free to set other options that might be needed
var options = new JsonSerializerOptions();
string str = JsonSerializer.Serialize<Foo>(obj, options);
// OR just simply:
string str = JsonSerializer.Serialize<Foo>(obj); So, I don't see why there is a need for the default naming policy to be public? |
@ahsonkhan the question is about setting it globally, not passing in every single time. If the application does a significant amount of serializations / deserializations, I'd like to set it in one place and not have to declare and pass every single time:
|
@btecu, even in the case of using a global, by not setting // Global setting - can be re-used over and over
// Implicitly, we are using JsonNamingPolicy.Default because a custom
// PropertyNamingPolicy wasn't set
var options = new JsonSerializerOptions();
// OR explicitly set the property to null - if for some reason it needs
// to be reset to undo some previous value
options = new JsonSerializerOptions
{
PropertyNamingPolicy = null
}; @steveharter - is there any reason we shouldn't expose the default naming policy? |
@ahsonkhan this is not much better than inline. Now you need some class where you have a random property
|
I understand, but there are by-design differences in the two libraries, and that design has already shipped. I think the concerns you brought up @btecu are unrelated to the issue at hand. @VassilAtanasov, @Zenexer, can you please clarify your requirements and address my comment above: Otherwise, I don't see this issue as being actionable. |
@ahsonkhan I have no requirements; I just find it odd and confusing that the default isn't public. What is that trying to tell me? Should I be avoiding the default? Is there something wrong with the default? Is the default going to change in a future version, meaning that I should write my own implementation? It's unintuitive. In general, throughout .NET, you can explicitly set default values--that is, if a property initializes to |
Fair enough, @Zenexer - thanks for the input. @steveharter - I don't see why the Default property is internal. Do you have any reason to keep it that way (or know why it was internal to begin with)? I am marking this ready for review (otherwise, let me know if you think we shouldn't do this). |
All public settable options by design are instance properties on The "default" internal static naming policy getter does nothing -- just returns the same string. It is an implementation detail for the Enum conversion code. It is not used anywhere else. To clarify, as mentioned earlier, you can set the default on options: var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}; but of course the "options" would need to be passed to every (de)serialize call. However, we did consider a feature (not implemented) where you can essentially set a static variable that defines a callback that returns what options instance is returned (a CallContext\thread variable, session variable, another static, etc), allowing use of the serializer methods that don't take "options" as a parameter. I can add that feature to track this if that is desired. The reason the options are instance-based is because we want to support different options for different use cases and don't want one area of code to "randomly" change behavior when another area of code changes a global static variable. For this reason we also prevent options from being changed once (de)serialization has occurred. Another benefit of preventing options from being changed once (de)serialization has occurred is that it allows us to effectively emit IL or code (for ahead-of-time scenarios) that can be optimized by not having to check to see if global options have changed on every (de)serialization. Closing this issue by design. See above if we should create a new issue to specify where the global options can live (so the options don't need to be passed on every call). |
@steveharter, please that feature (static variable with options) would be really useful. |
I see someone created a similar issue for configuring the default settings, so we will assume that for now: https://github.com/dotnet/corefx/issues/41612 |
Clarification: there is no "default" naming policy -- just a lack of a naming policy which is represented by a The lack of a naming policy just means the JSON will use the names of the POCO properties as-is (which are typically Pascal-cased). |
In aspnetcore |
@msmolka Set the property to |
Why this is internal? Developers need to be able to use it pass through PascalCase
Originally posted by @VassilAtanasov in dotnet/corefx#41354
The text was updated successfully, but these errors were encountered: