Skip to content

Commit

Permalink
Added read_ndjson_str()
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed Mar 16, 2024
1 parent 2aece8c commit f6e29ba
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(read_json_file)
export(read_json_raw)
export(read_json_str)
export(read_ndjson_file)
export(read_ndjson_str)
export(validate_json_file)
export(validate_json_str)
export(write_json_file)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# yyjsonr 0.1.18.9004 2024-03-15

* Re-introduce NDJSON support
* Add `read_ndjson_str()`

# yyjsonr 0.1.18.9003 2024-03-13

Expand Down
53 changes: 53 additions & 0 deletions R/ndjson.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,59 @@ read_ndjson_file <- function(filename, type = c('df', 'list'), nread = -1, nskip
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Parse an NDJSON file to a data.frame or list
#'
#' If reading as data.frame, each row of NDJSON becomes a row in the data.frame.
#' If reading as a list, then each row becomes an element in the list.
#'
#' If parsing NDJSON to a data.frame it is usually better if the json objects
#' are consistent from line-to-line. Type inference for the data.frame is done
#' during initialisation by reading through \code{nprobe} lines. Warning: if
#' there is a type-mismatch further into the file than it is probed, then you
#' will get missing values in the data.frame, or JSON values not captured in
#' the R data.
#'
#' No flattening of the namespace is done i.e. nested object remain nested.
#'
#' @inheritParams read_ndjson_file
#' @param x string containing NDJSON
#'
#' @examples
#' tmp <- tempfile()
#' json <- write_ndjson_str(head(mtcars))
#' read_ndjson_str(json, type = 'list')
#'
#' @family JSON Parsers
#' @return NDJSON data read into R as list or data.frame depending
#' on \code{'type'} argument
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
read_ndjson_str <- function(x, type = c('df', 'list'), nread = -1, nskip = 0, nprobe = 100, opts = list(), ...) {

type <- match.arg(type)

if (type == 'list') {
.Call(
parse_ndjson_str_as_list_,
x,
nread,
nskip,
modify_list(opts, list(...))
)
} else {
.Call(
parse_ndjson_str_as_df_,
x,
nread,
nskip,
nprobe,
modify_list(opts, list(...))
)
}
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Write list or data.frame object to NDJSON in a file
#'
Expand Down
3 changes: 2 additions & 1 deletion man/read_json_conn.Rd

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

3 changes: 2 additions & 1 deletion man/read_json_file.Rd

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

3 changes: 2 additions & 1 deletion man/read_json_raw.Rd

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

3 changes: 2 additions & 1 deletion man/read_json_str.Rd

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

3 changes: 2 additions & 1 deletion man/read_ndjson_file.Rd

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

68 changes: 68 additions & 0 deletions man/read_ndjson_str.Rd

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

2 changes: 1 addition & 1 deletion src/Makevars
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
PKG_LIBS=-lz
#PKG_CFLAGS += -Wconversion
#PKG_CFLAGS += -Wconversion
6 changes: 6 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ extern SEXP validate_json_str_ (SEXP str_ , SEXP verbose_, SEXP parse_opts_)
extern SEXP parse_ndjson_file_as_df_ (SEXP filename_, SEXP nread_, SEXP nskip_, SEXP nprobe_, SEXP parse_opts_);
extern SEXP parse_ndjson_file_as_list_(SEXP filename_, SEXP nread_, SEXP nskip_, SEXP parse_opts_);

extern SEXP parse_ndjson_str_as_df_ (SEXP str_, SEXP nread_, SEXP nskip_, SEXP nprobe_, SEXP parse_opts_);
extern SEXP parse_ndjson_str_as_list_(SEXP str_, SEXP nread_, SEXP nskip_, SEXP parse_opts_);

extern SEXP serialize_df_to_ndjson_str_ (SEXP robj_, SEXP serialize_opts_);
extern SEXP serialize_df_to_ndjson_file_(SEXP robj_, SEXP filename_, SEXP serialize_opts_);

Expand Down Expand Up @@ -58,6 +61,9 @@ static const R_CallMethodDef CEntries[] = {
{"parse_ndjson_file_as_df_" , (DL_FUNC) &parse_ndjson_file_as_df_ , 5},
{"parse_ndjson_file_as_list_", (DL_FUNC) &parse_ndjson_file_as_list_, 4},

{"parse_ndjson_str_as_df_" , (DL_FUNC) &parse_ndjson_str_as_df_ , 5},
{"parse_ndjson_str_as_list_", (DL_FUNC) &parse_ndjson_str_as_list_, 4},

{"serialize_df_to_ndjson_str_" , (DL_FUNC) &serialize_df_to_ndjson_str_ , 2},
{"serialize_df_to_ndjson_file_", (DL_FUNC) &serialize_df_to_ndjson_file_, 3},

Expand Down
Loading

0 comments on commit f6e29ba

Please sign in to comment.