Skip to content

Commit

Permalink
Merge pull request #229 from frictionlessdata/write_na_null
Browse files Browse the repository at this point in the history
Encode null as NULL and NA as string
  • Loading branch information
PietrH authored Jul 10, 2024
2 parents fde6751 + 049b767 commit 9e0d6d1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [checklist](https://github.com/inbo/checklist) tooling was removed, in favour of `CITATION.cff` for citation and Zenodo deposit.
* `write_package()` no longer writes to "." by default, since this is not allowed by CRAN policies. The user needs to explicitly define a directory (#205).
* `write_package()` encodes NULL and NA values as NULL (#203).
* `add_resource()` now allows to replace an existing resource (#227).

# frictionless 1.1.0
Expand Down
8 changes: 7 additions & 1 deletion R/write_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ write_package <- function(package, directory, compress = FALSE) {

# Write datapackage.json
package$directory <- NULL
package_json <- jsonlite::toJSON(package, pretty = TRUE, auto_unbox = TRUE)
package_json <- jsonlite::toJSON(
package,
pretty = TRUE,
null = "null",
na = "null",
auto_unbox = TRUE
)
write(package_json, file.path(directory, "datapackage.json"))

# Return (updated) package invisibly
Expand Down
Binary file modified data/example_package.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/example_package.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-read_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,10 @@ test_that("read_package() allows YAML descriptor", {
check_package(read_package(test_path("data/valid_minimal.yml")))
)
})

test_that("read_package() correctly converts a JSON `null` to an R NULL", {
p_path <- system.file("extdata", "datapackage.json", package = "frictionless")
p <- read_package(p_path)
# purrr::chuck() so an error is returned if `image` does not exist
expect_null(purrr::chuck(p, "image"))
})
32 changes: 32 additions & 0 deletions tests/testthat/test-write_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,35 @@ test_that("write_package() will gzip file for compress = TRUE", {
p_reread <- read_package(file.path(dir, "datapackage.json"))
expect_identical(read_resource(p_reread, "new"), dplyr::as_tibble(df))
})

test_that("write_package() encodes null and NA as null", {
p <-
example_package %>%
add_resource(
resource_name = "new",
data = data.frame("col_1" = c(1, 2), "col_2" = c("a", "b")),
title = NA
)
# Set a custom property as NA, to see if it gets written as `null` in JSON
p$my_property <- NA

# Image is set as `NULL` in the JSON and that should still be the case here.
expect_null(purrr::chuck(p, "image"))

## Use purrr::chuck() so an error is returned if image doesn't exist
expect_null(purrr::chuck(p, "image"))
expect_identical(p$resources[[4]]$title, NA)
expect_identical(p$my_property, NA)

dir <- file.path(tempdir(), "package")
on.exit(unlink(dir, recursive = TRUE))
write_package(p, dir)

p_loaded <- read_package(file.path(dir, "datapackage.json"))
# Use purrr::chuck() so an error is returned if the requested index doesn't
# exist
expect_null(purrr::chuck(p_loaded, "image"))
expect_null(purrr::chuck(p_loaded, "resources", 4, "title"))
## my_property was set to NA, when writing this becomes NULL
expect_null(purrr::chuck(p_loaded, "my_property"))
})

0 comments on commit 9e0d6d1

Please sign in to comment.