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

unbox fails with POSIXlt object #391

Open
ralmond opened this issue May 31, 2022 · 2 comments
Open

unbox fails with POSIXlt object #391

ralmond opened this issue May 31, 2022 · 2 comments

Comments

@ralmond
Copy link
Contributor

ralmond commented May 31, 2022

Here is a quick sample which shows the bug:

POSIXlt works as expected:

> time1 <- as.POSIXct("2018-08-16 19:12:19 EDT")
> unbox(time1)
 [x] "2018-08-16 19:12:19"
> 

However, POSIXct fails

time1l <- as.POSIXlt("2018-08-16 19:12:19 EDT")
unbox(time1l)
Error: Only atomic vectors of length 1 or data frames with 1 row can be unboxed.

I can work around by calling jsonlite:::as.scalar on the internal object, but I'm then depending on undocumented internals.

This issue may be at the heart of #192 and #194.

@dcooley
Copy link

dcooley commented Jun 12, 2022

The documentation for unbox() says

Arguments

x atomic vector of length 1, or data frame with 1 row.

Since a POSIXlt object is not an atomic vector of length 1 (it's a list of length 11), I'd say this function is documented and working as expected?

@ralmond
Copy link
Contributor Author

ralmond commented Jun 13, 2022

It may be working as documented, but it is not working as expected. In particular, I tend to think of a POSIXt object in the same way whether it is a POSIXlt or POSIXct object. The fact that the current unbox works differently for the two is unexpected, and forces the developer to think about the underlying representation of the object.

Also, normally both POSIXct and POSIXlt objects are translated into the same format depending on the POSIXt argument to toJSON. So treating the POSIXct object differently in unbox is a bit unexpected.

FInally, it is literally a 3-line patch; I've already submitted the pull request.

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

2 participants