I have a production api returning the below json:
[
{
"StartUtc": "2023-09-02T13:52:00Z",
"EndUtc": "2023-09-02T15:19:00Z",
"isCustomerInStore": false,
"productLead": {
"id": 1,
"leadType": "PREVIOUS_CUSTOMER",
"leadStatus": "SOLD_DELIVERED",
"leadStatusType": "SOLD",
"leadSourceId": 11,
"leadSourceName": "Previous Customer"
},
"customer": {
"id": 1,
"firstName": "Jane",
"lastName": "Doe",
"salesRepresentative": {
"id": 1,
"firstName": "Sales",
"lastName": "Guy1",
"assignedUserType": "SALES_REPRESENTATIVE"
}
},
"product": {
"year": 2023,
"make": "Google",
"model": "Pixel",
"stockNumber": "ABCD1234"
}
}
]
After the serialization of the above json, my api returns the below.
[
{
"StartUtc": "2023-09-02T13:52:00Z",
"EndUtc": "2023-09-02T15:19:00Z",
"isCustomerInStore": false,
"productLead": {
"id": 1,
"leadType": "previouS_CUSTOMER",
"leadStatus": "solD_DELIVERED",
"leadStatusType": "sold",
"leadSourceId": 11,
"leadSourceName": "Previous Customer"
},
"customer": {
"id": 1,
"firstName": "Jane",
"lastName": "Doe",
"salesRepresentative": {
"id": 1,
"firstName": "Sales",
"lastName": "Guy1",
"assignedUserType": "saleS_REPRESENTATIVE"
}
},
"product": {
"year": 2023,
"make": "Google",
"model": "Pixel",
"stockNumber": "ABCD1234"
}
}
]
The leadType
, leadStatus
, leadStatusType
& assignedUserType
are enum fields having attributes [JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
. Also, all the enums have the attribute [JsonConverter(typeof(JsonStringEnumConverter))]
. But they are returning with first few characters serialized to lower characters? Any ideas why?
"previouS_CUSTOMER"
is the camel case serialization ofPREVIOUS_CUSTOMER
generated by System.Text.Json. Somewhere in your startup method you must be constructing aJsonStringEnumConverter
usingJsonNamingPolicy.CamelCase
, e.g. inProgram.cs
To prevent this, you have a few options:
Firstly, if you never want camel case serialization of enums, just don't use
JsonNamingPolicy.CamelCase
withJsonStringEnumConverter
:You can still use camel case for your property names.
Secondly, if you want camel case serialization of enums for most enums but not your
LeadType
enum, useOptOutJsonConverterFactory
from this answer to Exclude an enum property of a Model from using the JsonStringEnumConverter which is globally set at the Startup? to disable its use forLeadType
:Demo fiddle #1 here.
Finally, if you don't want camel case enum serialization for the
LeadType
property, apply[System.Text.Json.Serialization.JsonConverter(typeof(JsonStringEnumConverter))]
to the property like so:Demo fiddle #2 here.
Notes:
As explained in the docs, converters are chosen with the following precedence:
As you can see, converters added to options supersede converters applied to types (but not properties). This is why applying
[JsonConverter(typeof(JsonStringEnumConverter))]
to your enum types did not solve the problem.You wrote that you applied
To each property type, but
StringEnumConverter
is from Newtonsoft.Json, a completely different serializer. System.Text.Json has an attributeJsonConverterAttribute
with the same name as Newtonsoft'sJsonConverterAttribute
so it's easy to get them confused. When using both serializers, I recommend using the fully qualified name for metadata attributes.