0

I have a model with a nullable DateTime property

public DateTime? DateOfBirth { get; set; }

when calling the API with the following value for the property

{       "Country": "US",
       "State": "New York",
       "Zip" : "123455",
       "dateOfBirth": "8/15/62"
}

This error is thrown by .net framework:

"errors": {
        "$.dateOfBirth": [
            "The JSON value could not be converted to System.Nullable`1[System.DateTime]. Path: $.dateOfBirth | LineNumber: 9 | BytePositionInLine: 33."
        ]

The parsing seem to work with other date formats like yyyy-mm-dd, but not for the format mentioned above in the example.

Sameed
  • 655
  • 1
  • 5
  • 18
  • If you wonder whether it's the format, have you considered just calling it with a different format to verify your hypothesis? – mason Feb 02 '22 at 21:42
  • @mason yes, I have, seems to work with other formats, but I wonder why this format is not supported while parsing – Sameed Feb 02 '22 at 21:43
  • 1
    Okay, so you know the answer to the question you asked in your post. Perhaps you've asked the wrong question, and should edit your post to ask the thing that you actually want to know? – mason Feb 02 '22 at 21:45
  • 1
    @mason updated the question, now do you have anything useful to mention? – Sameed Feb 02 '22 at 21:48
  • You've updated the post...but now it lacks a question entirely. Stack Overflow is a question based site. What is it you want to know? Do you want to know how to post data in that format to your API? Or do you want to know *why* it doesn't work as written? Those are different questions, requiring different answers. Therefore, you need to be specific about what you're asking. I'm sorry if you feel my comments have not been helpful so far, but guiding you towards how to ask a good question is actually a very important thing. – mason Feb 02 '22 at 21:50
  • Have you tried a custom DateTime converter? – aweyeahdawg Feb 02 '22 at 23:57

1 Answers1

1

I deserialized it using Newtonsoft.Json and had no errors in the processe. I used this class

public class Root
{
    public string Country { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public DateTime? dateOfBirth { get; set; }
}

but my Windows CultureInfo("en-US"). if your culture info is different or are using another serializer and have a propblem I would just advice you to change DateTime type to string, and try to convert ToDateTime after deserialization.

DateTime format depends on regioanal culture in the Windows settings, so if datetime string format in US (as in your json) but you are in Europe, you will always get the error , so IMHO you better use this code, since you can select a culture of json, not Windows default.

public class Root
{
    public string Country { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }

     [JsonProperty("dateOfBirth")]
    private string DateOfBirth { get; set; }
    
    [JsonIgnore]
    public DateTime? dateOfBirth
    {
        get { return DateTime.Parse(DateOfBirth, new CultureInfo("en-US")); }
    }
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • I am using .net's own deserializer from namespace System.Text.Json – Sameed Feb 02 '22 at 21:56
  • 2
    @Sameed System.Text.Json is a garbage, you will have only problems. Change to Newtonsoft one. – Serge Feb 02 '22 at 21:58
  • did some more testing and it looks like text.Json only parses dates with format yyyy-mm-dd, has it anything to do with the region culture? I am in Europe. – Sameed Feb 02 '22 at 22:10
  • @Sameed yes, you are right , I updated my answer – Serge Feb 02 '22 at 22:30
  • 3
    @Serge I have never had a problem with System.Text.Json. This question is totally within System.Text.Json's use. Curious as to why you think that? – aweyeahdawg Feb 02 '22 at 23:53
  • @aweyeahdawg you can check json answers in my profile. I have had some very good exprerience during the last year. What takes one line code in Newtosoft, will need at least one hundred lines of code using Text.Json – Serge Feb 02 '22 at 23:57
  • 3
    @Serge At least 100 lines? Sounds like you didn't know how to do it once and wrote it off forever. I've never found anything that couldn't be done with a custom converter (which abstracts the conversion away from your model resulting in cleaner code). – aweyeahdawg Feb 03 '22 at 00:02
  • @aweyeahdawg Yes, this is what I mean. When I use Newtonsoft for complicated jsons I need a custom converter in 1 case of hundred. With Text.Json I need a custom converter in 99% – Serge Feb 03 '22 at 01:21