wcf 将枚举反序列化为字符串

发布于 2024-08-18 11:38:38 字数 1000 浏览 6 评论 0原文

我正在尝试使用 WCF 来使用 RESTful Web 服务。我无法控制 Web 服务的格式,因此我必须到处采取一些解决方法。然而,我似乎无法解决的一个主要问题是如何使 WCF 将枚举反序列化为字符串。

这是我的代码(显然是换了名字):

[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; }
}

这是反序列化失败返回的数据:

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

最后抛出异常:

反序列化 Service.Foo 类型的对象时出错。值“bar”无法解析为“Int64”类型。

我不想改用 XmlSerializer,因为除了它的许多其他缺点之外,它不允许我在属性上拥有私有设置器。

如何使 WCF(或者,DataContractSerializer)将我的枚举视为字符串值?

编辑:这样做似乎是不可能的,并且行为是设计的方式。感谢微软,没有给我们选择,不得不诉诸黑客。按照 somori 建议的方式进行操作似乎是使用 JSON 和 WCF 获取字符串枚举的唯一方法。

I'm trying to consume a RESTful web service using WCF. I have no control over the format of the web service, so I have to make a few workarounds here and there. One major problem I cannot seem to get around, however, is how to make WCF deserialize an enum as a string.

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. The value 'bar' cannot be parsed as the type '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.

How do I make WCF (or, well, the DataContractSerializer) treat my enum as string values?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫性小仙女 2024-08-25 11:38:38

这可能是一个愚蠢的问题。

如果你这样做会发生什么

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

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

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);
  }
}

?

微凉徒眸意 2024-08-25 11:38:38

我知道这是一篇旧文章,但我认为值得一提。

我收到了类似的错误,其中 json 字符串的反序列化失败,似乎反序列化为错误的类型。

对我来说,解决方法是简单地对 json 字符串进行 URL 编码,然后再将其发送到服务器。一个简单但容易犯的错误。

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

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.

The fix for me was to simply URL encode the json string before sending it to the server. A simple but easy mistake to make.

HttpUtility.UrlEncode(JSONInstruction) /*remember to encode the string*/
烟花肆意 2024-08-25 11:38:38

呃..?枚举不是整数吗?例外是有效的。我不知道这是否有帮助: http://msdn.microsoft.com/en-我们/library/aa347875.aspx

Er..? Aren't enums integer? The exception is valid. I dunno if this helps: http://msdn.microsoft.com/en-us/library/aa347875.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文