Skip to content

Commit

Permalink
colearendt#39 first working version of is_json functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy committed Sep 6, 2016
1 parent adff7b5 commit 810f261
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 2 deletions.
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export(enter_object)
export(gather_array)
export(gather_keys)
export(is.tbl_json)
export(is_json_array)
export(is_json_logical)
export(is_json_null)
export(is_json_number)
export(is_json_object)
export(is_json_scalar)
export(is_json_string)
export(jlogical)
export(jnumber)
export(json_complexity)
Expand Down
70 changes: 70 additions & 0 deletions R/is_json.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' Factory to create \code{is_json} functions
#' @param desired.types character vector of types we want to check for
#' @return a function
is_json_factory <- function(desired.types) {

function(.x) {
types <- .x %>% json_types %>% extract2("type")
types %in% desired.types
}

}

#' Predicates to test for specific JSON types in \code{\link{tbl_json}} objects
#'
#' These functions are often useful with \code{\link[dplyr]{filter}} to
#' filter complex JSON by type before applying \code{\link{gather_keys}} or
#' \code{\link{gather_array}}.
#'
#' @seealso \code{\link{json_types}} for creating a new column to identify the
#' type of every JSON document
#' @name is-json
#' @param .x a json string or \code{\link{tbl_json}} object
#' @return a logical vector
#' @examples
#'
#' # Test a simple example
#' json <- '[1, "string", true, [1, 2], {"key": "value"}, null]' %>% gather_array
#' json %>% is_json_number
#' json %>% is_json_array
#' json %>% is_json_scalar
#'
#' # Use with filter
#' json %>% filter(is_json_object(.))
#'
#' # Combine with filter in advance of using gather_array
#' companies[1:5] %>% gather_keys %>% filter(is_json_array(.))
#' companies[1:5] %>% gather_keys %>% filter(is_json_array(.)) %>% gather_array
#'
#' # Combine with filter in advance of using gather_keys
#' companies[1:5] %>% gather_keys %>% filter(is_json_object(.))
#' companies[1:5] %>% gather_keys %>% filter(is_json_object(.)) %>% gather_keys("key2")
NULL

#' @rdname is-json
#' @export
is_json_string <- is_json_factory("string")

#' @rdname is-json
#' @export
is_json_number <- is_json_factory("number")

#' @rdname is-json
#' @export
is_json_logical <- is_json_factory("logical")

#' @rdname is-json
#' @export
is_json_null <- is_json_factory("null")

#' @rdname is-json
#' @export
is_json_array <- is_json_factory("array")

#' @rdname is-json
#' @export
is_json_object <- is_json_factory("object")

#' @rdname is-json
#' @export
is_json_scalar <- is_json_factory(c("string", "number", "logical"))
2 changes: 1 addition & 1 deletion R/json_lengths.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#'
#' @seealso \code{\link{json_complexity}} to compute the recursive length of
#' each value
#' @param .x a json string or tbl_json object
#' @param .x a json string or \code{\link{tbl_json}} object
#' @param column.name the name to specify for the length column
#' @return a \code{\link{tbl_json}} object
#' @export
Expand Down
62 changes: 62 additions & 0 deletions man/is-json.Rd

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

18 changes: 18 additions & 0 deletions man/is_json_factory.Rd

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

2 changes: 1 addition & 1 deletion man/json_lengths.Rd

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

33 changes: 33 additions & 0 deletions tests/testthat/test-is_json.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
context("is_json")

test_that("works for a simple example", {

json <- '[1, "string", true, [1, 2], {"key": "value"}, null]' %>%
gather_array

expect_identical(json %>% is_json_number %>% which, 1L)
expect_identical(json %>% is_json_string %>% which, 2L)
expect_identical(json %>% is_json_logical %>% which, 3L)
expect_identical(json %>% is_json_array %>% which, 4L)
expect_identical(json %>% is_json_object %>% which, 5L)
expect_identical(json %>% is_json_null %>% which, 6L)
expect_identical(json %>% is_json_scalar %>% which, c(1L, 2L, 3L))

})

test_that("works with filter", {

json <- '[1, "string", true, [1, 2], {"key": "value"}, null]' %>%
gather_array

expect_identical(
json %>% filter(is_json_array(.)),
json %>% slice(4)
)

expect_identical(
json %>% filter(is_json_object(.)),
json %>% slice(5)
)

})

0 comments on commit 810f261

Please sign in to comment.