Search code examples
c#jsonrestpropertiesdatacontract

DataMember Property with Custom Get?


I'm going by this tutorial and trying to figure out how to have a DataMember without an auto property. Basically I have a field is a date time in epoch format and I want the property to be a DateTime so I'm trying to do the conversion in the property's get. I'm not sure how to format this exactly.

Since Code was requested please look at the following. :

// The date looks like this in the JSON 
"someEpochDateTime": 1428785212000,

// I thought I could work around it using the following code, however
// I get a warning saying someEpochDateTime is never set.

[DataMember(Name = "someEpochDateTime")]
private long someEpochDateTime;

public DateTime test
{
get { return DateTimeConverter.FromUnixTime(someEpochDateTime); }
}

Using FromUnixTime


Solution

  • Apparently my last edit actually works as a solution, I just get a compiler warning for some reason.

    [DataMember(Name = "someEpochDateTime")]
    private long someEpochDateTime;
    
    public DateTime test
    {
        get { return DateTimeConverter.FromUnixTime(someEpochDateTime); }
    }