2

I want to deserialize and serialize a unix timestamp to a DateTime, what's the backing variable name that I should use in the getter? I'm using the standard Microsoft ToDo quickstart with Xamarin.

[JsonProperty(PropertyName = "date")]
public DateTime Date
{
    get
    {
        System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
        dtDateTime = dtDateTime.AddSeconds(WHATGOESHERE).ToLocalTime();
        return dtDateTime;
    }
    set => ((DateTimeOffset)value).ToUnixTimeSeconds();
}
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • You need to use classic property with explicit backing field. The backing field cannot be referenced if you use automatic property. – Dusan Jun 15 '17 at 12:33
  • will that automacially still work with the newtonsoft JSON? – MonkeyBonkey Jun 15 '17 at 12:38
  • @MonkeyBonkey Json.NET only cares about the property; what your code does *inside* that doesn't concern it. Slight caveat there: if you have a get only automatically implemented property (`public DateTime Date {get;}`) - many serializers will know how to store to that value by using internal Roslyn details – Marc Gravell Jun 15 '17 at 12:40
  • So I guess the part that I'm a bit confused about is to make sure it is stored in the database as a unix timestamp - if I create the backing variable, will it serialize to a DateTime to store in the JSON - I need it to be a unix timestamp in the JSON. I think I'm assuming a lot of Newtonsoft magic here which might not be the case. – MonkeyBonkey Jun 15 '17 at 12:42
  • You could also bind this json value to property of type `long`, And use another get-only property which converts this timestamp to actual `DateTime`. – Dusan Jun 15 '17 at 12:45
  • yeah that seems like a simpler plan than creating a json serializer – MonkeyBonkey Jun 15 '17 at 12:49

1 Answers1

1

If you aren't using "automatically implemented properties" (i.e. {get;set;}), then the backing field is whatever you create. It isn't done by the compiler. So: declare a field and use that.

Note that your set doesn't actually assign the computed value to anything currently - it just does the math and drops the value on the floor. That also needs to assign to the backing field.

I'll be honest though; I'm not sure why this isn't just:

[JsonProperty("date")]
public DateTime Date {get;set;}

It looks like you're trying to convert it such that it is DateTime in the serialization but epoch seconds inside the type. That sounds... unusual and odd. If you really want that, though, just declare an integer (long, presumably) for your field, and read/write accordingly.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • in the json it's a unixtimestamp and in the class I want it to be a DateTime - I want it to stay a unixtimestamp in the Cosmos database – MonkeyBonkey Jun 15 '17 at 12:39
  • @MonkeyBonkey k; you're doing it backawards, then; the good news is you don't need to do *anything* here - this is simply a Json.NET configuration setting... one moment while I look it up – Marc Gravell Jun 15 '17 at 12:41
  • @MonkeyBonkey edit: huh, my bad - Json.NET only supports ISO and MS - I must have been thinking of JIL (anther JSON serializer); but: see this question for ways to do it in Json.NET via custom serializers: https://stackoverflow.com/questions/19971494/how-to-deserialize-a-unix-timestamp-%CE%BCs-to-a-datetime (adjust the code shown to use seconds, obviously) – Marc Gravell Jun 15 '17 at 12:43
  • Ah ok - I was mistakenly assuming some magic happens in the getter setter with Newtonsoft. – MonkeyBonkey Jun 15 '17 at 12:45