将枚举序列化为字符串
我有一个枚举:
public enum Action {
Remove=1,
Add=2
}
和一个类:
[DataContract]
public class Container {
[DataMember]
public Action Action {get; set;}
}
当将 Container 实例序列化为 json 时,我得到: {Action:1}
(如果 Action 是“Remove”)。
我想得到: {Action:Remove}
(我需要枚举的 ToString 形式而不是 int)
我可以在不向类中添加其他成员的情况下完成此操作吗?
I have an enum:
public enum Action {
Remove=1,
Add=2
}
And a class:
[DataContract]
public class Container {
[DataMember]
public Action Action {get; set;}
}
When serialize instance of Container to json I get: {Action:1}
(in case Action is Remove).
I would like to get: {Action:Remove}
(instead of int I need to ToString form of the enum)
Can I do it without adding another member to the class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您只需将属性:添加
到未序列化为字符串的枚举属性即可。
或者,如果您想要更奇特的格式,您可以使用下面的属性来告诉 JSON 序列化程序仅序列化您已根据需要格式化的属性。有点取决于您的其余实施。它还识别属性上的 DataMember 属性。
You can just add the attribute:
to the enum property that is not serializing as a string.
or if you have a more exotic formatting in mind you could use the attributes as below to tell the JSON serializer to serialise only the property that you have formatted as you wish. Depends a bit on the rest of your implementation. It recognises the DataMember attribute on a property as well.
使用 Json.Net,您可以将自定义
StringEnumConverter
定义为并序列化为
Using Json.Net, you can define a custom
StringEnumConverter
asand serialize as
这是执行此操作的简单方法:
Here's a simple way to do this:
JSON 格式化程序在处理枚举时具有非常特殊的行为;普通的数据契约属性将被忽略,并将您的枚举视为数字,而不是您期望的其他格式的更易于人类阅读的字符串。虽然这使得处理标志类型枚举变得容易,但它使大多数其他类型更难处理。
来自 MSDN:
解决此问题的唯一实用方法(允许最终用户指定字符串而不是数字)是不在合同中使用枚举。相反,实际的答案是将枚举替换为字符串,并对值执行内部验证,以便可以将其解析为有效的枚举表示之一。
或者(虽然不是假装的),您可以用您自己的格式化程序替换 JSON 格式化程序,这将像其他格式化程序一样尊重枚举。
The JSON formatter has very specialized behaviour when working with enumerations; the normal Data Contract attributes are ignored and it treats your enum as a number, not the more human-readable string you'd expect with other formats. Whilst this makes it easy to deal with flag-type enumerations, it makes most other types much harder to work with.
From MSDN:
The only practical way to resolve this, to allow end-users to specify a string instead of a number, is to not use the enum in your contract. Instead the practical answer is to replace your enum with a string and perform internal validation on the value such that it can be parsed into one of the valid enum representations.
Alternatively (though not for the feint of heart), you could replace the JSON formatter with your own, which would respect enumerations in the same way as other formatters.
如果您使用 .Net 本机 json 序列化程序,即 System.Text.Json.Serialization,那么您可以在 enum 上添加一个属性,以便将 enum 转换为 string 而不是 int。
您应该将以下属性添加到您想要作为字符串的枚举中
If you are using .Net native json serializer i.e. System.Text.Json.Serialization, then you can add an attribute on enum so that it converts enum to string and not int.
You should add following attributes to enum which you want as a string
我一直在使用一个非常好的解决方法,即使用辅助私有属性进行序列化和反序列化,该属性可用于按枚举成员名称或按
EnumMemberAttribute
的值进行序列化。我认为最大的优点是:
获取和设置私有属性
string
而不是int
您的类将如下所示:
EnumHelper.cs
I've been using a very good workaround by using an auxiliary private property for serialization and deserialization that works either for serialization by the enum member name or by the value of the
EnumMemberAttribute
.The greatest advantages I see, are that:
get and set private properties
string
instead of anint
Your class will look like this:
EnumHelper.cs
尝试使用
我不确定这是否适合您的情况,所以我可能是错的。
此处描述: http://msdn.microsoft.com/en-us/library /aa347875.aspx
Try using
I am not sure if this suits your case though, so I might be wrong.
It's described here: http://msdn.microsoft.com/en-us/library/aa347875.aspx
Michal B 发布的解决方案效果很好。这是另一个例子。
您需要执行以下操作,因为描述属性不可序列化。
The solution posted by Michal B works good. Here is another example.
You would need to do the Following as the Description Attribute is not serializable.
出于序列化的目的,如果容器不能包含枚举属性但要填充枚举属性,则可以使用下面的扩展方法。
容器定义
枚举定义
视图中的代码
扩展方法
For serialization purpose, if the container must not contain enumeration properties but are filled with, you can use the extension method below.
Container definition
Enumeration definition
Code in views
Extension method
我已经使用 Newtonsoft.Json 库解决了这个问题。它修复了枚举问题,并且还使错误处理变得更好,并且它适用于 IIS 托管服务而不是自托管服务。它不需要任何更改或向您的 DataContract 类添加任何特殊内容。代码相当多,因此您可以在 GitHub 上找到它:https:// /github.com/jongrant/wcfjsonserializer/blob/master/NewtonsoftJsonFormatter.cs
您必须向您的
Web.config
要使其正常工作,您可以在此处查看示例文件:https://github.com/jongrant/wcfjsonserializer/blob/master/Web.config
I have put a solution to this using the
Newtonsoft.Json
library. It fixes the enum issue and also makes the error handling much better, and it works in IIS hosted services rather than self-hosted ones. It requires no changes or anything special to be added to yourDataContract
classes. It's quite a lot of code, so you can find it on GitHub here: https://github.com/jongrant/wcfjsonserializer/blob/master/NewtonsoftJsonFormatter.csYou have to add some entries to your
Web.config
to get it to work, you can see an example file here:https://github.com/jongrant/wcfjsonserializer/blob/master/Web.config