我正在使用 Azure Function 的 SignalR Service
输出绑定(bind)。当我向消息添加一些参数时,它会被库 Microsoft.Azure.WebJobs.Extensions.SignalRService
序列化。可以使用 System.Text.Json
或 Newtonsoft.Json
进行序列化。
我不知道如何使用任一序列化来提供序列化的自定义设置(我希望将枚举转换为字符串而不是数字)。
有什么办法可以做到这一点吗?
我也尝试过序列化自己,但是库会转义所有内容以确保有效的 json,这也不太理想。
最佳答案
使用System.Text.Json库
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.Configure<SignalROptions>(o => o.JsonObjectSerializer = new JsonObjectSerializer(
new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}));
}
}
使用Newtonsoft.Json库
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.Configure<SignalROptions>(o => o.JsonObjectSerializer = new NewtonsoftJsonObjectSerializer(
new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}));
}
}
关于json - 有没有办法自定义 Azure Function SignalR 服务绑定(bind) (Microsoft.Azure.WebJobs.Extensions.SignalRService) 的 json 设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69101894/