throw new JsonException("Invalid RFC 3339 format");
if (reader.TokenType == JsonTokenType.String) 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 | throw new JsonException("Invalid RFC 3339 format")
byte[] jsonUtf8 = Encoding.UTF8.GetBytes(@" ""created"": ""2023-12-01T09:15:30+02:00"" "); Utf8JsonReader reader = new Utf8JsonReader(jsonUtf8); while (reader.Read()) | | Manual token parsing
using System; using System.Text.Json; using System.Text.Json.Serialization; // for JsonException public static DateTimeOffset ParseDateTimeOffsetFromReader(ref Utf8JsonReader reader)
public DateTimeOffset Timestamp get; set;
⚠️ Stackalloc only safe for reasonably short strings (RFC 3339 ~30 chars). Perfectly safe here. RFC 3339 examples: