Skip to content

Commit

Permalink
Eensure json_structure works with tbl_json objects closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy committed Aug 26, 2016
1 parent f5575c2 commit 815ce9f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 4 additions & 2 deletions R/json_structure.r
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' an array, then subsequent rows will recursively correspond to the elements
#' (and their children) of the object or array.
#'
#' @param x a tbl_json object
#' @param x a json string or a tbl_json object
#' @return a tbl_json object with the following columns:
#'
#' \code{document.id} 1L if \code{x} is a single JSON string, otherwise the
Expand Down Expand Up @@ -46,6 +46,8 @@
#' '[{"a": 1}, [1, 2], "a", 1, true, null]' %>% json_structure
json_structure <- function(x) {

if (!is.tbl_json(x)) x <- as.tbl_json(x)

# Create initial structure for top level
structure <- json_structure_init(x)

Expand All @@ -67,7 +69,7 @@ json_structure <- function(x) {

json_structure_init <- function(x) {

x %>% as.tbl_json %>%
x %>%
mutate(
parent.id = NA_character_,
level = 1L,
Expand Down
2 changes: 1 addition & 1 deletion man/json_structure.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test-json_structure.r
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,24 @@ test_that("works with empty values appropriately", {
)

})

test_that("works with tbl_json already", {

expect_identical(
c('"a"', '"b"') %>% as.tbl_json %>% json_structure,
tbl_json(
data_frame(
document.id = c(1L, 2L),
parent.id = rep(NA_character_, 2),
level = rep(1L, 2),
index = rep(1L, 2),
child.id = rep("1", 2),
key = rep(NA_character_, 2),
type = rep("string", 2) %>% factor(levels = allowed_json_types),
length = rep(1L, 2)
),
list("a", "b")
)
)

})

0 comments on commit 815ce9f

Please sign in to comment.