Add simplified [JsonSerializable] source generation for POCOs#126861
Add simplified [JsonSerializable] source generation for POCOs#126861
Conversation
…tion pipeline - Extended JsonSerializableAttribute with parameterless ctor and AttributeTargets.Struct - Updated ref assembly - Added SYSLIB1227 (type must be partial) and SYSLIB1228 (member name collision) diagnostics - Added PocoTypeGenerationSpec model - Added second pipeline in Initialize() for POCO types - Added EmitPocoType() to generate backing context and static JsonTypeInfo property - Updated Roslyn 3.11 generator with equivalent POCO support - Handled parameterless [JsonSerializable] in ParseJsonSerializableAttribute Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d115f32b-7c2c-4cca-a15d-d57ce9850f97 Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
…s declaration - Changed ParsePocoTypeGenerationSpec to return (PocoTypeGenerationSpec, ContextGenerationSpec) tuple - Use existing type graph traversal to build proper ContextGenerationSpec for POCO types - Added synthetic TypeRef constructor for context types not in compilation - Emit base class declaration in separate .Base.g.cs file for synthetic context - All 218 unit tests pass including 7 new POCO tests Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d115f32b-7c2c-4cca-a15d-d57ce9850f97 Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
| """; | ||
|
|
||
| Compilation compilation = CompilationHelper.CreateCompilation(source); | ||
| JsonSourceGeneratorResult result = CompilationHelper.RunJsonSourceGenerator(compilation, disableDiagnosticValidation: true); |
There was a problem hiding this comment.
@copilot why are we disabling diagnostic validation?
There was a problem hiding this comment.
These tests intentionally produce Warning-level diagnostics (SYSLIB1227 for non-partial type, SYSLIB1228 for member name collision). The default validation in RunJsonSourceGenerator asserts AssertMaxSeverity(DiagnosticSeverity.Info), which would fail for these cases. This is the same pattern used by other diagnostic tests in this file (lines 211, 231, 987).
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
I think we need to update the API compat files to allow the new attribute usage |
…age change Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/8e75d756-a303-4334-8efc-7df66c802663 Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Added CP0015 suppressions for |
Description
Today, System.Text.Json source generation requires creating a separate
JsonSerializerContext-derived class and annotating it with[JsonSerializable(typeof(T))]for every type — significantly more ceremony than the reflection API. This PR allows placing[JsonSerializable]directly on a partial class or struct to auto-generate a backing context and a staticJsonTypeInfoproperty.Before:
After:
Attribute changes
JsonSerializableAttributeAttributeTargetsto includeStructAttributeUsageAttributechange onJsonSerializableAttributein bothApiCompatBaseline.NetCoreAppLatestStable.xmland per-packageCompatibilitySuppressions.xmlSource generator pipeline
JsonSourceGenerator.Roslyn4.0.csand equivalent logic inRoslyn3.11.csthat detects parameterless[JsonSerializable]on non-context types(PocoTypeGenerationSpec, ContextGenerationSpec)tuple using the existing type graph traversal, so the full emitter infrastructure is reusedTypeRef(string, string)constructor for synthetic context types that don't exist in the compilationEmitter output (per annotated type)
__JsonContext_<TypeName>.Base.g.cs— base class declaration for the synthetic context__JsonContext_<TypeName>.g.cs+ per-type files — full context implementation via existingEmit()path<TypeName>.JsonSerializable.g.cs— partial type extension withpublic static JsonTypeInfo<T> JsonTypeInfopropertyDiagnostics
SYSLIB1227— type must be declaredpartialSYSLIB1228— member name collision onJsonTypeInfo(useTypeInfoPropertyNameto resolve)SYSLIB1224now only fires for[JsonSerializable(typeof(T))]on non-context types, not for the parameterless formTests
TypeInfoPropertyName, coexistence with explicit context, and global namespaceNot included (follow-up)
JsonSerializer.Serialize(val)→JsonSerializer.Serialize(val, T.JsonTypeInfo)(plan item 4 — requires significant interceptor infrastructure)