diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
index 80b648dfd95803..cefc1c51e2f5ca 100644
--- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
+++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
@@ -137,16 +137,27 @@ private static SourceWriter CreateSourceWriterWithContextHeader(ContextGeneratio
{
var writer = new SourceWriter();
+ // In release builds of the generator, we suppress all diagnostics stemming from the generated code.
+ // There's nothing a developer can do about these diagnostics, and they can break the build. In debug
+ // builds, we allow more warnings in order to catch potential issues early.
+#if DEBUG
writer.WriteLine("""
//
#nullable enable annotations
#nullable disable warnings
+ #pragma warning disable CS0612, CS0618 // [Obsolete] member usage
- // Suppress warnings about [Obsolete] member usage in generated code.
- #pragma warning disable CS0612, CS0618
+ """);
+#else
+ writer.WriteLine("""
+ //
+
+ #nullable enable annotations
+ #pragma warning disable
""");
+#endif
if (contextSpec.Namespace != null)
{
diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/CompilationHelper.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/CompilationHelper.cs
index 37f6c74e172c0d..12175d068e0f4b 100644
--- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/CompilationHelper.cs
+++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/CompilationHelper.cs
@@ -184,6 +184,16 @@ internal sealed class CompilerFeatureRequiredAttribute : Attribute
public CompilerFeatureRequiredAttribute(string featureName) { }
}
}
+
+ namespace System.Diagnostics.CodeAnalysis
+ {
+ internal sealed class ExperimentalAttribute : Attribute
+ {
+ public ExperimentalAttribute(string diagnosticId) => DiagnosticId = diagnosticId;
+ public string DiagnosticId { get; }
+ public string UrlFormat { get; set; }
+ }
+ }
""";
#endif
diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
index 185881ccbcd16c..858b7176a3d176 100644
--- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
+++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
@@ -521,6 +521,36 @@ public class ClassWithObsolete
CompilationHelper.RunJsonSourceGenerator(compilation);
}
+ [Fact]
+ public static void NoWarningsDueToExperimentalMembers()
+ {
+ string source = """
+ using System;
+ using System.Diagnostics.CodeAnalysis;
+ using System.Text.Json.Serialization;
+
+ namespace Test
+ {
+ #pragma warning disable TEST001
+ [JsonSerializable(typeof(ClassWithExperimental))]
+ #pragma warning restore TEST001
+ public partial class JsonContext : JsonSerializerContext { }
+
+ public class ClassWithExperimental
+ {
+ [Experimental("TEST001")]
+ public bool Test { get; set; }
+
+ [Experimental("TEST002")]
+ public bool Test2 { get; set; }
+ }
+ }
+ """;
+
+ Compilation compilation = CompilationHelper.CreateCompilation(source);
+ CompilationHelper.RunJsonSourceGenerator(compilation);
+ }
+
[Fact]
public static void NoErrorsWhenUsingReservedCSharpKeywords()
{