Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yyjsonr switch #33

Merged
merged 12 commits into from
Jun 21, 2024
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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.2)
nicholas-masel marked this conversation as resolved.
Show resolved Hide resolved
Imports:
jsonlite (>= 1.8.0),
yyjsonr (>= 0.1.18),
jsonvalidate (>= 1.3.1)
Suggests:
nicholas-masel marked this conversation as resolved.
Show resolved Hide resolved
testthat (>= 2.1.0),
jsonlite (>= 1.8.0),
nicholas-masel marked this conversation as resolved.
Show resolved Hide resolved
knitr,
haven,
rmarkdown,
Expand Down
8 changes: 6 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion R/dataset_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 17 additions & 3 deletions R/read_dataset_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,33 @@
#' 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
mstackhouse marked this conversation as resolved.
Show resolved Hide resolved
ds_json <- jsonlite::fromJSON(file_contents)


# Pull the object out with a lot of assumptions because the format has already
# been validated
Expand Down
19 changes: 12 additions & 7 deletions R/write_dataset_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions man/read_dataset_json.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-datasetjson.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
Loading