-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from ambiorix-web/107-fix-serialiser
fix serialiser
- Loading branch information
Showing
6 changed files
with
142 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,83 @@ | ||
#' Serialise | ||
#' | ||
#' | ||
#' Serialise data to JSON. | ||
#' | ||
#' | ||
#' @param data Data to serialise. | ||
#' @param ... Options to pass to [yyjsonr::write_json_str]. | ||
#' | ||
#' @noRd | ||
#' @param ... Named options to pass to [yyjsonr::write_json_str]. | ||
#' | ||
#' @noRd | ||
#' @keywords internal | ||
default_serialiser <- function(data, ...){ | ||
yyjsonr::write_json_str(data, ...) | ||
default_serialiser <- function(data, ...) { | ||
dots <- list(...) | ||
|
||
# `yyjsonr::write_json_str()` accepts both `opts` & `...` but | ||
# `...` should override `opts`. | ||
# ensure that happens and use `opts` only: | ||
opts <- dots$opts | ||
if (is.null(opts)) { | ||
opts <- list() | ||
} | ||
|
||
dots$opts <- NULL | ||
opts[names(dots)] <- dots | ||
|
||
if (is.null(opts$auto_unbox)) { | ||
opts$auto_unbox <- TRUE | ||
} | ||
|
||
yyjsonr::write_json_str(data, opts = opts) | ||
} | ||
|
||
#' Retrieve Serialiser | ||
#' | ||
#' | ||
#' Retrieve the serialiser to use, either the default or that defined by user. | ||
#' | ||
#' @noRd | ||
#' | ||
#' @noRd | ||
#' @keywords internal | ||
get_serialise <- function(){ | ||
get_serialise <- function() { | ||
getOption("AMBIORIX_SERIALISER", default_serialiser) | ||
} | ||
|
||
#' Serialise to JSON | ||
#' | ||
#' Serialise an object to JSON. | ||
#' Default serialiser can be change by setting the | ||
#' `AMBIORIX_SERIALISER` option to the desired function. | ||
#' | ||
#' Serialise an Object to JSON | ||
#' | ||
#' @details | ||
#' Ambiorix uses [yyjsonr::write_json_str()] by default for serialization. | ||
#' | ||
#' ### Custom Serialiser | ||
#' | ||
#' To override the default, set the `AMBIORIX_SERIALISER` option to a function that accepts: | ||
#' - `data`: Object to serialise. | ||
#' - `...`: Additional arguments passed to the function. | ||
#' | ||
#' For example: | ||
#' | ||
#' ```r | ||
#' my_serialiser <- function(data, ...) { | ||
#' jsonlite::toJSON(x = data, ...) | ||
#' } | ||
#' | ||
#' options(AMBIORIX_SERIALISER = my_serialiser) | ||
#' ``` | ||
#' | ||
#' @param data Data to serialise. | ||
#' @param ... Passed to serialiser. | ||
#' | ||
#' @examples | ||
#' \dontrun{serialise(cars)} | ||
#' | ||
#' | ||
#' @examples | ||
#' if (interactive()) { | ||
#' # a list: | ||
#' response <- list(code = 200L, msg = "hello, world!") | ||
#' | ||
#' serialise(response) | ||
#' #> {"code":200,"msg":"hello, world"} | ||
#' | ||
#' serialise(response, auto_unbox = FALSE) | ||
#' #> {"code":[200],"msg":["hello, world"]} | ||
#' | ||
#' # data.frame: | ||
#' serialise(cars) | ||
#' } | ||
#' | ||
#' @export | ||
serialise <- function(data, ...){ | ||
serialise <- function(data, ...) { | ||
get_serialise()(data, ...) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,42 @@ | ||
test_that("serialise", { | ||
json <- default_serialiser(list(x = 1)) | ||
obj <- list(a = 1, b = "hello, world!") | ||
|
||
expect_equal( | ||
json, | ||
yyjsonr::write_json_str( | ||
list(x = 1) | ||
default_serialiser(obj), | ||
yyjsonr::write_json_str(obj, auto_unbox = TRUE) | ||
) | ||
|
||
expect_equal( | ||
default_serialiser(obj, auto_unbox = FALSE), | ||
yyjsonr::write_json_str(obj) | ||
) | ||
|
||
expect_equal( | ||
default_serialiser(obj, opts = list(auto_unbox = FALSE)), | ||
yyjsonr::write_json_str(obj) | ||
) | ||
}) | ||
|
||
test_that("custom serialiser works", { | ||
global_serialiser <- getOption("AMBIORIX_SERIALISER") | ||
on.exit(options(AMBIORIX_SERIALISER = global_serialiser)) | ||
|
||
my_serialiser <- function(data, ...) { | ||
list( | ||
data = data, | ||
serialised = jsonlite::toJSON(x = data, ...) | ||
) | ||
} | ||
|
||
options(AMBIORIX_SERIALISER = my_serialiser) | ||
|
||
obj <- list(a = 1, b = "hello, world!") | ||
|
||
expect_equal( | ||
my_serialiser(obj), | ||
list( | ||
data = obj, | ||
serialised = jsonlite::toJSON(obj) | ||
) | ||
) | ||
}) |