0

We have a MVC.Core project where we're having problems deserializing a DateTime value from a string into a DateTime property.

Here's an example:

public class TestClass
{
    public RecordStatus RecordStatus { get; set; } = RecordStatus.Active;

    [System.ComponentModel.Browsable(false)]
    public DateTime CreatedOn { get; private set; }
}


    string s = @"{""recordStatus"":""Active"",""createdOn"":""2018-03-02T21:39:22.075Z""}";

    JsonSerializerSettings jsonSettings = new JsonSerializerSettings()
    {
        DateFormatString = "yyyy-MM-ddTHH:mm:ss.FFFZ",
        DateTimeZoneHandling = DateTimeZoneHandling.Utc,
        MissingMemberHandling = MissingMemberHandling.Ignore
    };

    TestClass psm = JsonConvert.DeserializeObject<TestClass>(s, jsonSettings);

The value of psm.CreatedOn is being set to

{1/1/0001 12:00:00 AM}

I've tried a bunch of different combination of values for the serializer settings with no luck. What am I missing here? I know I'm missing something obvious, but it's on of those days.

Thanks

5
  • what's with the T between the date and time? Commented Mar 8, 2018 at 19:25
  • Can you try a date with the following format as in "MM/dd/yyyy HH:mm:ss"? Commented Mar 8, 2018 at 19:26
  • The string formatting of the datetime we're getting as input is not going to change. The datetime is in ISO format. Commented Mar 8, 2018 at 19:28
  • "T" is the time designator that precedes the time components of the representation. Commented Mar 8, 2018 at 19:32
  • Mark CreatedOn property with [JsonProperty] attribute. Commented Mar 8, 2018 at 19:53

2 Answers 2

2

The problem here isn't the format string, that is just fine (as we can see here):

Console.WriteLine(DateTime.ParseExact("2018-03-02T21:39:22.075Z","yyyy-MM-ddTHH:mm:ss.FFFZ", CultureInfo.InvariantCulture));

Yields:

02-Mar-18 4:39:22 PM

The issue is actually the private setter on CreatedOn. If you remove that everything works. In fact, it works with the default serializer settings, so there is no need to do anything with that format string.

If you are set on having a private setter for that field, then I would suggest looking into something like this to make Json.NET use the private setter.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes, that's most likely the issue... Let me check.
1
public class TestClass
{
    public RecordStatus RecordStatus { get; set; } = RecordStatus.Active;

    [System.ComponentModel.Browsable(false)]
    public DateTime CreatedOn { get; set; }
}

Remove the private access modifier from the setter for the CreatedOn property

2 Comments

You answered first. I'll mark this as the answer. Told you it was a simple problem.... This what happens after a few days of meetings and trying to get back to actual work.
@MonkeyWrench actually, Pete answered first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.