Skip to content

Commit df7e711

Browse files
committed
overhaul deserialize.cpp for extremely flexible querie, generalize decompression
1 parent 54707fc commit df7e711

File tree

8 files changed

+872
-456
lines changed

8 files changed

+872
-456
lines changed

R/RcppExports.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

4-
.deserialize_json <- function(json, json_pointer = "", empty_array = NULL, empty_object = NULL, single_null = NULL, error_ok = FALSE, on_error = NULL, simplify_to = 0L, type_policy = 0L, int64_r_type = 0L) {
5-
.Call(`_RcppSimdJson_deserialize_json`, json, json_pointer, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type)
4+
.deserialize_json <- function(json, query = NULL, empty_array = NULL, empty_object = NULL, single_null = NULL, error_ok = FALSE, on_error = NULL, simplify_to = 0L, type_policy = 0L, int64_r_type = 0L) {
5+
.Call(`_RcppSimdJson_deserialize`, json, query, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type)
66
}
77

8-
.load_json <- function(file_path, json_pointer = "", empty_array = NULL, empty_object = NULL, single_null = NULL, error_ok = FALSE, on_error = NULL, simplify_to = 0L, type_policy = 0L, int64_r_type = 0L) {
9-
.Call(`_RcppSimdJson_load_json`, file_path, json_pointer, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type)
8+
.load_json <- function(json, query = NULL, empty_array = NULL, empty_object = NULL, single_null = NULL, error_ok = FALSE, on_error = NULL, simplify_to = 0L, type_policy = 0L, int64_r_type = 0L) {
9+
.Call(`_RcppSimdJson_load`, json, query, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type)
1010
}
1111

1212
.exceptions_enabled <- function() {

R/fload.R

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
file_ext = .file_extension(x)
3636
)
3737

38-
init$compressed <- tolower(init$file_ext) %in% c(".gz", ".bz", ".bz2", ".xz", ".zip")
39-
if (any(init$compressed)) {
40-
stop(
41-
"Compressed files are not yet supported. The following files are affected:",
42-
sprintf("\n\t- %s", x[init$compressed]),
43-
call. = FALSE
44-
)
45-
}
38+
# init$compressed <- tolower(init$file_ext) %in% c(".gz", ".bz", ".bz2", ".xz", ".zip")
39+
# if (any(init$compressed)) {
40+
# stop(
41+
# "Compressed files are not yet supported. The following files are affected:",
42+
# sprintf("\n\t- %s", x[init$compressed]),
43+
# call. = FALSE
44+
# )
45+
# }
4646

4747
init$type <- ifelse(
4848
!is.na(init$url_prefix), "url",
@@ -85,7 +85,7 @@
8585
#'
8686
#' @export
8787
fload <- function(json,
88-
query = "",
88+
query = NULL,
8989
empty_array = NULL,
9090
empty_object = NULL,
9191
single_null = NULL,
@@ -105,11 +105,11 @@ fload <- function(json,
105105
if (length(json) == 1L) return(json) else return(as.list(json))
106106
}
107107

108-
if (is.null(query)) {
109-
query <- ""
110-
} else if (!.is_scalar_chr(query)) {
111-
stop("`query=` must be a single, non-`NA` `character`.")
112-
}
108+
# if (!is.null(query) && !()) {
109+
# query <- ""
110+
# } else if (!.is_scalar_chr(query)) {
111+
# stop("`query=` must be a single, non-`NA` `character`.")
112+
# }
113113

114114
if (!.is_scalar_lgl(error_ok)) {
115115
stop("`error_ok=` must be either `TRUE` or `FALSE`.")
@@ -217,8 +217,8 @@ fload <- function(json,
217217
}
218218
# load =======================================================================
219219
.load_json(
220-
file_path = input,
221-
json_pointer = query,
220+
json = input,
221+
query = query,
222222
empty_array = empty_array,
223223
empty_object = empty_object,
224224
error_ok = error_ok,

R/fparse.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
#' }
2222
#' }
2323
#'
24-
#' @param query String used as a JSON Pointer to identify a specific element within \code{json}.
25-
#' \code{character(1L)}, default: \code{""}
24+
#' @param query If not \code{NULL}, a string used as a JSON Pointer to identify a
25+
#' specific element within \code{json}.
26+
#' \code{character(1L)}, default: \code{NULL}
2627
#'
2728
#' @param empty_array Any R object to return for empty JSON arrays.
2829
#' default: \code{NULL}
@@ -215,7 +216,7 @@
215216
#'
216217
#' @export
217218
fparse <- function(json,
218-
query = "",
219+
query = NULL,
219220
empty_array = NULL,
220221
empty_object = NULL,
221222
single_null = NULL,
@@ -234,10 +235,18 @@ fparse <- function(json,
234235
stop("`json=` must be a `character` or `raw`.")
235236
}
236237

237-
if (is.null(query)) {
238-
query <- ""
239-
} else if (!.is_scalar_chr(query)) {
240-
stop("`query=` must be a single, non-`NA` `character`.")
238+
if (!is.null(query)) {
239+
if (is.character(query)) {
240+
if (anyNA(query)) {
241+
stop("`query=` should not contain any `NA`s.")
242+
}
243+
} else if (is.list(query)) {
244+
if (any(vapply(query, function(.x) !is.character(.x) || anyNA(.x), logical(1L)))) {
245+
stop("If `query=` is a list, it should only contain `character` vectors with no-`NA`s.")
246+
}
247+
} else {
248+
stop("`query=` must be `NULL`, a `character` vector, or a list of `character` vectors.")
249+
}
241250
}
242251

243252
if (!.is_scalar_lgl(error_ok)) {
@@ -299,7 +308,7 @@ fparse <- function(json,
299308
# deserialize ==============================================================
300309
.deserialize_json(
301310
json = json,
302-
json_pointer = query,
311+
query = query,
303312
empty_array = empty_array,
304313
empty_object = empty_object,
305314
single_null = single_null,

inst/tinytest/test_deserialization.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,15 +1025,15 @@ expect_identical(
10251025
)
10261026
expect_identical(
10271027
RcppSimdJson:::.deserialize_json(
1028-
test,
1028+
test,
10291029
type_policy = type_policy$ints_as_dbls,
10301030
simplify_to = simplify_lvl$matrix
10311031
),
10321032
target
10331033
)
10341034
expect_identical(
10351035
RcppSimdJson:::.deserialize_json(
1036-
test,
1036+
test,
10371037
type_policy = type_policy$strict,
10381038
simplify_to = simplify_lvl$matrix
10391039
),
@@ -1047,15 +1047,15 @@ expect_identical(
10471047
)
10481048
expect_identical(
10491049
RcppSimdJson:::.deserialize_json(
1050-
test,
1050+
test,
10511051
type_policy = type_policy$ints_as_dbls,
10521052
simplify_to = simplify_lvl$vector
10531053
),
10541054
target
10551055
)
10561056
expect_identical(
10571057
RcppSimdJson:::.deserialize_json(
1058-
test,
1058+
test,
10591059
type_policy = type_policy$strict,
10601060
simplify_to = simplify_lvl$vector
10611061
),
@@ -1069,18 +1069,18 @@ expect_identical(
10691069
)
10701070
expect_identical(
10711071
RcppSimdJson:::.deserialize_json(
1072-
test,
1072+
test,
10731073
type_policy = type_policy$ints_as_dbls,
10741074
simplify_to = simplify_lvl$list
10751075
),
10761076
target
10771077
)
10781078
expect_identical(
10791079
RcppSimdJson:::.deserialize_json(
1080-
test,
1080+
test,
10811081
type_policy = type_policy$strict,
10821082
simplify_to = simplify_lvl$list
10831083
),
10841084
target
10851085
)
1086-
1086+

inst/tinytest/test_fparse_fload.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ expect_error(fload(c(test_file1, test_file2)))
3434
expect_identical(fparse("1"), 1L)
3535
expect_identical(fparse(c("1", "2")), list(1L, 2L))
3636
expect_identical(fparse(c(json1 = "1", json2 = "2")), list(json1 = 1L, json2 = 2L))
37-
expect_identical(fparse(NA_character_), NA_character_)
38-
expect_identical(fparse(c(NA_character_, NA_character_)), list(NA_character_, NA_character_))
39-
expect_identical(fparse(c("1", NA_character_)), list(1L, NA_character_))
37+
expect_identical(fparse(NA_character_), NA)
38+
expect_identical(fparse(c(NA_character_, NA_character_)), list(NA, NA))
39+
expect_identical(fparse(c("1", NA_character_)), list(1L, NA))
4040
expect_true(fparse("null", single_null = TRUE))
4141
expect_true(fparse("junk JSON", error_ok = TRUE, on_error = TRUE))
4242
expect_identical(fparse(
@@ -58,7 +58,7 @@ expect_identical(fload(c(json1 = test_file1, json2 = test_file2)),
5858
list(json1 = 1L, json2 = 2L))
5959
expect_identical(fload(NA_character_), NA_character_)
6060
expect_identical(fload(c(NA_character_, NA_character_)), list(NA_character_, NA_character_))
61-
expect_identical(fload(c(test_file1, NA_character_))[[2L]], NA_character_)
61+
expect_identical(fload(c(test_file1, NA_character_))[[2L]], NA)
6262
.write_file("null", test_file1)
6363
expect_true(fload(test_file1, single_null = TRUE))
6464
.write_file("junk JSON", test_file1)
@@ -509,9 +509,6 @@ expect_error(
509509
"`verbose=` must be either `TRUE` or `FALSE`."
510510
)
511511

512-
expect_error(fload("not-a-real-file.rcppsimdjson.gz"),
513-
"Compressed files are not yet supported.")
514-
515512

516513
# TODO verify CRAN policies for downloading, Travis usage
517514
if (FALSE) {

man/fparse.Rd

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
using namespace Rcpp;
77

8-
// deserialize_json
9-
SEXP deserialize_json(SEXP json, const std::string& json_pointer, SEXP empty_array, SEXP empty_object, SEXP single_null, const bool error_ok, SEXP on_error, const int simplify_to, const int type_policy, const int int64_r_type);
10-
RcppExport SEXP _RcppSimdJson_deserialize_json(SEXP jsonSEXP, SEXP json_pointerSEXP, SEXP empty_arraySEXP, SEXP empty_objectSEXP, SEXP single_nullSEXP, SEXP error_okSEXP, SEXP on_errorSEXP, SEXP simplify_toSEXP, SEXP type_policySEXP, SEXP int64_r_typeSEXP) {
8+
// deserialize
9+
SEXP deserialize(SEXP json, SEXP query, SEXP empty_array, SEXP empty_object, SEXP single_null, const bool error_ok, SEXP on_error, const int simplify_to, const int type_policy, const int int64_r_type);
10+
RcppExport SEXP _RcppSimdJson_deserialize(SEXP jsonSEXP, SEXP querySEXP, SEXP empty_arraySEXP, SEXP empty_objectSEXP, SEXP single_nullSEXP, SEXP error_okSEXP, SEXP on_errorSEXP, SEXP simplify_toSEXP, SEXP type_policySEXP, SEXP int64_r_typeSEXP) {
1111
BEGIN_RCPP
1212
Rcpp::RObject rcpp_result_gen;
1313
Rcpp::RNGScope rcpp_rngScope_gen;
1414
Rcpp::traits::input_parameter< SEXP >::type json(jsonSEXP);
15-
Rcpp::traits::input_parameter< const std::string& >::type json_pointer(json_pointerSEXP);
15+
Rcpp::traits::input_parameter< SEXP >::type query(querySEXP);
1616
Rcpp::traits::input_parameter< SEXP >::type empty_array(empty_arraySEXP);
1717
Rcpp::traits::input_parameter< SEXP >::type empty_object(empty_objectSEXP);
1818
Rcpp::traits::input_parameter< SEXP >::type single_null(single_nullSEXP);
@@ -21,18 +21,18 @@ BEGIN_RCPP
2121
Rcpp::traits::input_parameter< const int >::type simplify_to(simplify_toSEXP);
2222
Rcpp::traits::input_parameter< const int >::type type_policy(type_policySEXP);
2323
Rcpp::traits::input_parameter< const int >::type int64_r_type(int64_r_typeSEXP);
24-
rcpp_result_gen = Rcpp::wrap(deserialize_json(json, json_pointer, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type));
24+
rcpp_result_gen = Rcpp::wrap(deserialize(json, query, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type));
2525
return rcpp_result_gen;
2626
END_RCPP
2727
}
28-
// load_json
29-
SEXP load_json(const Rcpp::CharacterVector& file_path, const std::string& json_pointer, SEXP empty_array, SEXP empty_object, SEXP single_null, const bool error_ok, SEXP on_error, const int simplify_to, const int type_policy, const int int64_r_type);
30-
RcppExport SEXP _RcppSimdJson_load_json(SEXP file_pathSEXP, SEXP json_pointerSEXP, SEXP empty_arraySEXP, SEXP empty_objectSEXP, SEXP single_nullSEXP, SEXP error_okSEXP, SEXP on_errorSEXP, SEXP simplify_toSEXP, SEXP type_policySEXP, SEXP int64_r_typeSEXP) {
28+
// load
29+
SEXP load(const Rcpp::CharacterVector& json, SEXP query, SEXP empty_array, SEXP empty_object, SEXP single_null, const bool error_ok, SEXP on_error, const int simplify_to, const int type_policy, const int int64_r_type);
30+
RcppExport SEXP _RcppSimdJson_load(SEXP jsonSEXP, SEXP querySEXP, SEXP empty_arraySEXP, SEXP empty_objectSEXP, SEXP single_nullSEXP, SEXP error_okSEXP, SEXP on_errorSEXP, SEXP simplify_toSEXP, SEXP type_policySEXP, SEXP int64_r_typeSEXP) {
3131
BEGIN_RCPP
3232
Rcpp::RObject rcpp_result_gen;
3333
Rcpp::RNGScope rcpp_rngScope_gen;
34-
Rcpp::traits::input_parameter< const Rcpp::CharacterVector& >::type file_path(file_pathSEXP);
35-
Rcpp::traits::input_parameter< const std::string& >::type json_pointer(json_pointerSEXP);
34+
Rcpp::traits::input_parameter< const Rcpp::CharacterVector& >::type json(jsonSEXP);
35+
Rcpp::traits::input_parameter< SEXP >::type query(querySEXP);
3636
Rcpp::traits::input_parameter< SEXP >::type empty_array(empty_arraySEXP);
3737
Rcpp::traits::input_parameter< SEXP >::type empty_object(empty_objectSEXP);
3838
Rcpp::traits::input_parameter< SEXP >::type single_null(single_nullSEXP);
@@ -41,7 +41,7 @@ BEGIN_RCPP
4141
Rcpp::traits::input_parameter< const int >::type simplify_to(simplify_toSEXP);
4242
Rcpp::traits::input_parameter< const int >::type type_policy(type_policySEXP);
4343
Rcpp::traits::input_parameter< const int >::type int64_r_type(int64_r_typeSEXP);
44-
rcpp_result_gen = Rcpp::wrap(load_json(file_path, json_pointer, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type));
44+
rcpp_result_gen = Rcpp::wrap(load(json, query, empty_array, empty_object, single_null, error_ok, on_error, simplify_to, type_policy, int64_r_type));
4545
return rcpp_result_gen;
4646
END_RCPP
4747
}
@@ -107,8 +107,8 @@ END_RCPP
107107
}
108108

109109
static const R_CallMethodDef CallEntries[] = {
110-
{"_RcppSimdJson_deserialize_json", (DL_FUNC) &_RcppSimdJson_deserialize_json, 10},
111-
{"_RcppSimdJson_load_json", (DL_FUNC) &_RcppSimdJson_load_json, 10},
110+
{"_RcppSimdJson_deserialize", (DL_FUNC) &_RcppSimdJson_deserialize, 10},
111+
{"_RcppSimdJson_load", (DL_FUNC) &_RcppSimdJson_load, 10},
112112
{"_RcppSimdJson_exceptions_enabled", (DL_FUNC) &_RcppSimdJson_exceptions_enabled, 0},
113113
{"_RcppSimdJson_check_int64", (DL_FUNC) &_RcppSimdJson_check_int64, 0},
114114
{"_RcppSimdJson_validateJSON", (DL_FUNC) &_RcppSimdJson_validateJSON, 1},

0 commit comments

Comments
 (0)