From 33b687230a63a46cc0154d57a0513c83b9d8bedc Mon Sep 17 00:00:00 2001 From: Admin_mschuemi Date: Wed, 29 Mar 2023 03:47:15 -0400 Subject: [PATCH] Using ParallelLogger's JSON functions to store attributes. Fixes #54 --- DESCRIPTION | 3 ++- R/LoadingSaving.R | 6 ++++-- tests/testthat/test-loadingSaving.R | 5 ++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8c2b12e..2133e33 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,8 @@ Imports: pillar, glue, tidyselect, - jsonlite + jsonlite, + ParallelLogger Suggests: testthat, dbplyr, diff --git a/R/LoadingSaving.R b/R/LoadingSaving.R index 2d85b06..70b962a 100644 --- a/R/LoadingSaving.R +++ b/R/LoadingSaving.R @@ -63,7 +63,8 @@ saveAndromeda <- function(andromeda, fileName, overwrite = TRUE) { attribs <- attributes(andromeda) attribs[["class"]] <- attribs[["path"]] <- attribs[["names"]] <- attribs[["env"]] <- attribs[[".xData"]] <- NULL attributesFileName <- file.path(tempdir(), "user-defined-attributes.json") - jsonlite::write_json(attribs, attributesFileName) + ParallelLogger::saveSettingsToJson(attribs, attributesFileName) + # jsonlite::write_json(attribs, attributesFileName, pretty = TRUE, force = TRUE, null = "null", auto_unbox = TRUE) tableDirs <- list.dirs(attr(andromeda, "path"), recursive = FALSE) zip::zipr(fileName, c(attributesFileName, tableDirs), compression_level = 2) @@ -115,7 +116,8 @@ loadAndromeda <- function(fileName) { } class(andr) <- andrClass - attributes <- jsonlite::read_json(file.path(path, "user-defined-attributes.json"), simplifyVector = TRUE) + attributes <- ParallelLogger::loadSettingsFromJson(file.path(path, "user-defined-attributes.json")) + # attributes <- jsonlite::read_json(file.path(path, "user-defined-attributes.json"), simplifyVector = TRUE) on.exit(unlink(file.path(path, "user-defined-attributes.json"))) for (nm in names(attributes)) { attr(andr, nm) <- attributes[[nm]] diff --git a/tests/testthat/test-loadingSaving.R b/tests/testthat/test-loadingSaving.R index 2e2093d..33d01d9 100644 --- a/tests/testthat/test-loadingSaving.R +++ b/tests/testthat/test-loadingSaving.R @@ -8,7 +8,9 @@ test_that("Saving and loading", { expect_true("table" %in% names(andromeda)) iris1 <- andromeda$table %>% collect() - attr(andromeda, "metaData") <- list(x = 1) + s3Object <- list(a = 1) + class(s3Object) <- "MyClass" + attr(andromeda, "metaData") <- list(x = 1, y = list(s3Object)) fileName <- tempfile(fileext = ".zip") fileName <- file.path(.getAndromedaTempFolder(), "asdf.zip") @@ -27,6 +29,7 @@ test_that("Saving and loading", { expect_equivalent(iris1, iris2) expect_false(is.null(attr(andromeda2, "metaData"))) expect_equal(attr(andromeda2, "metaData")$x, 1) + expect_equal(class(attr(andromeda2, "metaData")$y[[1]]), "MyClass") expect_error(capture.output(saveAndromeda(andromeda2, fileName, overwrite = TRUE)), NA)