Skip to content

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

Open
@Reprevise

Description

@Reprevise

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions