-
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
Reuse JsonSerializerOptions when used with JsonSerializerContext #64025
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionWe currently have an extension method to encapsulate the serialization/deserialization logic, this is just basically because we have a custom converter that serialize enums as strings, we have a private static field of JsonSerializerOptions that we reuse when we call JsonSerializer.Serialize to avoid generating a new object in every call. We are testing the source generator in System.Text.Json and tried to do the same, but we got the exception: The call is basically this: Is there a way to avoid creating a new instance of JsonSerializerOptions in every call? or is it something wrong on how we are approaching this? Configuration.NET SDK 6.0.101
|
Can you cache & reuse the context instance that you create, similar to how you're already caching your custom options? StaticJsonSerializerContext = new MyContext(StaticJsonSerializerOptions);
...
JsonSerializer.Serialize(myObject, typeof(MyObjectDto), StaticJsonSerializerContext); |
This issue has been marked |
It does work, but it needs a new instance of JsonSerializerOptions per JsonSerializerContext (I just found out that i can reuse the context with multiple types and have just one JsonSerializerContext), and our benchmarks shows that the performance is almost identical to the reflection based serialization (in speed and memory allocation) so we will use the current solution at the moment. Thanks a lot for your time @layomia |
@JuanZamudioGBM thanks for your feedback. Could you share a repro of your benchmarks so we can see why there are no perf improvements for serialization in your scenario? Note that there are no expected throughput improvements for deserialization with the current source-gen implementation. |
This issue has been marked |
Let me prepare a repro and upload it to a public account. |
Here is the repro, I hope its useful. |
With the inclusion of #63686 in .NET 7 it should be possible to assign existing IJsonTypeInfoResolver combinedResolver = JsonTypeInfoResolver.Combine(MyContext1.Default, MyContext2.Default, MyContext3.Default);
var options = new JsonSerializerOptions { TypeInfoResolver = combinedResolver }; The new options instance will be combining source generated metadata from all three sources. Per @layomia's earlier comment, using the metadata-based source generator is not expected to produce any substantial performance gains compared to reflection. The purpose of the metadata-based source generator is to minimize static initialization time and support application trimming. |
Description
We currently have an extension method to encapsulate the serialization/deserialization logic, this is just basically because we have a custom converter that serialize enums as strings, we have a private static field of JsonSerializerOptions that we reuse when we call JsonSerializer.Serialize to avoid generating a new object in every call.
We are testing the source generator in System.Text.Json and tried to do the same, but we got the exception:
System.InvalidOperationException: "The specified 'JsonSerializerOptions' instance is already bound with a 'JsonSerializerContext' instance."
The call is basically this:
JsonSerializer.Serialize(myObject, typeof(MyObjectDto), new MyContext(StaticJsonSerializerOptions));
Is there a way to avoid creating a new instance of JsonSerializerOptions in every call? or is it something wrong on how we are approaching this?
Configuration
.NET SDK 6.0.101
The text was updated successfully, but these errors were encountered: