Utf8jsonreader Datetimeoffset Parsing Rfc 3339 -
throw new JsonException("Invalid RFC 3339 format");
return dateTimeOffset;
If your JSON source provides dates that are "almost" RFC 3339 (e.g., Microsoft JSON format /Date(123456789)/ or missing the 'T' separator), GetDateTimeOffset() will fail. You must implement a custom converter.
if (DateTimeOffset.TryParse(dateString, out DateTimeOffset dto)) return dto; utf8jsonreader datetimeoffset parsing rfc 3339
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 is designed for zero-allocation reading.
ReadOnlySpan<byte> propertyName = reader.ValueSpan; string propertyNameString = System.Text.Encoding.UTF8.GetString(propertyName); ReadOnlySpan<byte> propertyName = reader
public static DateTimeOffset ReadDateTimeOffset(this ref Utf8JsonReader reader)
| Error | Cause | |------------------------------------|-----------------------------------------------------------------------| | JsonException: Expected string | Token is not a string (maybe null or number). | | FormatException on TryParse | Missing T , wrong offset format ( +0530 instead of +05:30 ), wrong fractional seconds. | | Z not recognized | Some old parsers need ToUniversalTime() – not with DateTimeOffset . |
// Fallback or error handling
:
RFC 3339 is the standard timestamp format used by most modern APIs (e.g., 2023-10-27T10:30:00Z or 2023-10-27T10:30:00+02:00 ).
If you deserialize a whole object, you don’t need manual parsing: | // Fallback or error handling : RFC