Skip to content

Commit

Permalink
convert strings to UTF-8 before converting
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey authored and spgarbet committed Sep 13, 2021
1 parent 4788abe commit 7256cba
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ build
Session.vim
yaml_*.tar.gz
.Renv-version
*.Rproj
*.o
*.so
*.dll
.Rproj.user
.Rbuildignore
.Rhistory
3 changes: 2 additions & 1 deletion R/as.yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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
}

}
6 changes: 6 additions & 0 deletions inst/tests/test_as_yaml.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

0 comments on commit 7256cba

Please sign in to comment.