c# - .NET Core 3.0 StringEnumConverter 未序列化为字符串
问题描述
装饰枚举时:
[JsonConverter(typeof(StringEnumConverter))]
public EventEntity Entity { get; set; }
并将其序列化
JsonConvert.SerializeObject(myEvent)
您可能会注意到枚举没有序列化为字符串,而是作为默认整数。
解决方案
如果您使用的是System.Text.Json
没有的纯文本Newtonsoft.JSON
,则此代码段Startup.cs
可能会有所帮助:
// using System.Text.Json.Serialization
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
这里的关键点是 this 中定义的转换器System.Text.Json
(注意类名与 from 不同Newtonsoft.JSON
):JsonStringEnumConverter
推荐阅读
- python - 如何使用 Python zeep 从具有多个绑定的 Python Soap 客户端访问所有 Web 服务方法
- rest - BitBucket 服务器 Rest API 调用以获取 2 个标签之间合并的拉取请求列表
- javascript - WebAudio API:是否可以使用 StereoPanner 节点数据导出 AudioBuffer?
- visual-studio-code - 如何隐藏 VS Code 在花括号后放置的竖线?
- regex - Elisp regexp 如何在换行符后匹配文本?
- bash - 在 Dockerfile 中, RUN /bin/mkdir -p /root/lib/{sh,pl,py,rb} 未能达到预期
- python - 将函数分配给实例的方法
- c - / 操作没有给我结果
- r - R 功能:.Rstudio
- javascript - Javascript - 如何在同一类中的另一个方法中调用方法