Sending DateTimeOffset from Android to .NET Web API
Asked Answered
P

2

6

I am using Azure Mobile Apps SDK for Android.

public class MyClass {
    public String Id;
    public String Code;
    public DateTimeOffset ClientCreatedAt;
}

MyClass myClass = new MyClass();
myClass.Id = "1234567890";
myClass.Code = "dfgdrvyet";
myClass.ClientCreatedAt = new DateTimeOffset(new Date());

It is being used as follows:

MobileServiceSyncTable<MyClass> myClassSyncTable = _client.getSyncTable(MyClass.class);
ListenableFuture<MyClass> responseFuture = myClassSyncTable.insert(myClass);

Upon insertion, the ClientCreatedAt is set to null, when I investigated the insert statement, it is the Gson within the library that is not serialising the DatetimeOffset, specifically, this line:

JsonObject json = mClient.getGsonBuilder().create().toJsonTree(item).getAsJsonObject();

When I replace the DateTimeOffset with Date, the value is serialised properly.

So, my questions are:

  1. Is it intended in Azure Mobile Apps for me to use the DateTimeOffset and if so, what is the right way to use it?
  2. Can I force Gson to serialise the DateTimeOffset properly? I looked at the Gson Annotations, but nothing that can help there. I am not sure if I should be creating a getter and a setter for serialising and deserialising.
Piraeus answered 1/3, 2016 at 1:28 Comment(0)
M
2

DateTimeOffset is actually a dumb wrapper of Date. Here it is in its entirety.

/**
 * Represents a point in time, typically expressed as a date and time of day
 */
public class DateTimeOffset extends Date {

    public DateTimeOffset(Date date) {
        this.setTime(date.getTime());
    }
}

I threw my hands up in the air and simply decided to use Strings instead, and joda-time's DateTime class via DateTime.parse(string) to handle them.

However the nice alternative is probably to register your own serialiser via MobileServiceClient.registerSerializer(...) but you won't get anything useful out of using their wrapper.

This SDK is rubbish.

edit: Using a custom (de)serialiser seems to work quite well:

private class DateTimeSerialiser implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> {
    @Override
    public JsonElement serialize(final DateTime src, final Type typeOfSrc, final JsonSerializationContext context) {
        return new JsonPrimitive(src.toString());
    }

    @Override
    public DateTime deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
        return DateTime.parse(json.getAsString());
    }
}

Set it up to deserialise/serialise to/from the object of your choice, then set an instance of it via MobileServiceClient.registerSerializer + registerDeserializer. If using local sync, just use ColumnDataType.String or ColumnDataType.DateTimeOffset (it's just String under the hood). Now you can have your field set to the type you want.

private DateTime date;
Maleeny answered 27/6, 2016 at 0:12 Comment(0)
R
0

If it's not to late for answer. I have same issue. You can use
Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String formattedDate = df.format(c.getTime()); to parse date. IMHO would be easier to use Gson and Volley. I think you can read it as string and then convert it to Date.

Roo answered 10/3, 2016 at 1:10 Comment(2)
Is this regarding the DateTimeOffset Azure SDK class?Piraeus
I mean use string in class, but insert date in this format type. Something like this ` public class MyClass{ public String Id; public String ClientCreatedAt; } MyClass inst = new MyClass(); inst.ClientCreatedAt = df.Format(c.getTime);`Roo

© 2022 - 2024 — McMap. All rights reserved.