Skip to content

Commit

Permalink
adding option to overwrite single null values (like RcppSimdJson::fpa…
Browse files Browse the repository at this point in the history
…rse single_null)
  • Loading branch information
ecqiu committed Mar 2, 2024
1 parent fe83868 commit b6b9998
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
3 changes: 3 additions & 0 deletions R/json-opts.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ yyjson_write_flag <- list(
#' be stored in this type.
#' @param df_missing_list_elem R value to use when elements are missing in list
#' columns in data.frames. Default: NULL
#' @param any_single_null_elem R value to use for any single NULL element. Default: NULL
#' @param obj_of_arrs_to_df logical. Should a named list of equal-length
#' vectors be promoted to a data.frame? Default: TRUE. If FALSE, then
#' result will be left as a list.
Expand Down Expand Up @@ -204,6 +205,7 @@ yyjson_write_flag <- list(
opts_read_json <- function(
promote_num_to_string = FALSE,
df_missing_list_elem = NULL,
any_single_null_elem = NULL,
obj_of_arrs_to_df = TRUE,
arr_of_objs_to_df = TRUE,
str_specials = c('string', 'special'),
Expand All @@ -217,6 +219,7 @@ opts_read_json <- function(
list(
promote_num_to_string = isTRUE(promote_num_to_string),
df_missing_list_elem = df_missing_list_elem,
any_single_null_elem = any_single_null_elem,
obj_of_arrs_to_df = isTRUE(obj_of_arrs_to_df),
arr_of_objs_to_df = isTRUE(arr_of_objs_to_df),
length1_array_asis = isTRUE(length1_array_asis),
Expand Down
3 changes: 3 additions & 0 deletions man/opts_read_json.Rd

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

6 changes: 5 additions & 1 deletion src/R-yyjson-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ parse_options create_parse_options(SEXP parse_opts_) {
parse_options opt = {
.int64 = INT64_AS_STR,
.df_missing_list_elem = R_NilValue,
.any_single_null_elem = R_NilValue,
.obj_of_arrs_to_df = true,
.arr_of_objs_to_df = true,
.length1_array_asis = false,
Expand Down Expand Up @@ -74,6 +75,8 @@ parse_options create_parse_options(SEXP parse_opts_) {
}
} else if (strcmp(opt_name, "df_missing_list_elem") == 0) {
opt.df_missing_list_elem = val_;
} else if (strcmp(opt_name, "any_single_null_elem") == 0) {
opt.any_single_null_elem = val_;
} else if (strcmp(opt_name, "yyjson_read_flag") == 0) {
for (unsigned int idx = 0; idx < length(val_); idx++) {
opt.yyjson_read_flag |= (unsigned int)INTEGER(val_)[idx];
Expand Down Expand Up @@ -1794,7 +1797,8 @@ SEXP json_as_robj(yyjson_val *val, parse_options *opt) {
res_ = PROTECT(mkString(yyjson_get_str(val))); nprotect++;
break;
case YYJSON_TYPE_NULL:
res_ = R_NilValue;
// res_ = R_NilValue;
res_ = opt->any_single_null_elem;
break;
default:
warning("json_as_robj(): unhandled: %s\n", yyjson_get_type_desc(val));
Expand Down
1 change: 1 addition & 0 deletions src/R-yyjson-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
typedef struct {
unsigned int int64;
SEXP df_missing_list_elem;
SEXP any_single_null_elem;
bool obj_of_arrs_to_df;
bool arr_of_objs_to_df;
bool length1_array_asis;
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ test_that("array of nested json objects to data.frame", {
)
df$a <- list(10.1, "greg", NA)
expect_equal(test, df)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Ordinary array with "NA" for missing values
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test <- read_json_str('{"x":null,"y":null,"z":3}',opts=yyjsonr::opts_read_json(any_single_null_elem=NA,arr_of_objs_to_df = T))
test
df <- list(
x = NA,
y = NA,
z=3
)
expect_equal(test, df)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Data.frame with explicit NULL
Expand Down

0 comments on commit b6b9998

Please sign in to comment.