Json to enum deserialization issue
Can someone explain why does my controller not deserialize enum property but does for a List of that same enum in the same object?? this is the object:
public class CommunityReportFilter
{
public List<RentalMetrics>? RentalMetrics { get; set; }
public RentalMetrics? PrimaryMetric { get; set; }
}
This is the payload:
"rentalMetrics": [
"effRent"
],
"primaryMetric:": "effRent"
list passes the value down but PrimaryMetric is null. Controller is configured to deserialize camelCased strings to enum
9 Replies
I tried changing the both to a list but only RentalMetrics deserializes still
Are you using
System.Text.Json
?
Also, your PrimaryMetric
property is not of type RentalMetrics
, it's of type Nullable<RentalMetrics>
. Depending on your serialization engine, that might be the issue.I am using System.Text.Json
And the same thing happens with RentalMetrics, it just takes the first enum in the in RentalMetrics enum as default instead of taking value from primaryMetric...
How is your json payload generated? Can you post your
JsonSerializerOptions
used?as
It was done from Postman
so raw json
What is the
JsonPropertyNameEnumConverter
type? It seems to me that type is at the core of your problem, if it is responsible for (de)serializing enums...
Also, try serializing the numeric enum values instead of string names, that seems to be the issue to my eyes
yields
Also, I'm noticing in your payload there are no enclosing braces. Did you leave those out or is the payload missing them?
For deserializing stuff in controllers, @TeBeCo is an expert too, or so I hear...
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View