Skip to content

Utf8JsonWriter WriteXValue methods assume an array is being written to #11360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Reprevise opened this issue May 27, 2025 · 4 comments
Open
Labels
area-System.Text.Json help wanted Good for community contributors to help [up-for-grabs]
Milestone

Comments

@Reprevise
Copy link

The documentation of the WriteXValue methods assumes you're calling the method to append a value to a JSON array when there other usecases, for example when writing converters (see here).

namespace SystemTextJsonSamples
{
    public class DateTimeOffsetJsonConverter : JsonConverter<DateTimeOffset>
    {
        public override DateTimeOffset Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options) =>
                DateTimeOffset.ParseExact(reader.GetString()!,
                    "MM/dd/yyyy", CultureInfo.InvariantCulture);

        public override void Write(
            Utf8JsonWriter writer,
            DateTimeOffset dateTimeValue,
            JsonSerializerOptions options) =>
                writer.WriteStringValue(dateTimeValue.ToString( // Call to WriteStringValue
                    "MM/dd/yyyy", CultureInfo.InvariantCulture));
    }
}

Documentation for WriteStringValue:

Writes a string text value (as a JSON string) as an element of a JSON array.

There's also a method call to SetFlagToAddListSeparatorBeforeNextItem() inside the WriteStringValue(ReadOnlySpan<char> value) method.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 27, 2025
Copy link
Contributor

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

@eiriktsarpalis
Copy link
Member

Thanks, this is clearly incorrect and should be fixed. The methods can be used to write a value in any context.

@eiriktsarpalis eiriktsarpalis transferred this issue from dotnet/runtime May 28, 2025
@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label May 28, 2025
@eiriktsarpalis eiriktsarpalis added this to the Backlog milestone May 28, 2025
@eiriktsarpalis eiriktsarpalis added the help wanted Good for community contributors to help [up-for-grabs] label May 28, 2025
@Reprevise
Copy link
Author

What about the call to SetFlagToAddListSeparatorBeforeNextItem() inside WriteStringValue(ReadOnlySpan<char>)? It seems like this method was intended to be for writing values to arrays. Though this hasn't broken anything (as far as its known).

@eiriktsarpalis
Copy link
Member

That's just implementation detail, setting a flag that is invariant when writing in non-array contexts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json help wanted Good for community contributors to help [up-for-grabs]
Projects
None yet
Development

No branches or pull requests

2 participants