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.
Background and motivation
ReferenceHandler.Preserve
is very useful when references to the same object need to be sent. However, it easily breaks, for example withTuple<>
, 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
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 byObjectDefaultConverter
.