Skip to content

Commit 8085435

Browse files
authored
Added check for POSIXt objects to unbox. (jeroen#392)
* Added check for POSIXt objects to `unbox`. Added tests for `unbox` and Data objects. * changed is to inherits * Changed is to inherits
1 parent 6773daa commit 8085435

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

R/unbox.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ unbox <- function(x){
4141
stop("Tried to unbox dataframe with ", nrow(x), " rows.")
4242
}
4343
}
44+
if (length(x) == 1L && inherits(x,"POSIXt")) {
45+
return (as.scalar(x))
46+
}
4447
if(is.null(x) || !is.atomic(x) || length(dim(x)) > 1){
4548
stop("Only atomic vectors of length 1 or data frames with 1 row can be unboxed.")
4649
}

tests/testthat/test-fromJSON-date.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,21 @@ test_that("fromJSON date objects", {
1515
expect_that(fromJSON(toJSON(mydf, POSIXt="mongo", na="null"))$x, is_a("POSIXct"))
1616
expect_that(fromJSON(toJSON(mydf, POSIXt="mongo"))$x, equals(x))
1717

18+
xct <- as.POSIXct(x)
19+
xlt <- as.POSIXlt(x)
20+
21+
expect_equal(xct, xlt)
22+
expect_true(unbox(xct[1]) == unbox(xlt[1]))
23+
xct3un <- unbox(xct[3])
24+
expect_true(is.na(xct3un) && inherits(xct3un,"scalar") &&
25+
inherits(xct3un,"POSIXt"))
26+
xlt3un <- unbox(xlt[3])
27+
expect_true(is.na(xlt3un) && inherits(xlt3un,"scalar") &&
28+
inherits(xlt3un,"POSIXt"))
29+
30+
expect_equal(toJSON(xct,POSIXt="mongo"),toJSON(xlt,POSIXt="mongo"))
31+
expect_equal(toJSON(unbox(xct[1]),POSIXt="mongo"),
32+
toJSON(unbox(xlt[1]),POSIXt="mongo"))
33+
1834
});
35+

0 commit comments

Comments
 (0)