jsonlite icon indicating copy to clipboard operation
jsonlite copied to clipboard

unbox fails with POSIXlt object

Open ralmond opened this issue 3 years ago • 2 comments

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.

ralmond avatar May 31 '22 15:05 ralmond

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?

dcooley avatar Jun 12 '22 22:06 dcooley

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.

ralmond avatar Jun 13 '22 20:06 ralmond