-
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]: Provide APIs to create JsonPropertyInfo based on PropertyInfo #78711
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationThe latests System.Text.Json APIs finally allow me to use Interfaces for deserialization since I'm able to control class to be used etc.. Creating my own TypeInfoResolver based on If I'm not mistaken, most of the magic happens in API ProposalCreate a public API that offers these functionalities (e.g. by making this class public available?). API Usagepublic class MyTypeInfoResolver : DefaultJsonTypeInfoResolver
{
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
if (type.IsInterface)
{
var result = JsonTypeInfo.CreateJsonTypeInfo(type, options);
result.CreateObject = () => new CAA();
foreach (var p in type.GetProperties())
{
//Allow me to add properties here based on `System.Reflection.PropertyInfo`
}
return result;
}
return base.GetTypeInfo(type, options);
}
} Alternative DesignsNo response RisksNo response
|
I might not be understanding the request well enough but doesn't adding entries to the |
Also, per the issue title and to raise the visibility of the method in the doc Eirik linked: are you aware of |
This issue has been marked |
Yes, I know JsonTypeInfo.CreateJsonPropertyInfo, but thank you for mentioning it! Is there a reason it is not a static method? I was asking myself when I found it. Never the less, I'd like to have an easy API allowing me to generate a |
We might consider offering that as an accelerator method, but generally speaking it should pretty straightforward to write one yourself: public static JsonPropertyInfo CreateJsonPropertyInfo(this JsonTypeInfo jsonTypeInfo, PropertyInfo propertyInfo)
{
// NB does not map any attribute data like JsonConverterAttribute or JsonPropertyName
JsonPropertyInfo jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(propertyInfo.PropertyType, propertyInfo.Name);
jsonPropertyInfo.AttributeProvider = propertyInfo;
if (propertyInfo.CanRead)
jsonPropertyInfo.Get = propertyInfo.GetValue;
if (propertyInfo.CanWrite)
jsonPropertyInfo.Set = propertyInfo.SetValue;
return jsonPropertyInfo;
} |
@eiriktsarpalis Yeah, sure. We're speaking about micro optimization here. But imho this extension method could / should be used by the reflector JsonPropertyInfo internally, too. Corresponding it should be an implementation supporting attributes etc. Optionally configurable. |
The reason CreateJsonPropertyInfo is not static is that we want it to be possible to create Please update your proposal with specific API addition and we can consider that depending on the demand. Generally speaking creating properties from scratch most of the time should not be necessary because of the modifiers and different knobs in the options and that's why we didn't particularly focus on adding utilities. Can you describe your scenario more why it needs to add properties from scratch and it can't re-use what was added by default? |
I've implemented an API where for in- and outgoing objects only interfaces are used. For a long time I was forced to use Newtonsoft.JSON, with S.T.J 7.0 I'm finally able to use interfaces (btw, thank you for this new, awesome API!). But since Interfaces do not inherit properties the classical way, I need to loop through the interfaces and add their properties to serialization manually. That is where I'm forced to add the properties myself. (1) Furthermore I'm adding my own OutputFormatters (based on Is one of those use-cases interesting for you to get respected?
please let me know, so I would create a new API Proposal and close this discussion. |
I'm guessing you mean this issue? #41749 |
@eiriktsarpalis at least this issue is pretty related, yes. But imho STJ isn't the library that needs to handle this. Guess we'll end up with the discussion, why .Net doesn't think inherited interface properties belong to the interface (just like inherited class properties does). Even though I never found a good reason for it, that is the way .Net handles this and changing it would be quite more than just a breaking change. I think the use-case is pretty advanced / rare and interface serialization should be handled manually anyway. Otherwise people could complain that the serializer writes properties, typeof(IObject).GetProperties() doesn't provide. Staying with .Net's behavior is just right I guess. Never the less, a common code base to generate properties (JsonPropertyInfo) based on PropertyInfos would be great. And what do you think of the SystemTextJsonFormatters being able to change the SerializerSettings context based? I could write a pretty simple PR containing this possibility. Wouldn't be a breaking change and allow even more customizations I think. I'd create an abstract If you think this use-case is too rare I'd stay with my solution implementing my very own Formatter that copies just the default ones code. |
Background and motivation
The latests System.Text.Json APIs finally allow me to use Interfaces for deserialization since I'm able to control class to be used etc.. Creating my own TypeInfoResolver based on
DefaultJsonTypeInfoResolver
makes it now pretty hard to popule my own PropertyCollection since the default resolver uses pretty much internal APIs to generate it's property collection.If I'm not mistaken, most of the magic happens in
ReflectionJsonTypeInfo
(internal sealed class).API Proposal
Create a public API that offers these functionalities (e.g. by making this class public available?).
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: