C
C#2y ago
Thinker

❔ How does ASP.NET deserialize JSON to records?

This isn't specifically related to ASP.NET, but still wondering. If I have an endpoint like this
private sealed record SendRequest(string Content);
private static Task<IResult> Send(
[FromBody] SendRequest request)
{
// ...
}
private sealed record SendRequest(string Content);
private static Task<IResult> Send(
[FromBody] SendRequest request)
{
// ...
}
then ASP.NET has no problem deserializing the body into the record. But... how does it do that? Just using System.Text.Json it doesn't appear like you can (or I'm doing something wrong), and plus, the properties that records generate don't have setters. How does this work?
2 Replies
Thinker
ThinkerOP2y ago
I know records generate essentially get-only auto-properties for all the parameters of its primary constructor, which means that each of those properties also have a backing fields. So does the JSON deserialization somehow link properties to their corresponding backing fields and set the fields instead?
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server