question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ASP.NET Core 3.0 deserialize datetime not like Newtonsoft.Json?

See original GitHub issue

Describe 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() to services.AddControllers().AddNewtonsoftJson(), the model1 is correct.
  • "createTime": "2019-01-01" works well both two way.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
khellangcommented, Dec 16, 2019

That’s just how the new serializer works; it only accepts valid ISO8601-formatted dates, i.e. YYYY-MM-DD where YYYY indicates a four-digit year, 0000 through 9999. MM indicates a two-digit month of the year, 01 through 12 and DD 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

1reaction
khellangcommented, Dec 16, 2019

But why “2019-1-1” deserialize to [FromQuery]DateTime createTime is correct?

That’s because DateTime query values are converted using DateTimeConverter, which uses DateTime.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found