Skip to content

Commit

Permalink
Merge pull request #33 from atorus-research/yyjsonr_switch
Browse files Browse the repository at this point in the history
Yyjsonr switch
  • Loading branch information
nicholas-masel committed Jun 21, 2024
2 parents 3e9b5fe + 922de64 commit e0a1f71
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 20 deletions.
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.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,
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
21 changes: 17 additions & 4 deletions R/read_dataset_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

0 comments on commit e0a1f71

Please sign in to comment.