Support for java.util.Date in JsonElement is absent in lattest release

123 views
Skip to first unread message

Haghard

unread,
Mar 29, 2014, 4:12:58 AM3/29/14
to ve...@googlegroups.com
What should i do it mongodb return result map with java.util.Date value but this is unsupported in json conversion.

 

class  JsonElement
.
.
.
56      } else if (obj == null || obj instanceof String || obj instanceof Number || obj instanceof Boolean) {

Commit id

7228719d (purplefox 2014-02-26 10:07:44 +0000 56)

Paulo Lopes

unread,
Mar 29, 2014, 3:17:23 PM3/29/14
to ve...@googlegroups.com
This is a feature that has been brought up lots of time, I think that for the next big version there should be some discussions on the json API either allow adding custom encoders do one can add a date encoder that complies to ecmascript spec 404 (json) where dates are converted to: YYYY-MM-DDTHH:mm:ss.sssZ

and it would be nice to have a way to register decoders so one could say:

public <T> T get(String fieldName, Class<T> clazz) {
  return getDecoderFor(clazz).decode(map.get(fieldName);
}

Or any other way that would not be too intrusive to the API...

Haghard

unread,
Mar 29, 2014, 3:54:15 PM3/29/14
to ve...@googlegroups.com

Yes, I have found discussion about this issue 
https://bugs.eclipse.org/bugs/show_bug.cgi?id=425613 

Thank you anyway

суббота, 29 марта 2014 г., 12:12:58 UTC+4 пользователь Haghard написал:

Guilherme Ceschiatti Barbosa Moreira

unread,
Jun 23, 2016, 10:04:31 PM6/23/16
to vert.x, hagh...@gmail.com
You could just convert into an object that Mongo undertands:

def dateToMongoDate(date: java.util.Date): JsonObject = {
val obj = new JsonObject()
obj.put("$date", s"${date.toInstant}")
obj
}

This is Scala code, but almost the same in Java.

Guilherme Ceschiatti Barbosa Moreira

unread,
Jun 23, 2016, 10:06:55 PM6/23/16
to vert.x, hagh...@gmail.com
And then use it to create your doc:

val doc = new JsonObject()
.put("symbol", symbol)
.put("price", price)
.put("volume", volume)
.put("trade_time_str", tradeTimeStr) // from cedro
.put("trade_time", dateToMongoDate(marketDataCalendar.getTime)) // from cedro but converted to db type
.put("trade_time_dtc", tradeTimeDtc) // dtc format
.put("insert_time", dateToMongoDate(insertTime))
.put("bidask", bidask)
.put("buyer", buyer)
.put("seller", seller)
.put("trade_id", tradeId)

Regards,
6qat
Reply all
Reply to author
Forward
0 new messages