throw new JsonException($"Invalid RFC 3339 DateTimeOffset format: dateString");
public static DateTimeOffset GetDateTimeOffsetRfc3339(ref this Utf8JsonReader reader) if (reader.TokenType != JsonTokenType.String) throw new JsonException($"Expected string, got reader.TokenType"); // Fast path: use built-in conversion if possible (avoids custom parse) if (reader.TryGetDateTimeOffset(out DateTimeOffset dto)) return dto; utf8jsonreader datetimeoffset parsing rfc 3339
| Scenario | Recommended Approach | |----------------------------------------|--------------------------------------------------------------------------------------| | Full object deserialization | Use JsonSerializer.Deserialize<T> | | Manual token parsing, performance OK | reader.GetString() + DateTimeOffset.TryParse | | Strict RFC 3339, low alloc | reader.ValueSpan + TryParseExact + stackalloc | | High-throughput streaming JSON | Use Utf8JsonReader + span-based parsing without intermediate string | | | Manual token parsing
if (reader.TokenType == JsonTokenType.String) utf8jsonreader datetimeoffset parsing rfc 3339
using System; using System.Text.Json; using System.Text.Json.Serialization; // for JsonException public static DateTimeOffset ParseDateTimeOffsetFromReader(ref Utf8JsonReader reader)