Skip to content
Draft
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 null)
{
writer.WriteNullValue();
}
else
{
writer.{{primitiveWriterMethod}}Value({{valueExpr}});
}
""");
}
Comment on lines +1786 to +1800
else
{
writer.WriteLine($"writer.{primitiveWriterMethod}Value({valueExpr});");
Expand All @@ -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 null)
{
writer.WriteNull({{propertyNameExpr}});
}
else
{
writer.{{primitiveWriterMethod}}({{propertyNameExpr}}, {{valueExpr}});
}
""");
}
Comment on lines +1827 to +1841
else
{
writer.WriteLine($"writer.{primitiveWriterMethod}({propertyNameExpr}, {valueExpr});");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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);
Expand Down Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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);
Expand Down
Loading