diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index cd24cda84f0bd3..abf92ca701d8a6 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -1783,6 +1783,21 @@ private static void GenerateSerializeValueStatement(SourceWriter writer, TypeGen { writer.WriteLine($"writer.{primitiveWriterMethod}Value({valueExpr}.ToString());"); } +else if (typeSpec.PrimitiveTypeKind is JsonPrimitiveTypeKind.ByteArray) +{ + // byte[] is a nullable reference type; emit an explicit null check so that null values + // serialize as 'null' rather than an empty Base64 string, matching reflection behavior. + writer.WriteLine($$""" + if ({{valueExpr}} is byte[] __byteArray) + { + writer.{{primitiveWriterMethod}}Value(__byteArray); + } + else + { + writer.WriteNullValue(); + } + """); +} else { writer.WriteLine($"writer.{primitiveWriterMethod}Value({valueExpr});"); @@ -1809,6 +1824,21 @@ private static void GenerateSerializePropertyStatement(SourceWriter writer, Type { writer.WriteLine($"writer.{primitiveWriterMethod}({propertyNameExpr}, {valueExpr}.ToString());"); } +else if (typeSpec.PrimitiveTypeKind is JsonPrimitiveTypeKind.ByteArray) +{ + // byte[] is a nullable reference type; emit an explicit null check so that null values + // serialize as 'null' rather than an empty Base64 string, matching reflection behavior. + writer.WriteLine($$""" + if ({{valueExpr}} is byte[] __byteArray) + { + writer.{{primitiveWriterMethod}}({{propertyNameExpr}}, __byteArray); + } + else + { + writer.WriteNull({{propertyNameExpr}}); + } + """); +} else { writer.WriteLine($"writer.{primitiveWriterMethod}({propertyNameExpr}, {valueExpr});"); diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs index 3cfcdcdc03a7e9..f24387da387891 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs @@ -873,6 +873,7 @@ public virtual void ClassWithNullableProperties_Roundtrip() { Uri = new Uri("http://contoso.com"), Array = new int[] { 42 }, + ByteArray = new byte[] { 1, 2, 3 }, Poco = new ClassWithNullableProperties.MyPoco(), NullableUri = new Uri("http://contoso.com"), @@ -889,6 +890,7 @@ void RunTest(ClassWithNullableProperties expected) Assert.Equal(expected.Uri, actual.Uri); Assert.Equal(expected.Array, actual.Array); + Assert.Equal(expected.ByteArray, actual.ByteArray); Assert.Equal(expected.Poco, actual.Poco); Assert.Equal(expected.NullableUri, actual.NullableUri); @@ -943,6 +945,7 @@ public class ClassWithNullableProperties { public Uri? Uri { get; set; } public int[]? Array { get; set; } + public byte[]? ByteArray { get; set; } public MyPoco? Poco { get; set; } public Uri? NullableUri { get; set; } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs index b557871e76d460..48eb0e377e0276 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs @@ -455,6 +455,7 @@ public override void ClassWithNullableProperties_Roundtrip() { Uri = new Uri("http://contoso.com"), Array = new int[] { 42 }, + ByteArray = new byte[] { 1, 2, 3 }, Poco = new ClassWithNullableProperties.MyPoco(), NullableUri = new Uri("http://contoso.com"), @@ -471,6 +472,7 @@ void RunTest(ClassWithNullableProperties expected) Assert.Equal(expected.Uri, actual.Uri); Assert.Equal(expected.Array, actual.Array); + Assert.Equal(expected.ByteArray, actual.ByteArray); Assert.Equal(expected.Poco, actual.Poco); Assert.Equal(expected.NullableUri, actual.NullableUri);