Serializing Enum as string using attribute in Azure Functions 3.0
I was able to get this working using the following code
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Converters;
[assembly: FunctionsStartup(typeof(Configs.Startup))]
namespace Configs
{
class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddMvcCore().AddNewtonsoftJson(x =>
{
x.SerializerSettings.Converters.Add(new StringEnumConverter());
});
}
}
}
This was in a netcoreapp3.1 on Azure Functions Core Tools (3.0.2534 Commit hash: bc1e9efa8fa78dd1a138dd1ac1ebef97aac8d78e) and Function Runtime Version: 3.0.13353.0 with the following packages:
<PackageReference Include="AsyncEnumerator" Version="4.0.2" />
<PackageReference Include="AzureFunctions.Autofac" Version="4.0.0" />
<PackageReference Include="CsvHelper" Version="15.0.5" />
<PackageReference Include="Dapper" Version="2.0.35" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.7" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
Hopefully this helps someone.
I pushed up an example repo here: https://github.com/rawrspace/string-enum-example
EDIT: I was using this again today with the same setup and using [JsonConverter(typeof(StringEnumConverter))] worked perfectly fine. I am not sure if an update recently happened but I will leave the above solution just in case.
I also ran into serialization issues with a .NET Core 3.1 Function App. I was recommended to use this application setting as a temporary workaround:
"FUNCTIONS_V2_COMPATIBILITY_MODE": true
This solved my issue.