gpt4 book ai didi

c# - 如何配置 Azure Function v3 使用的默认 JsonSerializerOptions (System.Text.Json)?

转载 作者:行者123 更新时间:2023-12-04 11:11:38 25 4
gpt4 key购买 nike

我有一组在 .net core 3.1 上运行的 Azure Functions v3。

我有一个 JsonSerializerOptions 的自定义配置,我希望在反/序列化数据时由我的函数自动使用它。

问题

如何设置我的 Azure Functions,以便它们可以使用我的默认 System.Text.Json.JsonSerializerOptions 实例?

更新1

根据 @sellotape 的建议,我找到了以下有关 JsonResult 类的文档:

JsonResult documentation

问题是我的 JsonResult 实例没有 object 类型的属性;它只接受 JsonSerializerSettings 实例。

更新2

我仍然收到以下错误,并且我不确定 Newtonsoft 来自哪里:

Microsoft.AspNetCore.Mvc.NewtonsoftJson: Property 'JsonResult.SerializerSettings' must be an instance of type 'Newtonsoft.Json.JsonSerializerSettings'.

最佳答案

对于那些拥有现有 Azure Function 应用程序的人来说,这不是一个完美的解决方案,但Azure Functions for .net 5 isolated runtime现在允许您配置 JSON 序列化选项。因此,对于任何开始新的 Azure 功能或有预算和精力升级现有功能的人来说,现在可以按照您喜欢的方式配置序列化器,提供一流的支持。不幸的是,到目前为止,枚举一直是二等公民,但迟到总比不到好。

程序.cs

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MyFunctionApp
{
public class Program
{
public static void Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.Configure<JsonSerializerOptions>(options =>
{
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.Converters.Add(new JsonStringEnumConverter());
});
})
.Build();

host.Run();
}
}
}

如果您想在那里选择 System.Text.Json 或 Newtonsoft.Json,您可以配置其中之一,如 seen in this example

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core.Serialization;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Configuration
{
public class Program
{
public static void Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults(workerApplication =>
{
// Use any of the extension methods in WorkerConfigurationExtensions.
})
.Build();

host.Run();
}
}

internal static class WorkerConfigurationExtensions
{
/// <summary>
/// Calling ConfigureFunctionsWorkerDefaults() configures the Functions Worker to use System.Text.Json for all JSON
/// serialization and sets JsonSerializerOptions.PropertyNameCaseInsensitive = true;
/// This method uses DI to modify the JsonSerializerOptions. Call /api/HttpFunction to see the changes.
/// </summary>
public static IFunctionsWorkerApplicationBuilder ConfigureSystemTextJson(this IFunctionsWorkerApplicationBuilder builder)
{
builder.Services.Configure<JsonSerializerOptions>(jsonSerializerOptions =>
{
jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
jsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
jsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;

// override the default value
jsonSerializerOptions.PropertyNameCaseInsensitive = false;
});

return builder;
}

/// <summary>
/// The functions worker uses the Azure SDK's ObjectSerializer to abstract away all JSON serialization. This allows you to
/// swap out the default System.Text.Json implementation for the Newtonsoft.Json implementation.
/// To do so, add the Microsoft.Azure.Core.NewtonsoftJson nuget package and then update the WorkerOptions.Serializer property.
/// This method updates the Serializer to use Newtonsoft.Json. Call /api/HttpFunction to see the changes.
/// </summary>
public static IFunctionsWorkerApplicationBuilder UseNewtonsoftJson(this IFunctionsWorkerApplicationBuilder builder)
{
builder.Services.Configure<WorkerOptions>(workerOptions =>
{
var settings = NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.NullValueHandling = NullValueHandling.Ignore;

workerOptions.Serializer = new NewtonsoftJsonObjectSerializer(settings);
});

return builder;
}
}
}

关于c# - 如何配置 Azure Function v3 使用的默认 JsonSerializerOptions (System.Text.Json)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60574428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com