Skip to content

Conversation

chsienki
Copy link
Member

The generator will ignore any class without a matching attribute or base class. We can thus not ever consider them for binding, short circuiting in the vast majority of cases.

@ghost
Copy link

ghost commented Aug 19, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

The generator will ignore any class without a matching attribute or base class. We can thus not ever consider them for binding, short circuiting in the vast majority of cases.

Author: chsienki
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

if (syntaxNode is ClassDeclarationSyntax cds)
if (syntaxNode is ClassDeclarationSyntax { AttributeLists.Count: > 0, BaseList.Types.Count: > 0 } cds)
{
(ClassDeclarationSyntaxList ??= new List<ClassDeclarationSyntax>()).Add(cds);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) I wonder if this property should be renamed. Before it held all classes, so the name made more sense. But now it only holds "potential context classes", so maybe a rename is in order?

public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode is ClassDeclarationSyntax cds)
if (syntaxNode is ClassDeclarationSyntax { AttributeLists.Count: > 0, BaseList.Types.Count: > 0 } cds)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we further constrain it to partial classes or would that loose a valuable diagnostic in the case the user forgot to mark the class partial? I guess we're already losing a diagnostic here when the user forgot to derive from JsonSerializerContext. Do we care?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered that. It does seem odd that it only warns on missing partial.

Aside: is there a reason as a user that I can't just put an attribute on my serialization class, and have it generate the whole thing for me. Seems weird I have to make an empty partial class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: is there a reason as a user that I can't just put an attribute on my serialization class, and have it generate the whole thing for me.

What name and namespace would we generate for the class that derives from JsonSerializerContext?

There is also the scenario of when you want to serialize a Type in an assembly that you don't own/develop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What name and namespace would we generate for the class that derives from JsonSerializerContext?

I mean, you could supply it in the attribute, or derive it.

There is also the scenario of when you want to serialize a Type in an assembly that you don't own/develop.

Assembly attribute? But I digress. I just found the experience a little odd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We welcome the feedback. If you have an idea how we can improve the experience, we'd like to hear it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually used to have assembly level attributes for interacting with the generator and then changed course to the partial class. Some of that discussion is here: #51945 (comment)

@eerhardt
Copy link
Member

@steveharter - can this be merged?

@eiriktsarpalis eiriktsarpalis merged commit 1ae2f79 into dotnet:main Aug 24, 2021
@eiriktsarpalis
Copy link
Member

/backport to release/6.0

@github-actions
Copy link
Contributor

Started backporting to release/6.0: https://github.com/dotnet/runtime/actions/runs/1162083606

@eiriktsarpalis
Copy link
Member

@ericstj we're also backporting this to RC1, right?

@ghost ghost locked as resolved and limited conversation to collaborators Sep 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants