ASP.NET Core 3.0 deserialize datetime not like Newtonsoft.Json?
See original GitHub issueDescribe the bug
When the model have a datetime property and json-value ‘2019-1-1’, ASP.NET Core 3.0 deserialize it to null.
To Reproduce
# action
public void Post([FromBody] Model1 model1)
public class Model1
{
public DateTime? CreateTime { get; set; }
}
{
"createTime": "2019-1-1"
}
- By the code above, the model1 will be null.
- When I change
services.AddControllers()
toservices.AddControllers().AddNewtonsoftJson()
, the model1 is correct. "createTime": "2019-01-01"
works well both two way.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
After porting ASP.NET Core 2.2 to 3.0,request DateTime ...
I'm porting a simple web API from asp.net core 2.2 to asp.net core 3.0 and getting a serialization error after sending a request...
Read more >Migrate from Newtonsoft.Json to System.Text.Json - .NET
If you're using System.Text.Json indirectly by using ASP.NET Core, you don't need to do anything to get behavior like Newtonsoft.Json .
Read more >DateTime and DateTimeOffset support in System.Text.Json
DateTime and DateTimeOffset can also be deserialized with JsonSerializer: C# ... output: // The JSON value could not be converted to System.
Read more >How to Deserialize a Complex JSON Object in C# .NET
In this article, we are gonig to learn how to deserialize a complex JSON object using C# as our language of choice.
Read more >Json.NET - Newtonsoft
Serialize and deserialize any .NET object with Json.NET's powerful JSON serializer. LINQ to JSON. Create, parse, query and modify JSON using Json.NET's JObject ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s just how the new serializer works; it only accepts valid ISO8601-formatted dates, i.e.
YYYY-MM-DD
whereYYYY
indicates a four-digit year, 0000 through 9999.MM
indicates a two-digit month of the year, 01 through 12 andDD
indicates a two-digit day of that month, 01 through 31.What you’re passing is not a valid ISO8601 date and needs a custom converter to work. Here’s an example of a converter for a custom date format; https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to#sample-basic-converter
That’s because
DateTime
query values are converted usingDateTimeConverter
, which usesDateTime.Parse
and isn’t as strict as the JSON serializer (at the cost of performance). If you want the same behavior, you need to do the same in your custom converter.