简体   繁体   English

wcf将enum反序列化为字符串

[英]wcf deserialize enum as string

I'm trying to consume a RESTful web service using WCF. 我正在尝试使用WCF使用RESTful Web服务。 I have no control over the format of the web service, so I have to make a few workarounds here and there. 我无法控制Web服务的格式,所以我必须在这里和那里做一些解决方法。 One major problem I cannot seem to get around, however, is how to make WCF deserialize an enum as a string. 然而,我似乎无法解决的一个主要问题是如何使WCF将枚举反序列化为字符串。

This is my code (names changed, obviously): 这是我的代码(显然名称已更改):

[DataContract]
public enum Foo
{
    [EnumMember( Value = "bar" )]
    Bar,

    [EnumMember( Value = "baz" )]
    Baz
}

[DataContract]
public class UNameIt
{
    [DataMember( Name = "id" )]
    public long Id { get; private set; }

    [DataMember( Name = "name" )]
    public string Name { get; private set; }

    [DataMember( Name = "foo" )]
    public Foo Foo { get; private set; }
}

And this is the returned data that fails deserialization: 这是反序列化失败的返回数据:

{
     "id":123456,
     "name":"John Doe",
     "foo":"bar"
}

Finally, the exception thrown: 最后,抛出异常:

There was an error deserializing the object of type Service.Foo. 反序列化Service.Foo类型的对象时出错。 The value 'bar' cannot be parsed as the type 'Int64'. 值'bar'无法解析为'Int64'类型。

I do not want to switch to using the XmlSerializer, because, among its many other shortcomings, it won't let me have private setters on properties. 我不想切换到使用XmlSerializer,因为在它的许多其他缺点中,它不会让我在属性上拥有私有的setter。

How do I make WCF (or, well, the DataContractSerializer) treat my enum as string values? 如何使WCF(或者,DataContractSerializer)将我的枚举视为字符串值?

EDIT : Doing this seems to be impossible, and the behavior is the way it is by design. 编辑 :这似乎是不可能的,行为是它的设计方式。 Thank you Microsoft, for not giving us options, having to resort to hacks. 谢谢微软,不给我们选择,不得不诉诸黑客。 Doing it the way somori suggests seems to be the only way to get string enums with JSON and WCF. 按照somori建议的方式这样做似乎是获得带有JSON和WCF的字符串枚举的唯一方法。

This might be a silly question. 这可能是一个愚蠢的问题。

What happens if you do 如果你这样做会发生什么

[DataMember( Name = "foo" )]
private string foo { get; private set; }

public Foo Foo 
{ 
  get 
  {
    return Foo.Parse(foo);
  }
}

?

I know this is an old post, but I think it worth mentioning. 我知道这是一个老帖子,但我认为值得一提。

I received a similar error where the deserialization of the json string failed, seeming to deserialize to the wrong types. 我收到一个类似的错误,其中json字符串的反序列化失败,似乎反序列化为错误的类型。

The fix for me was to simply URL encode the json string before sending it to the server. 对我来说,修复是在将json字符串发送到服务器之前简单地对其进行URL编码。 A simple but easy mistake to make. 一个简单但容易犯的错误。

HttpUtility.UrlEncode(JSONInstruction) /*remember to encode the string*/

Er..? 呃..? Aren't enums integer? 枚举不是整数? The exception is valid. 例外是有效的。 I dunno if this helps: http://msdn.microsoft.com/en-us/library/aa347875.aspx 我不知道如果这有帮助: http//msdn.microsoft.com/en-us/library/aa347875.aspx

This perhaps is happening because the setter is private and the reported error is misleading. 这可能正在发生,因为setter是私有的,报告的错误是误导性的。 The Foo is the first property to be deserialized and since no public setter is available it throws an exception. Foo是第一个被反序列化的属性,因为没有公共setter可用它会引发异常。

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

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