Description
The System.Text.Json source generator serializes a null byte[] property as an empty string ("") instead of null, diverging from the reflection-based serializer. The reflection path (ByteArrayConverter) writes null for a null byte[]; the generated fast path writes "", breaking round-tripping and backward compatibility.
Reproduction Steps
using System.Text.Json;
using System.Text.Json.Serialization;
var poco = new Poco(); // Data is null
Console.WriteLine(JsonSerializer.Serialize(poco)); // reflection
Console.WriteLine(JsonSerializer.Serialize(poco, Ctx.Default.Poco)); // source-gen
class Poco { public byte[]? Data { get; set; } }
[JsonSerializable(typeof(Poco))]
partial class Ctx : JsonSerializerContext { }
Expected behavior
Both serializers produce the same output: {"Data":null}
Actual behavior
The source generator produces: {"Data":""}
Regression?
No
Known Workarounds
Use a custom converter.
Configuration
- Affects all current versions where the STJ source generator emits the Base64 fast path (reproduces on .NET 8/9/10/current main).
- Not OS- or architecture-specific.
Other information
No response
Description
The System.Text.Json source generator serializes a null
byte[]property as an empty string ("") instead ofnull, diverging from the reflection-based serializer. The reflection path (ByteArrayConverter) writesnullfor a nullbyte[]; the generated fast path writes"", breaking round-tripping and backward compatibility.Reproduction Steps
Expected behavior
Both serializers produce the same output:
{"Data":null}Actual behavior
The source generator produces:
{"Data":""}Regression?
No
Known Workarounds
Use a custom converter.
Configuration
Other information
No response