-
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
[API Proposal]: System.Text.Json serialization callback context #59892
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsBackground and motivationNewtonsoft.Json allows you to pass context to its JSON serializer via serializer settings that can later be retrieved in the callbacks. This works well enough for objects that need a specific property (like parent clients, status, etc.) to be set as passed value and it works at any depth. Unfortunately, there isn't a way to do that with System.Text.Json apart from using custom converters, that can't have serializer settings to be passed down (and you can't pass the property value to child types that require same property set if settings aren't passed). This can make it harder to move certain projects over to System.Text.Json and if not make it worse in some instances. API Proposalnamespace System.Text.Json.Serialization
{
public interface IJsonOnDeserialized<T>
{
void OnDeserialized(SomeContextType<T> context);
}
} or with the breaking change: namespace System.Text.Json.Serialization
{
public interface IJsonOnDeserialized
{
void OnDeserialized(StreamingContext context);
}
} API UsageJsonSerializerOptions options = new JsonSerializerOptions
{
// ...
Context = new SomeContextType<int>(10),
// ...
}; public class SomeObject : IJsonOnDeserialized<int>
{
public int Status { get; set; }
public void IJsonOnDeserialized.OnDeserialized(SomeContextType<int> context) => Status = context.Value;
// ... Now we can set up methods or anything else with the received context ...
} RisksThis could lead to breaking changes if callback interfaces wouldn't use generic types.
|
This was discussed in the serialization callback API proposal, see #54528 (comment). I understand passing contexts was cut since it would be difficult to support in the current iteration of source generation. cc @steveharter |
This issue has been marked with the When ready to submit an amended proposal, please ensure that the original post in this issue has been updated, following the API proposal template and examples as provided in the guidelines. |
This would likely need to be considered in combination with #36785. |
Background and motivation
Newtonsoft.Json allows you to pass context to its JSON serializer via serializer settings that can later be retrieved in the callbacks. This works well enough for objects that need a specific property (like parent clients, status, etc.) to be set as passed value and it works at any depth. Unfortunately, there isn't a way to do that with System.Text.Json apart from using custom converters, that can't have serializer settings to be passed down (and you can't pass the property value to child types that require same property set if settings aren't passed). This can make it harder to move certain projects over to System.Text.Json and if not make it worse in some instances.
API Proposal
or with the breaking change:
API Usage
Risks
This could lead to breaking changes if callback interfaces wouldn't use generic types.
The text was updated successfully, but these errors were encountered: