Android-DDP
Android-DDP copied to clipboard
Support for dates (parsing + serializing)
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.
thanks!
@romaluca Did that solve your problem?
In general, my idea for the solution to this problem would be as follows:
- 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 ajava.util.Date
orjava.util.Calendar
instance, or along
primitive. - Whenever you put a
java.util.Date
orjava.util.Calendar
instance into aMap
that is used ininsert
,update
orremove
, it will be transformed into the corresponding JSON date string. Unfortunately, we can't do this forlong
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.
@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
@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.
@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.
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".
See also: https://github.com/meteor/meteor/blob/b6c2991a28b2ac4e635081133360b55c1e4387f6/packages/ejson/ejson.js#L90