From 7256cba34fa2aa0607fed6abbfa5e0dcffb8e4d1 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Fri, 18 Sep 2020 10:14:45 -0700 Subject: [PATCH] convert strings to UTF-8 before converting --- .gitignore | 5 +++++ R/as.yaml.R | 3 ++- R/utils.R | 12 ++++++++++++ inst/tests/test_as_yaml.R | 6 ++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 R/utils.R diff --git a/.gitignore b/.gitignore index dfa5c22..c990e55 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,10 @@ build Session.vim yaml_*.tar.gz .Renv-version +*.Rproj *.o *.so +*.dll +.Rproj.user +.Rbuildignore +.Rhistory diff --git a/R/as.yaml.R b/R/as.yaml.R index 5cd2e33..4969d15 100644 --- a/R/as.yaml.R +++ b/R/as.yaml.R @@ -3,10 +3,11 @@ function(x, line.sep = c('\n', '\r\n', '\r'), indent = 2, omap = FALSE, column.major = TRUE, unicode = TRUE, precision = getOption('digits'), indent.mapping.sequence = FALSE, handlers = NULL) { + x <- as.utf8(x) line.sep <- match.arg(line.sep) res <- .Call(C_serialize_to_yaml, x, line.sep, indent, omap, column.major, unicode, precision, indent.mapping.sequence, handlers, - PACKAGE="yaml") + PACKAGE = "yaml") Encoding(res) <- "UTF-8" res } diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..0a2c3cc --- /dev/null +++ b/R/utils.R @@ -0,0 +1,12 @@ +`as.utf8` <- +function(x) { + + if (is.character(x)) { + enc2utf8(x) + } else if (is.list(x)) { + lapply(x, as.utf8) + } else { + x + } + +} \ No newline at end of file diff --git a/inst/tests/test_as_yaml.R b/inst/tests/test_as_yaml.R index e66eca1..b64118f 100644 --- a/inst/tests/test_as_yaml.R +++ b/inst/tests/test_as_yaml.R @@ -440,3 +440,9 @@ test_custom_tag_for_unnamed_list <- function() { result <- as.yaml(x) checkEquals(expected, result) } + +test_latin1_strings <- function() { + data <- list(description = enc2native("á")) + result <- as.yaml(data) + checkEquals(result, enc2utf8("description: á\n")) +} \ No newline at end of file