System.Text.Json UTC DateTime Issue in C#

Timothy Fischer 121 Reputation points
2021-09-17T23:49:10.83+00:00

I'm trying to deserialize a Json response into a class that looks like this:

jiraResponseMessage = JsonSerializer.Deserialize<InternalJiraIssuesResponse>(responseString);

When running the code, I keep getting the following error:

The JSON value could not be converted to System.DateTime. Path: $.issues[0].changelog.histories[0].created | LineNumber: 0 | BytePositionInLine: 1451.

The error is being thrown when trying to deserialize this part :

"created":"2021-09-16T09:12:55.427-0700",

Can anyone help me to figure out how to get the Json to be deserialized for this?

This is the class I have for deserializing:

public class History
{
    public string id { get; set; }
    public Author author { get; set; }
    public DateTime created { get; set; }
    public Item[] items { get; set; }
}

Even when changing "created" to a string or object, it still chokes.

Any ideas on how I can resolve this?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,501 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timothy Fischer 121 Reputation points
    2021-09-18T01:52:14.837+00:00

    Weird...all of a sudden it works when I change to string:
    -- public string created { get; set; }

    I swear it was throwing the exception I posted earlier, even after I changed it from DateTime to a string

    Thanks for your help Paul! You rock!


2 additional answers

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2021-09-18T00:54:00.13+00:00

    ISO 8601 date formats have a colon between the hours & minutes of the timezone offset like this:
    2021-09-16T09:12:55.427-07:00

    https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC

    Did you hand write this or was this serialised programmatically?

    Edit: Actually it looks like this format just isn't supported.

    According to this comment the offset that contains the colon is the extended format that the JsonSerializer supports only be default.
    https://github.com/dotnet/runtime/issues/30883#issuecomment-533289968

    He did reference this article which explains how to make a DateTime/DateTimeOffset converter to handle this case though:
    https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#custom-support-for--and-

    0 comments No comments

  2. Timothy Fischer 121 Reputation points
    2021-09-18T01:19:35.363+00:00

    Is there a way I could trick the deserializer into thinking it is a string instead -- I did read the articles in the links you added prior to asking the question...mainly because I'm trying to pull into a class and I did not see any samples there :(

    I would think that because the datetime comes over in string format (i.e. "created": "##:##T##...") that I could just pull it in my class as a string rather than datetime.

    Thanks for the response.