forked from colearendt/tidyjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
colearendt#39 first working version of is_json functions
- Loading branch information
Jeremy
committed
Sep 6, 2016
1 parent
adff7b5
commit 810f261
Showing
7 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
|
||
}) |