Search code examples
c#datacontract

DataContract and custom set/get to set a DateTime from a string


I need to parse JSON data from a server and make a list of object istances.

I'm using DataContract in order to make an association between json dictionary fields and class properties, however I've a problem: one of these fields contains a date in string from (something like "2011-01-01 15:00 UTC"); I wanna put this inside a DateTime property.

How can I convert this string to a datetime and pass it the property automatically using DataContract? is that possible?


Solution

  • You could use a property for the purpose:

    [DataMember(Name="Foo")]
    public string FormattedFoo {
        get { return /* apply some custom formatting to 'Foo' */; }
        set { Foo = /* apply some custom parsing to 'value' */; }
    }
    public DateTime Foo {get;set;}