Open
Description
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.