Skip to content

Commit

Permalink
checks IsInt first #81
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Mar 18, 2024
1 parent 32ee890 commit a1e9b09
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions inst/include/jsonify/from_json/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ namespace from_json {
}
// numeric
case rapidjson::kNumberType: {
if( json.IsDouble() ) {
return Rcpp::wrap< double >( json.GetDouble() );
} else {
// if( json.IsDouble() || json.IsUint64() || json.IsInt64() ) {
// return Rcpp::wrap< double >( json.GetDouble() );
// } else {
// return Rcpp::wrap< int >( json.GetInt() );
// }
if( json.IsInt() ) {
return Rcpp::wrap< int >( json.GetInt() );
} else {
return Rcpp::wrap< double >( json.GetDouble() );
}
}
case rapidjson::kObjectType: {
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-from_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ test_that("scalar values handled properly", {
expect_equal(from_json("true"), TRUE)
})

test_that("Int64 treated as double", {
json_str <- '{"value" : 5500000000}'
expect_equal(from_json(json_obj)$value, 5.5e+09)
})

test_that("vector / array values handled properly", {

target <- list(a = list(1L, 2L, 3L, NA), b = list(1L, "cats", 3L, NA))
Expand Down

0 comments on commit a1e9b09

Please sign in to comment.