gpt4 book ai didi

asp.net-core - 如何控制 HotChocolate 中枚举值的序列化?

转载 作者:行者123 更新时间:2023-12-05 01:07:53 48 4
gpt4 key购买 nike

HotChocolate 在所有大写蜗牛情况下序列化枚举值,这导致是枚举值 FooBar 被 Hot Chocolate 推断为 FOO_BAR,但 value.ToString()Enum.GetName(value) 给出 FooBar,Hot Chocolate 似乎忽略了 [EnumMember(Value = "FooBar")]

如何将序列化更改为我想要的任何方式?

最佳答案

HotChocolate server v11 遵循规范建议,默认枚举值被序列化为 UPPER_SNAIL_CASE。

你可以这样改变:

    builder
.AddConvention<INamingConventions>(new YourNamingConvention())

    public class YourNamingConvention
: DefaultNamingConventions
{
public override NameString GetEnumValueName(object value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return value.ToString().ToUpperInvariant(); // change this to whatever you like
}
}

关于asp.net-core - 如何控制 HotChocolate 中枚举值的序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66807364/

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