-
Notifications
You must be signed in to change notification settings - Fork 5.5k
System.Text.Json source generator emits "" for null byte[] properties #129833
Open
Description
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
Metadata
Metadata
Assignees
Type
Fields
Give feedbackNo fields configured for issues without a type.