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.Text.JsonSerializerContext should support deserialization to private members. #95385

Closed
voroninp opened this issue Nov 29, 2023 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json

Comments

@voroninp
Copy link

Background and motivation

Reflection-based deserialization has no problems with accessing properties with private setters when [JsonInclude] is applied.
Yet JsonSerializerContext throws an exception in this case.

Why cannot source generator [UnsafeAccessor] to write to the property?

public sealed record Foo
{
    public int Bar { get; private set; }
}

public static class BreakIntoFoo
{
    [UnsafeAccessor(UnsafeAccessorKind.Method, Name = $"set_{nameof(Foo.Bar)}")]
    public static extern void SetProp(Foo foo, int value);
}

var foo = new Foo();
BreakIntoFoo.SetProp(foo, 5);
Console.WriteLine(foo.Bar); // You see 5 here.

API Proposal

Api won't not change at all, the internals should.

API Usage

As usual.

Alternative Designs

No response

Risks

I assume that UnsafeAccessorAttribute is taken into account by the linker when trimming the app. If it's not the case, then this feature won't be trimming firendly.

@voroninp voroninp added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 29, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 29, 2023
@ghost
Copy link

ghost commented Nov 29, 2023

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

Issue Details

Background and motivation

Reflection-based deserialization has no problems with accessing properties with private setters when [JsonInclude] is applied.
Yet JsonSerializerContext throws an exception in this case.

Why cannot source generator [UnsafeAccessor] to write to the property?

public sealed record Foo
{
    public int Bar { get; private set; }
}

public static class BreakIntoFoo
{
    [UnsafeAccessor(UnsafeAccessorKind.Method, Name = $"set_{nameof(Foo.Bar)}")]
    public static extern void SetProp(Foo foo, int value);
}

var foo = new Foo();
BreakIntoFoo.SetProp(foo, 5);
Console.WriteLine(foo.Bar); // You see 5 here.

API Proposal

Api won't not change at all, the internals should.

API Usage

As usual.

Alternative Designs

No response

Risks

I assume that UnsafeAccessorAttribute is taken into account by the linker when trimming the app. If it's not the case, then this feature won't be trimming firendly.

Author: voroninp
Assignees: -
Labels:

api-suggestion, area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

Duplicate of #88519.

Note that we wouldn't be supporting private members by default, only those annotated with the JsonInclude attribute. Also, UnsafeAccessorAttribute is only available in .NET 8 and newer versions, so this would be challenging to support in multi-targeted projects.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 29, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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