-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
Type of issue
Other (describe below)
Description
The first code example under Utf8Parser and Utf8Formatter contains this code in the overridden Write method:
Span<byte> utf8Date = new byte[29];
Allocating a new byte array every time a DateTime value is written seems to be a considerable overhead. Allocating byte array on the stack instead would be more efficient.
Also, that section begins with the statement "if your input DateTime or DateTimeOffset text representations are compliant with one of the "R", "l", "O", or "G" standard date and time format strings". It seems like this is not a limitation and the value can be serialized using any supported format. For example, here's how DateTime value can be written in "s" format:
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
Span<char> span = stackalloc char[32];
value.TryFormat(span, out var count, "s");
writer.WriteStringValue(span[..count]);
}
Page URL
https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#-and-
Content source URL
https://github.com/dotnet/docs/blob/main/docs/standard/datetime/system-text-json-support.md
Document Version Independent Id
77358796-3ee2-9b86-7e0a-92c60ebc371a
Platform Id
45e1d7c2-9630-1091-5921-5194b2bfef72
Article author
Metadata
- ID: eb41b02f-8fe9-44a1-544f-b6016f1ff4b3
- PlatformId: 45e1d7c2-9630-1091-5921-5194b2bfef72
- Service: dotnet-fundamentals