Submit DateTimeOffset from Android to .NET Web API

I am using the 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()); 

Used as follows:

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

When inserting, ClientCreatedAt set to null when I examined the insert statement, it is Gson inside the library that does not serialize DatetimeOffset, in particular this line:

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

When I replace DateTimeOffset with Date , the value is serialized correctly.

So my questions are:

  • Azure Mobile Apps uses DateTimeOffset for me, and if so, what is the way to use it?
  • Can I get Gson to serialize DateTimeOffset correctly? I looked at Gsson's annotations, but nothing that could help there. I'm not sure if I should create a getter and setter for serialization and deserialization.
+5
source share
2 answers

DateTimeOffset is actually a dumb Date wrapper. Here he is completely.

 /** * 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 raised my hands up and just decided instead to use Strings and the DateTime class of joda-time via DateTime.parse(string) to process them.

However, a good alternative is probably to register your own serializer with MobileServiceClient.registerSerializer(...) , but you will not get anything useful in using your wrapper.

This SDK is trash.

edit: Using a custom (de) serializer 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 to deserialize / serialize to / from the object of your choice, then set its instance using MobileServiceClient.registerSerializer + registerDeserializer . If you use local synchronization, just use ColumnDataType.String or ColumnDataType.DateTimeOffset (it's just a line under the hood). Now you can specify your field in the type you need.

 private DateTime date; 
+2
source

If it is not too late for an answer. I have the same problem. 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()); Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String formattedDate = df.format(c.getTime()); for date analysis. IMHO it would be easier to use Gsson and Will. I think you can read it as a string and then convert to Date.

0
source

Source: https://habr.com/ru/post/1244166/


All Articles