jsonlite icon indicating copy to clipboard operation
jsonlite copied to clipboard

fromJSON does not decode mongo coded dates.

Open ralmond opened this issue 1 year ago • 2 comments

Consider this code:

dt <- Sys.time()
dtl <- fromJSON(toJSON(unbox(dt),POSIXt="mongo"),FALSE)

Returns a list with a name $date and a big integer.

This can be fixed with:

as.POSIXct(dtl$'$date'/1000,origin="1970-01-01")

But it would be nice if fromJSON could note the {"$date":1234} structure and convert it back to POSIX datetime.

ralmond avatar May 25 '24 20:05 ralmond

@ralmond jsonlite already supports parsing mongo dates into POSIXct.

dt <- Sys.time()
dtl <- fromJSON(toJSON(unbox(dt),POSIXt="mongo"), simplifyVector=TRUE)

I'm not exactly sure why the date parsing is tied to simplifyVector. There is an undocumented simplifyDate parameter to turn off the feature, but setting it to TRUE doesn't work unless simplifyVector is also enabled.

jim22k avatar Feb 05 '25 21:02 jim22k

The simplifyVector=FALSE is important for my use case. I'm trying to store S4 objects as documents in a mongo database collection. I find it easier to work with the raw lists rather than a data frame with one row. You can see my code at my mongo project.

ralmond avatar Feb 06 '25 22:02 ralmond