Skip to content

System.Text.Json source generator emits "" for null byte[] properties #129833

Closed

Description

@lezzi

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

Activity

  1. added
    untriaged
    on Jun 25, 2026
  2. dotnet-policy-service commented on Jun 25, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  3. lezzi commented on Jun 25, 2026

    @lezzi
    ContributorAuthor

    I drafted #129834, but open to feedback about best possible approach here before I officially open it.

  4. added
    source-generator
    and removed
    untriaged
    on Jun 25, 2026
  5. added this to the 11.0.0 milestone on Jun 25, 2026
  6. added a commit that references this issue on Jul 1, 2026
    4aa41b7
  7. added a commit that references this issue on Jul 15, 2026
    44159ab
  8. locked and limited conversation to collaborators on Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonbugsource-generator

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions