0

I am having some issues deserializing JSON that has a Zulu formatted time string. I continue to receive an exception that states the string is not a data and time.

I have the following string being returned from and REST service.

{
    ".issued": "2023-03-01T13:26:35.1134406Z",
    ".expires": "2023-03-01T21:26:35.1134406Z",
}

My object:

public class Time
{
     [JsonPropertyName(".issued")]
     [JsonConverter(typeof(DateTimeOffsetJsonConvertZulu))]
     public DateTimeOffset issued { get; set; }

     [JsonPropertyName(".expires")]
     [JsonConverter(typeof(DateTimeOffsetJsonConvertZulu))]
     public DateTimeOffset expires { get; set; }
}

The JSON Converter:


public class DateTimeOffsetJsonConvertZulu : JsonConverter<DateTimeOffset>
{
    public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert,
         JsonSerializerOptions options) =>
         DateTimeOffset.ParseExact(reader.GetString(), "yyyy-MM-ddThh:mm:ss.Z", CultureInfo.InvariantCulture);

   public override void Write(Utf8JsonWriter writer, DateTimeOffset value,
        JsonSerializerOptions options) =>
        writer.WriteStringValue(value.ToUniversalTime());
    }
}
Calling Code:

    
if (httpResponse.IsSuccessStatusCode && httpResponse.Content !=null)
{
   JsonSerializerOptions options= new JsonSerializerOptions() { WriteIndented = true };
                    options.Converters.Add(new DateTimeOffsetJsonConvertZulu());
                    serviceToken = JsonSerializer.Deserialize<CAPIServiceToken>(
                        await httpResponse.Content.ReadAsStringAsync())!;
}

The error: Exception when parsing date time string


I tried to change the format string but unclear how indicate the numbers after the "." and before the "Z".

I am assuming that they are the offset. In UTC I get a +- and the number of hours.
dbc
  • 104,963
  • 20
  • 228
  • 340
Ducky
  • 11
  • 2
  • It's not clear why you'd need a converter at all. This string parses as a valid `DateTimeOffset` with no further configuration -- try leaving this out altogether. If you *do* need a converter though, the format string to use would be `"yyyy-MM-ddTHH:mm:ss.FFFFFFFZ"` (note capital `H` for 24-hours and adding `F` to match the milliseconds, allowing for shorter sequences). Likewise, you should use exactly that pattern in the `ToString` for serialization -- `ToUniversalTime` does *not* produce a string but just converts the value to UTC, which it already should be. – Jeroen Mostert Mar 01 '23 at 20:58

1 Answers1

0

The completed Converter:

public class DateTimeOffsetJsonConvertZulu : JsonConverter<DateTimeOffset>
{
    public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert,
         JsonSerializerOptions options) =>
         DateTimeOffset.ParseExact(reader.GetString(), "yyyy-MM-ddTHH:mm:ss.FFFFFFFZ", CultureInfo.InvariantCulture);

   public override void Write(Utf8JsonWriter writer, DateTimeOffset value,
        JsonSerializerOptions options) =>
        writer.WriteStringValue(value.ToUniversalTime());
    }
}
Ducky
  • 11
  • 2
  • Thanks to [Jeroen Mostert](https://stackoverflow.com/users/4137916/jeroen-mostert) for helping to complete the converter with the required format string. – Ducky Mar 02 '23 at 16:39