Skip to content

Should we remove DynamicallyAccessedMembers attributes on JsonSerializer? #52268

Closed

Description

@eerhardt

In 5.0, we annotated the JsonSerializer APIs with DynamicallyAccessedMembers attributes in #38595.

[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
public static TValue? Deserialize<[DynamicallyAccessedMembers(JsonHelpers.MembersAccessedOnRead)] TValue>(string json, JsonSerializerOptions? options = null)

[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
public static string Serialize<[DynamicallyAccessedMembers(MembersAccessedOnWrite)] TValue>(TValue value, JsonSerializerOptions? options = null)

In 6.0, we resolved all the ILLink warnings in System.Text.Json in #51886, which caused us to also add the RequiresUnreferencedCode attributes. This is because the trim annotations can't model everything that needs to be preserved to make these APIs trim compatible. For example, see dotnet/linker#1087.

Having both sets of attributes on these APIs isn't really ideal. The DynamicallyAccessedMembers attributes will only preserve members on the top-level type. So having the DynamicallyAccessedMembers gives a false sense of hope that this API will work with trimming, when in reality it won't.

The small benefit it does provide is in the case where the user is not serializing a graph, but just a class with primitive properties. However, even in this case, we'd like the user to move to use the source generator instead. So keeping these attributes for this tiny case doesn't seem to outweigh the benefit of the encouragement to move to using the source generator.

We also didn't annotate the DataContractSerializer and XmlSerializer APIs like this. We just marked the unsafe APIs with RequiresUnreferencedCode.

However, if we have plans for making the non-source generation Json serializer APIs work with trimming in 6.0, it might not be worth the effort of removing these attributes now, just to add them back in the near future.

cc @vitek-karas @agocke @layomia @steveharter

Activity

  1. ghost added
    untriaged
    on May 4, 2021
  2. vitek-karas commented on May 5, 2021

    @vitek-karas
    Member

    I think we should remove them - as mentioned they are not enough and won't help in most cases anyway.

    However, if we have plans for making the non-source generation Json serializer APIs work with trimming in 6.0, it might not be worth the effort of removing these attributes now, just to add them back in the near future.

    If we do come up with a solution, I doubt it will be using the exact same annotations, so we would have to change this anyway. I don't mind revisiting this code again, if we have a reason to do so.

  3. eerhardt commented on May 5, 2021

    @eerhardt
    MemberAuthor

    Just an FYI - we have trimming tests for these attributes:

    using (var stream = new MemoryStream())
    {
    MyStruct obj = default;
    await JsonSerializer.SerializeAsync(stream, obj, typeof(MyStruct));
    string actual = Encoding.UTF8.GetString(stream.ToArray());
    if (!TestHelper.JsonEqual(@"{""X"":0,""Y"":0}", actual))
    {
    return -1;
    }

    I assume as part of this work, we would be deleting those tests as well.

  4. eerhardt commented on May 18, 2021

    @eerhardt
    MemberAuthor

    @steveharter @layomia - I plan on working on this. Just ensuring you don't have any objections to removing these attributes.

  5. added a commit that references this issue on May 25, 2021
    ed2c3a9
  6. ghost added
    in-pr
    on May 25, 2021
  7. added a commit that references this issue on May 29, 2021
    95a4476
  8. ghost removed
    in-pr
    on May 29, 2021
  9. davidfowl commented on May 29, 2021

    @davidfowl
    Member

    Didn't we decide that if we had a special [DAM] we could propagate these from user code to preserve type the call sites?

  10. eerhardt commented on May 29, 2021

    @eerhardt
    MemberAuthor

    Yes, when the linker supports making a custom [DAM], then we can mark these methods with that. But we don’t have that feature yet.

  11. ghost locked as resolved and limited conversation to collaborators on Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions