Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto_unbox = T destroyed by POSIXt = 'mongo' in toJSON() #194

Open
nabudaldah opened this issue Jul 31, 2017 · 3 comments
Open

auto_unbox = T destroyed by POSIXt = 'mongo' in toJSON() #194

nabudaldah opened this issue Jul 31, 2017 · 3 comments

Comments

@nabudaldah
Copy link

nabudaldah commented Jul 31, 2017

When doing:

toJSON(list(t = Sys.time()), POSIXt = 'mongo', auto_unbox = T)

I expected:

{"t":{"$date":1501530029411}}

But I get:

{"t":[{"$date":1501530029411}]}

Workaround by using unbox() directly:

toJSON(list(t = unbox(Sys.time())), POSIXt = 'mongo', auto_unbox = T)

@maxto
Copy link

maxto commented Dec 28, 2017

Same issue here #192

it seems a bug in automatic conversion to mongo timestamp

jsonlite::toJSON(list("t" = list("$date" = 1501530029411)),auto_unbox = T)
# {"t":{"$date":1501530029411}} 
jsonlite::toJSON(list("t" = list(list("$date" = 1501530029411))),auto_unbox = T)
#  {"t":[{"$date":1501530029411}]} 

Your workaround converts to scalar, so it works and at the moment i guess we have only these two solutions: unbox or direct list wrapper with $date

Note for unbox : it can be used with atomic vectors of length 1 or data frames with 1 row

So it works with

t1 <- Sys.time()
#  "2017-12-28 11:17:53 CET"

jsonlite::unbox(t1)
#  "2017-12-28 11:17:53"

jsonlite::toJSON(list("t" = jsonlite::unbox(t1)), POSIXt = 'mongo', auto_unbox = T)
#  {"t":{"$date":1514456273863}} 

But it shows an error with

t2 <- as.POSIXct( as.numeric(t1),tz = "CET",origin = "1970-01-01")
#  "2017-12-28 11:17:53 CET"

jsonlite::unbox(t2)
#  Error: Only atomic vectors of length 1 or data frames with 1 row can be unboxed.

This is due to the attribute $tzone

attributes(t2)
#  $class
#  "POSIXct" "POSIXt" 

# $tzone
#  "CET"

# removing attribute
attr(t2,"tzone") <- NULL

jsonlite::unbox(t2)
#  "2017-12-28 11:17:53" #OK

jsonlite::toJSON(list("t" = jsonlite::unbox(t2)), POSIXt = 'mongo', auto_unbox = T)
#  {"t":{"$date":1514456273863}} 

@jeroen
Copy link
Owner

jeroen commented Apr 13, 2020

Maybe the same as jeroen/mongolite#198 ...

@bguillod
Copy link

But it shows an error with

t2 <- as.POSIXct( as.numeric(t1),tz = "CET",origin = "1970-01-01")
#  "2017-12-28 11:17:53 CET"

jsonlite::unbox(t2)
#  Error: Only atomic vectors of length 1 or data frames with 1 row can be unboxed.

This is due to the attribute $tzone

attributes(t2)
#  $class
#  "POSIXct" "POSIXt" 

# $tzone
#  "CET"

# removing attribute
attr(t2,"tzone") <- NULL

jsonlite::unbox(t2)
#  "2017-12-28 11:17:53" #OK

jsonlite::toJSON(list("t" = jsonlite::unbox(t2)), POSIXt = 'mongo', auto_unbox = T)
#  {"t":{"$date":1514456273863}} 

This does not work if the tzone attribute is not your current time zone, as removing the tzone attribute modifies the time value. Any other suggestion how to deal with that case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants