简体   繁体   English

C# 可以使用 Json 序列化枚举描述而不是枚举值

[英]C# Possible To Serialise Enum description instead Enum value With Json

I have the situation where I am receiving a response from two different web services.我遇到过从两个不同的 Web 服务收到响应的情况。 The two responses are identical in structure, but have different parent namespaces.这两个响应在结构上是相同的,但具有不同的父命名空间。 What I am trying to do is convert these responses into a standard class I can then pass out to the parent object.我想要做的是将这些响应转换为一个标准类,然后我可以传递给父对象。 I figured the simplest way was to Serialize the incoming object to Json, and then Deserialize the object into my standard object.我认为最简单的方法是将传入的对象序列化为 Json,然后将对象反序列化为我的标准对象。 This keeps my processing code rather simple这使我的处理代码相当简单

var jsonString = JsonConvert.SerializeObject(serviceResponse.Results);
var commonObject = JsonConvert.DeserializeObject<StandardResult>(jsonString);

And for the most part this is working.在大多数情况下,这是有效的。 However I'm expanding the StandardResult object I've discovered a bit of a problem.但是,我在扩展 StandardResult 对象时发现了一些问题。

One of the properties of the object is a class with the following properties:对象的属性之一是具有以下属性的类:

public class EntryDetailType
{
public string EntryNumber {get; set;}
public string EntryText {get; set;
public int Item {get; set;}
}

The issue is that Item can actually be one of two different enums in the incoming data.问题是Item实际上可以是传入数据中的两个不同枚举之一。 So although the conversion is correctly putting the enum's value in Item I have no idea which enum that value refers to.因此,尽管转换正确地将枚举的值放入Item我不知道该值指的是哪个枚举。 I'm not at this stage using a JsonConverter class as the object is very large and I wanted to avoid that if possible.在这个阶段我没有使用 JsonConverter 类,因为对象非常大,如果可能的话,我想避免这种情况。

Is there a method of being able to get the enum's description instead of the value as part of this process, or am I going to be forced to write my own json converters?作为此过程的一部分,是否有一种方法可以获取枚举的描述而不是值,还是我将被迫编写自己的 json 转换器? Please bear in mind that the EntryDetailType class is not in my project.请记住, EntryDetailType类不在我的项目中。 It is the return type from a web service that has been added via a wsdl document.它是通过 wsdl 文档添加的 Web 服务的返回类型。

Well, if the class EntryDetailType really has a property named Item of type int , it is not possible to infer which name representation value of the enum you have.好吧,如果类EntryDetailType确实有一个名为Item的类型为int的属性,则无法推断您拥有的enum哪个名称表示值。

But, if this class has a property Item of any enum type, then it is as easy as serializing to Json using the built-in StringEnumConverter as the second parameter of the SerializeObject method:但是,如果该类具有任何enum类型的属性Item ,那么它就像使用内置StringEnumConverter作为 SerializeObject 方法的第二个参数序列化为 Json 一样简单:

var jsonString = JsonConvert.SerializeObject(serviceResponse.Results, new Newtonsoft.Json.Converters.StringEnumConverter()); 

ps: I'm assuming you are using the Json.NET from NewtonSoft. ps:我假设您使用的是 NewtonSoft 的 Json.NET。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM