jeroen / jsonlite

A Robust, High Performance JSON Parser and Generator for R
http://arxiv.org/abs/1403.2805
Other
377 stars 40 forks source link

unbox fails with POSIXlt object #391

Open ralmond opened 2 years ago

ralmond commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.