-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 a copy constructor to JsonSerializerOptions #30445
Comments
Can you provide a link to where it would be used in practice? What do you end up doing today? Setting/resetting the options after modifying/using it? So are you looking for something like: namespace System.Text.Json
{
public sealed partial class JsonSerializerOptions
{
public JsonSerializerOptions(System.Text.Json.JsonSerializerOptions options) { };
}
} With sample usage: public static object ModifyOptionsCopy(string json, JsonSerializerOptions currentOptions)
{
if (currentOptions.Encoder != JavascriptEncoder.Default)
{
var newOptions = new JsonSerializerOptions (currentOptions);
newOptions.Encoder = JavascriptEncoder.Default;
return JsonSerializer.Deserialize(json, newOptions);
}
return null;
} Would the underlying caches also get copied (i.e. full deep copy)?
Can you expand on what you mean by this? |
It doesn't need to. Users could independently cache the copied instance.
If you add a new property to the serializer options, MVC would need to react to it accordingly. This becomes complicated if the user happens to reference a newer version of the library. Using reflection to copy things over seems like overkill. |
This should be very simple to implement and it could be very helpful since will allow our ASP.NET users to try-out preview features that need to be enabled from the options like Proposed APInamespace System.Text.Json
{
public sealed partial class JsonSerializerOptions
{
public JsonSerializerOptions(JsonSerializerOptions source)
{
/* Copy all options from source to a new JsonSerializerOptions instance. */
}
}
} |
The state on the option instances can diverge after a clone so we need to ensure they don't contaminate eachother by sharing caches (the For the clone, we should only add the converters that were manually added via |
namespace System.Text.Json
{
public sealed partial class JsonSerializerOptions
{
public JsonSerializerOptions(JsonSerializerOptions options)
{
/* Copy all options from source to a new JsonSerializerOptions instance. */
}
}
} |
Also capturing another note: The copied option that is returned should be "unlocked" and the user should be able to modify it until the first (de)serialization occurs (regardless of whether the original option was locked or unlocked). |
(Edit by @jozkee: proposed API is in a comment at #30445 (comment))
MVC has a scenario where it needs to start with an existing options instance, modify a single property on a copy of it (the encoder) and perform serialization. In the absence of a copy constructor, we're at risk of missing out on newly added properties to the type.
Proposed API
The text was updated successfully, but these errors were encountered: