Skip to content

Commit

Permalink
Handle 'NA' in factors written as strings. [fixed #35]
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed Apr 2, 2024
1 parent aeb3276 commit 431cca9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/R-yyjson-serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ yyjson_mut_val *scalar_factor_to_json_val(SEXP factor_, R_xlen_t idx, yyjson_mu

if (opt->factor == FACTOR_AS_INT) {
val = scalar_integer_to_json_val(factor, doc, opt);
} else if (factor == NA_INTEGER) {
val = yyjson_mut_null(doc);
} else {
SEXP nms_ = getAttrib(factor_, R_LevelsSymbol);
const char *nm = CHAR(STRING_ELT(nms_, factor - 1));
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-factors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

test_that("multiplication works", {

# issue #35
color <- c("red", "blue", NA, "red", "white")
color <- factor(color, levels=c("red", "blue"))
color

expect_no_error({
color_str <- yyjsonr::write_json_str(color, factor = 'integer')
})
expect_identical('[1,2,null,1,null]', color_str)

expect_no_error({
color_str <- yyjsonr::write_json_str(color, factor = 'string')
})
expect_identical('["red","blue",null,"red",null]', color_str)

})

0 comments on commit 431cca9

Please sign in to comment.