WCF客户端如何从JSON响应中反序列化不兼容的日期格式? [英] WCF Client how can I deserialize an incompatible date format from the JSON response?

查看:201
本文介绍了WCF客户端如何从JSON响应中反序列化不兼容的日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了网络的信息,但大部分的结果是关于创建WCF服务或服务在您的控制下的情况。

I have scoured the Net for info on this, but most of the results are about creating WCF services or situations where the service is under your control.

我是构建一个无法控制的RESTful JSON服务的WCF客户端代理。我正在使用基本的ServiceContract / DataContract模式,并尝试让框架尽可能多的工作。

I am building a WCF client proxy for a RESTful JSON service which is out of my control. I am using the basic ServiceContract/DataContract pattern, and trying to let the framework do as much of the work as possible.

大多数情况下,这是正常的,但所有的datetime来自该外部服务的字段是特定的格式,例如

Mostly this is working fine, but all the datetime fields coming from this external service are in a particular format, e.g.

{"SomeObject": 
    {"details":"blue and round", "lastmodified":"2013/01/02 23:14:55 +0000"}
}

所以我收到一个错误:


反序列化MyNamespace.SomeObject类型的对象有一个错误。 DateTime内容'2013/01/02 23:14:55 +0000'不按照JSON的要求以'/ Date('结尾')/'开头。

There was an error deserializing the object of type MyNamespace.SomeObject. DateTime content '2013/01/02 23:14:55 +0000' does not start with '/Date(' and end with ')/' as required for JSON.'.

我的数据收集是:

namespace Marshmallow.WebServices.ServiceModels
{
    [DataContract]
    public class SomeObject
    {
        [DataMember(Name = "details")]
        public string Details { get; set; }

        [DataMember(Name = "lastmodified")]
        public DateTime LastModified { get; set; }
    }
}

我的servicecontract是:

My servicecontract is:

[ServiceContract]
public interface ICoolExternalApi
{
    [OperationContract]
    [WebGet(UriTemplate = "/something.json",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    [return: MessageParameter(Name = "SomeObject")]
    SomeObject GetAccount();
}

我想知道的是,我可以在哪里贴一些代码定义如何WCF应该反序列化最后修改的字段(使一个DateTime对象离开字符串)?

或者更好的是,定义如何反序列化所有DateTime DataMembers对于我所有的DataContracts。我不想要很多重复的代码。

Or better yet, define how to deserialize all DateTime DataMembers for all my DataContracts. I do not want a lot of repeated code.

我也不想诉诸一些第三方的解串器,也不想通过自定义反序列化方法,如果是可以避免的。

I also do not want to resort to some third-party deserializer nor do I want to start putting everything else through a custom deserialization method, if it is avoidable.

推荐答案

到目前为止,这是我想出的最好的:

So far this is the best I have come up with:

我有一个内部字符串扩展方法:

I have an internal string Extension method:

internal static class DeserializationHelper
{
    internal static DateTime GetDeserializedDateTime(this string @string)
    {
        if (string.IsNullOrEmpty(@string)) return default(DateTime);
        //insert complex custom deserialization logic here
        return DateTime.Parse(@string);
    }

}

这是DataMember设置: / p>

This is the DataMember setup:

[DataMember(Name = "lastmodified")]
internal string _LastModified 
{
    set { LastModified = value.GetDeserializedDateTime(); }
    //getter is not needed for receiving data but WCF requires one
    get { return default(string); }
}

public DateTime LastModified { get; private set; }

如果要使用此DataContract发送数据(使其成为可写属性),您将必须编写一个DateTime扩展方法(GetSerializedDateString),扩展setters / getter并引入私有成员作为go-betweens。

If you wanted to use this DataContract for sending data (make this a writeable Property), you would have to write a DateTime Extension method (GetSerializedDateString), expand the setters/getters and introduce private members as go-betweens.

它闻起来是剪切和粘贴,它是不利用任何WCF框架功能。比尔·盖茨会做什么?

It smells of cut and paste, and it does not take advantage of any WCF framework features. What would Bill Gates do?

这篇关于WCF客户端如何从JSON响应中反序列化不兼容的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆