Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libraries/System.Text.Json/gen/JsonSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private sealed class SyntaxReceiver : ISyntaxReceiver

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)

{
(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?

}
Expand Down