Utf8jsonreader Datetimeoffset Parsing Rfc 3339

1 file1 year ago529 views

Utf8jsonreader Datetimeoffset Parsing Rfc 3339

When reading token-by-token and encountering a JsonTokenType.String that contains an RFC 3339 date:

RFC 3339 is essentially a strict profile of . By default, System.Text.Json (and thus Utf8JsonReader ) is designed to support the ISO 8601-1:2019 extended format . While this aligns closely with RFC 3339, there are critical differences in how Utf8JsonReader enforces these rules:

If you need UTC normalization:

string? propertyName = reader.GetString(); if (propertyName == "timestamp")

if (reader.TokenType != JsonTokenType.String) throw new JsonException("Expected string token"); string s = reader.GetString(); const string rfc3339Pattern = "yyyy-MM-dd'T'HH:mm:ss.FFFFFFFK";

reader.Read(); // Move to the value (String)

Utf8JsonReader reader = new Utf8JsonReader(jsonUtf8Bytes, new JsonReaderOptions AllowTrailingCommas = true );

ReadOnlySpan<byte> utf8Span = reader.ValueSpan; Span<char> buffer = stackalloc char[utf8Span.Length]; int chars = Encoding.UTF8.GetChars(utf8Span, buffer); ReadOnlySpan<char> dateStr = buffer.Slice(0, chars);