开发者

Is there a way to override the Json Seralizer for DateTime format in WCF 4.0

I have an object that has DateTime properties, I am wondering if there is a way to override the default format that gets convert开发者_StackOverflowed for Json responses.


Unfortunately no, the date format is fixed on .NET 3.5 and 4.0.

There is a workaround - which isn't too pretty - which is to declare auxiliary properties to be serialized of type string, similar to the example below. It works, but you need to do it for every DateTime property in your object graph.

[DataContract]
public class MyType
{
    public DateTime MyDTProp { get; set; }
    [DataMember(Name = "MyDTProp")]
    private string strDate
    {
        get
        {
            return this.MyDTProp.ToString("yyyy/MM/dd");
        }
        set
        {
            this.MyDTProp = DateTime.Parse(value);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜