diff --git a/DESCRIPTION b/DESCRIPTION index f30c31d..2019222 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,13 +21,14 @@ Language: en-US License: Apache License (>= 2) LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 -Depends: R (>= 3.5) +RoxygenNote: 7.3.1 +Depends: R (>= 4.0) Imports: - jsonlite (>= 1.8.0), + yyjsonr (>= 0.1.18), jsonvalidate (>= 1.3.1) Suggests: testthat (>= 2.1.0), + jsonlite (>= 1.8.0), knitr, haven, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index c882af0..a9a5cd9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,8 +23,12 @@ export(set_source_system) export(set_study_oid) export(validate_dataset_json) export(write_dataset_json) -importFrom(jsonlite,fromJSON) -importFrom(jsonlite,toJSON) importFrom(jsonvalidate,json_validate) importFrom(tools,file_path_sans_ext) importFrom(utils,tail) +importFrom(yyjsonr,opts_read_json) +importFrom(yyjsonr,opts_write_json) +importFrom(yyjsonr,read_json_file) +importFrom(yyjsonr,read_json_str) +importFrom(yyjsonr,write_json_file) +importFrom(yyjsonr,write_json_str) diff --git a/R/dataset_metadata.R b/R/dataset_metadata.R index 22edcb1..e65560e 100644 --- a/R/dataset_metadata.R +++ b/R/dataset_metadata.R @@ -95,7 +95,7 @@ set_item_data <- function(x, .data, ...) { # Insert into object in proper spots x[[1]][['records']] <- records - x[[1]][['itemData']] <- item_data + x[[1]][['itemData']] <- unname(item_data) x } diff --git a/R/read_dataset_json.R b/R/read_dataset_json.R index 161b382..a389558 100644 --- a/R/read_dataset_json.R +++ b/R/read_dataset_json.R @@ -23,19 +23,32 @@ #' dat <- read_dataset_json(js) read_dataset_json <- function(file) { + json_opts <- yyjsonr::opts_read_json( + promote_num_to_string = TRUE + ) + if (path_is_url(file)) { # Url? file_contents <- read_from_url(file) + ds_json <- yyjsonr::read_json_str( + file_contents, + opts = json_opts + ) } else if (file.exists(file)) { # File on disk? - file_contents <- readLines(file) + ds_json <- yyjsonr::read_json_file( + file, + opts = json_opts + ) } else { # Direct file contents? - file_contents <- file + ds_json <- yyjsonr::read_json_str( + file, + opts = json_opts + ) } - # Read the file and convert to datasetjson object - ds_json <- jsonlite::fromJSON(file_contents) + # Pull the object out with a lot of assumptions because the format has already # been validated diff --git a/R/write_dataset_json.R b/R/write_dataset_json.R index af7c165..099bba0 100644 --- a/R/write_dataset_json.R +++ b/R/write_dataset_json.R @@ -32,18 +32,23 @@ write_dataset_json <- function(x, file, pretty=FALSE) { } # Create the JSON text - js <- jsonlite::toJSON( - x, - dataframe = "values", - na = "null", + json_opts <- yyjsonr::opts_write_json( + pretty = pretty, auto_unbox = TRUE, - pretty = pretty) + ) if (!missing(file)) { # Write file to disk - cat(js, "\n", file = file) + yyjsonr::write_json_file( + x, + filename = file, + opts = json_opts + ) } else { # Print to console - js + yyjsonr::write_json_str( + x, + opts = json_opts + ) } } diff --git a/R/zzz.R b/R/zzz.R index bba701f..9e26d2b 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,5 +1,5 @@ #' @importFrom jsonvalidate json_validate -#' @importFrom jsonlite fromJSON toJSON +#' @importFrom yyjsonr opts_write_json opts_read_json write_json_file write_json_str read_json_str read_json_file #' @importFrom tools file_path_sans_ext #' @importFrom utils tail NULL diff --git a/man/read_dataset_json.Rd b/man/read_dataset_json.Rd index b5ce954..811cc6f 100644 --- a/man/read_dataset_json.Rd +++ b/man/read_dataset_json.Rd @@ -21,6 +21,7 @@ either a file path on disk of a URL which contains the Dataset JSON file. # Read from disk \dontrun{ dat <- read_dataset_json("path/to/file.json") + # Read file from URL dat <- dataset_json('https://www.somesite.com/file.json') } diff --git a/tests/testthat/test-datasetjson.R b/tests/testthat/test-datasetjson.R index 131ff46..52576a0 100644 --- a/tests/testthat/test-datasetjson.R +++ b/tests/testthat/test-datasetjson.R @@ -49,10 +49,10 @@ test_that("datasetjson object builds with minimal defaults", { expect_equal(ds_json$clinicalData$itemGroupData$IG.IRIS$items, iris_items_list) # Verify that data are attached properly with ITEMGRPUPSEQ attached - iris_test <- cbind( + iris_test <- unname(cbind( ITEMGROUPDATASEQ = 1:nrow(iris), iris - ) + )) expect_equal(ds_json$clinicalData$itemGroupData$IG.IRIS$itemData, iris_test) })