delight-im / Android-DDP

[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Apache License 2.0
274 stars 54 forks source link

Support for dates (parsing + serializing) #82

Open ocram opened 8 years ago

ocram commented 8 years ago

Meteor stores dates as timestamps in milliseconds. The appropriate Java type would be long.

But when a client communicates with the server, the server always sends them in another form and expects them in the same form when it receives data from the client.

What Meteor does is wrapping a plain timestamp such as 1459443366000 in a JSON object containing the date as a property, such as { "$date": 1459443366000 }.

So when you receive a date, the value is not the timestamp itself but the JSON object that you must parse the date from, first.

And when you try to send a date, you can't just put the timestamp right in there but must wrap it in a JSON string, e.g. myMap.put("myDate", "{ \"$date\": 1459443366000 }");.

We should add built-in support to make this easier.

romaluca commented 8 years ago

thanks!

ocram commented 8 years ago

@romaluca Did that solve your problem?

In general, my idea for the solution to this problem would be as follows:

  1. When this library parses the JSON for you (i.e. when you're using the built-in database), it can parse the JSON date for you. It will just replace the { "$date": 1459443366000 } string with a java.util.Date or java.util.Calendar instance, or a long primitive.
  2. Whenever you put a java.util.Date or java.util.Calendar instance into a Map that is used in insert, update or remove, it will be transformed into the corresponding JSON date string. Unfortunately, we can't do this for long primitives, as we don't know if it's a date or something else.

The first change would be a breaking change, but shold be helpful. We still have to decide on the data type to use.

The second change could be implemented right away without any downsides.

Tailmaters commented 7 years ago

@ocram I am sending the date while subscribing but i will get the error "Match Failed" in android. the format i am sending is "1970-01-01 00:00:00 +0000". the same exact date format we are using in IOS it is working fine. can u please give a solution for this as soon as possible. Thank u

ocram commented 7 years ago

@Tailmaters I'm afraid that is not really enough information for us to know what's your problem. Does your server expect a Date instance here? If so, send the date as a string, as shown above:

"{ \"$date\": "+date.getTime()+" }"

Otherwise, your problem is not really relate to this issue.

the same exact date format we are using in IOS it is working fine.

That doesn't mean anything! What works on iOS is probably unrelated to what's working on Android.

Tailmaters commented 7 years ago

@ocram Can u please Share your email-id. We have some Issues with connecting and Fetching Data from the collections.we need to discuss with u how exactly it need to be done.

ocram commented 7 years ago

In general, I prefer public discussions here in the issues which help everybody using this library. If someone has the same problem later, they can benefit as well.

My email address can be found by going to the website linked in my profile and choosing "Contact".

ocram commented 7 years ago

See also: https://github.com/meteor/meteor/blob/b6c2991a28b2ac4e635081133360b55c1e4387f6/packages/ejson/ejson.js#L90