简体   繁体   English

在WCF中需要对象时,将JSON枚举从JSON反序列化为C#

[英]Deserialize enum from JSON to C# when object is expected in WCF

I have a filter object in C#, that contains a dictionary of strings (that represent the propery name) and the corresponding value (expecting an object). 我在C#中有一个过滤器对象,其中包含一个字符串字典(代表属性名称)和相应的值(期望一个对象)。

When sending an object of a custom type with JSON to WCF, I use __type in my JSON to deserialize correctly from the JSON object to a C# object. 将带有JSON的自定义类型的对象发送到WCF时,我在JSON中使用__type将JSON对象正确反序列化为C#对象。 However, when sending a enum (represented as integer in JSON) this will be serialized as integer in C#, because the deserializer has no way of telling what kind of enum it is expecting. 但是,在发送枚举(在JSON中表示为整数)时,它将在C#中被序列化为整数,因为解串器无法告知期望的枚举类型。

The JSON I send: 我发送的JSON:

{
    "filterList": [
        { "FilterProperty": "Status", "FilterValue": 4 }
        { "FilterProperty": "Status", "FilterValue": 6 }
    ]
}    

The DataContract it deserializes to a list of these: 它反序列化DataContract到以下列表:

public class Filter
{
        [DataMember]
        public string FilterProperty { get; set; }
        [DataMember]
        public object FilterValue { get; set; }
}

The FilterValue corresponds in this specific situation with the an enum. 在此特定情况下,FilterValue与枚举对应。 But because the FilterValue property is defined as object, the deserializer does not know how to deserialize it without additional information. 但是因为FilterValue属性被定义为对象,所以反序列化器在没有其他信息的情况下不知道如何反序列化。

Is it possible to send additional information with JSON to let WCF correctly deserialize the enum even if an object is expected? 是否可以使用JSON发送其他信息,以使WCF正确反序列化枚举,即使需要对象也是如此?

Or is the only way to solve this to use reflection to resolve the type from the filter property name? 还是解决此问题的唯一方法是使用反射从过滤器属性名称解析类型?

No, there is no extra information you can embed in the JSON to instruct deserializers to set the type of a property of type object to anything other than object . 不,还有,你可以在JSON中嵌入指示反序列化到类型的属性的类型设置任何额外信息object比其他任何object

If you need to be able to support multiple types polymorphically across the WCF service boundary you can define an interface to represent your type, in this case something like IFilterValue , and then use the ServiceKnownType attribute to tell WCF about the supported implementations of the interface. 如果您需要能够跨WCF服务边界多态地支持多种类型,则可以定义一个接口来表示您的类型,在这种情况下,例如IFilterValue ,然后使用ServiceKnownType属性告诉WCF有关接口支持的实现。

Hope this is of help. 希望这会有所帮助。

EDIT 编辑

It seems to me another more interoperable solution would be to do something like: 在我看来,另一个更可互操作的解决方案将是执行以下操作:

public class Filter
{
    [DataMember]
    public string FilterProperty { get; set; }

    [DataMember]
    public string Type { get; set; }

    [DataMember]
    public string FilterValue { get; set; }
}

This means that rather than unboxing an object you can do an explicit string conversion which strikes me as a better option. 这意味着您可以进行显式的字符串转换,而不是对对象进行拆箱,这对我来说是一个更好的选择。

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

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