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
David Jovanovski
I tried changing the both to a list but only RentalMetrics deserializes still
SleepWellPupper
SleepWellPupper4mo ago
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.
David Jovanovski
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...
SleepWellPupper
SleepWellPupper4mo ago
How is your json payload generated? Can you post your JsonSerializerOptions used?
David Jovanovski
as
C#
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonPropertyNameEnumConverter(JsonNamingPolicy.CamelCase));
options.JsonSerializerOptions.Converters.Add(new DoubleConvertor());
options.JsonSerializerOptions.Converters.Add(new FloatConvertor());
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
C#
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonPropertyNameEnumConverter(JsonNamingPolicy.CamelCase));
options.JsonSerializerOptions.Converters.Add(new DoubleConvertor());
options.JsonSerializerOptions.Converters.Add(new FloatConvertor());
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
It was done from Postman so raw json
SleepWellPupper
SleepWellPupper4mo ago
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
SleepWellPupper
SleepWellPupper4mo ago
using System;
using System.Collections.Generic;
using System.Text.Json;

var options = new JsonSerializerOptions()
{
WriteIndented = true
};

var serialized = JsonSerializer.Serialize(new CommunityReportFilter()
{
RentalMetrics = [RentalMetrics.FirstValue, RentalMetrics.SecondValue, RentalMetrics.DefaultValue],
PrimaryMetric = RentalMetrics.FirstValue
}, options);
Console.WriteLine(serialized);
var deserialized = JsonSerializer.Deserialize<CommunityReportFilter>(serialized, options);

Console.WriteLine($"RentalMetrics: {String.Join(", ", deserialized.RentalMetrics ?? [])}");
Console.WriteLine($"PrimaryMetric: {deserialized.PrimaryMetric?.ToString() ?? "null"}");

public class CommunityReportFilter
{
public List<RentalMetrics>? RentalMetrics { get; set; }
public RentalMetrics? PrimaryMetric { get; set; }
}

public enum RentalMetrics
{
DefaultValue,
FirstValue,
SecondValue
}
using System;
using System.Collections.Generic;
using System.Text.Json;

var options = new JsonSerializerOptions()
{
WriteIndented = true
};

var serialized = JsonSerializer.Serialize(new CommunityReportFilter()
{
RentalMetrics = [RentalMetrics.FirstValue, RentalMetrics.SecondValue, RentalMetrics.DefaultValue],
PrimaryMetric = RentalMetrics.FirstValue
}, options);
Console.WriteLine(serialized);
var deserialized = JsonSerializer.Deserialize<CommunityReportFilter>(serialized, options);

Console.WriteLine($"RentalMetrics: {String.Join(", ", deserialized.RentalMetrics ?? [])}");
Console.WriteLine($"PrimaryMetric: {deserialized.PrimaryMetric?.ToString() ?? "null"}");

public class CommunityReportFilter
{
public List<RentalMetrics>? RentalMetrics { get; set; }
public RentalMetrics? PrimaryMetric { get; set; }
}

public enum RentalMetrics
{
DefaultValue,
FirstValue,
SecondValue
}
yields
No description
SleepWellPupper
SleepWellPupper4mo ago
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
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server