I am currently using .NET 6 and System.Text.Json for serialization. I am having issues where system.text.json does not deserialize the enum to string properly when I return the response using OkObjectResult or ObjectResult
I have used the following on model
public class Customer
{
public string? id { get; set; }
public string? Name { get; set; }
public string Address { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))]
public CustomerType Type {get; set;}
}
using System.Text.Json.Serialization;
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum CustomerType
{
NEW,
EXISTING
}
Now the API Code
public async Task<IActionResult> GetCustomerById(string Id)
{
var results = await _customerService.GetData(Id);
// The results have correct JSON data with enum as string but as soon as I pass this to OkObjectResult, it changes the enum back to int
return new OkObjectResult(results );
}
Service
public async Task<Customer> GetData(string Id)
{
var results = await _httpClient.SendAsync(Id); // Get Data
var jsonResults = await results.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions{ Converters ={
new JsonStringEnumConverter()};
return JsonSerializer.Deserialize<Customer>(jsonResults ,
options ) ; // This returns the data correctly
}
Now my question why OkObjectResult breaks the code and return the integer instead of enum string value
results
are actually a strongly typedCustomer
, meaning they aren't JSON at all: they're a C# object. The CustomerType property on that object is not a string, it's aCustomerType
enum. So the question is, how is the framework choosing to serialize that C# object you're passing in? See learn.microsoft.com/en-us/aspnet/core/web-api/advanced/… – DiscomfortableCustomer
).GetData
doesn't return JSON: it returns aCustomer
. – DiscomfortablejsonResults
string? Also, is there any chance that, in your production code, the property is actually nullable, i.e.public CustomerType? Type {get; set;}
– CacomistlejsonResults
equal{"id":"123","type":"NEW"}
or{"customer":{"id":"123","type":"NEW"}}
? Your comment shows some sort of wrapper object (but omits the outer braces and so is technically malformed) which leaves me confused as to the precise contents of jsonResults. – CacomistleCustomer
type serializes correctly as{"id":"id","Name":null,"Address":null,"Type":"NEW"}
whenSystem.Text.Json
is called directly, see dotnetfiddle.net/K1LDyC. And Sedat's answer should work even whenJsonStringEnumConverter
is not applied directly. This leads me to believe you need to recheck your assumptions, and creating a minimal reproducible example will force you to do that, – Cacomistle"customer":{"id":"123","type":"NEW"}
is malformed because it lacks outer braces. 2){"customer":{"id":"123","type":"NEW"}}
has a wrapper object not reflected in youCustomer
data model. 3){"id":"123","type":"NEW"}
is camel cased but in your callJsonSerializer.Deserialize<Customer>(jsonResults, options )
you don't useJsonNamingPolicy.CamelCase
. So as shown your ` GetData(string Id)` method should not be returning a correctly populatedCustomer
. – Cacomistle