Skip to content
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.Json reference handling allow per-type customization #110048

Open
cpiber opened this issue Nov 21, 2024 · 1 comment
Open
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Milestone

Comments

@cpiber
Copy link

cpiber commented Nov 21, 2024

Background and motivation

ReferenceHandler.Preserve is very useful when references to the same object need to be sent. However, it easily breaks, for example with Tuple<>, since it does not have a constructor of the expected form.

It would be useful, if the user could opt-in to reference preservation only for specific types. I read a little through the implementation, and it seems like JsonConverter.CanHaveMetadata could already do what I need, but it is marked as internal and thus cannot be influenced by the user.

API Proposal

API Usage

class MyReferenceConverter : ObjectConverterFactory {
  public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) {
    var converter = base.CreateConverter(typeToConvert, options);
    converter.CanHaveMetadata = typeToConvert != typeof(Tuple<>);
    return converter;
  }
}

Alternative Designs

No response

Risks

The property setter should probably only allow setting to false, as reference preserving on value types is not supported, and the converter must already support writing metadata, as implemented by ObjectDefaultConverter.

@cpiber cpiber added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 21, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 21, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis eiriktsarpalis added this to the Future milestone Nov 21, 2024
@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

2 participants