Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lost numeric precision with promote_num_to_string on read #60

Open
mstackhouse opened this issue Jan 28, 2025 · 1 comment
Open

Lost numeric precision with promote_num_to_string on read #60

mstackhouse opened this issue Jan 28, 2025 · 1 comment

Comments

@mstackhouse
Copy link

It looks like some numeric precision gets lost when using opts_read_json(promote_num_to_string=TRUE). This only appears to happen when a multi-type row is read in, triggering the numbers to convert to character so the object imports as a character matrix.

Here's a reprex:

x <- unname(as.data.frame(list(
  col1=c(143.66666666666699825, 2/3, 1/3, 165/37, 6/7), 
  col2 = c('a', 'b', 'c', 'd', 'e')
)))

y <- yyjsonr::write_json_str(x)

print(y)
#> [1] "[[143.666666666667,\"a\"],[0.6666666666666666,\"b\"],[0.3333333333333333,\"c\"],[4.45945945945946,\"d\"],[0.8571428571428571,\"e\"]]"

with_opt <- yyjsonr::read_json_str(y, opts=yyjsonr::opts_read_json(
  promote_num_to_string = TRUE
))

print(with_opt)
#>      [,1]         [,2]
#> [1,] "143.666667" "a" 
#> [2,] "0.666667"   "b" 
#> [3,] "0.333333"   "c" 
#> [4,] "4.459459"   "d" 
#> [5,] "0.857143"   "e"

without_opt <- yyjsonr::read_json_str(y, opts=yyjsonr::opts_read_json(
  promote_num_to_string = FALSE
))

for (i in seq(1, 5)) {
  print(as.character(without_opt[[i]][[1]]))
  print(with_opt[i,1])
}
#> [1] "143.666666666667"
#> [1] "143.666667"
#> [1] "0.666666666666667"
#> [1] "0.666667"
#> [1] "0.333333333333333"
#> [1] "0.333333"
#> [1] "4.45945945945946"
#> [1] "4.459459"
#> [1] "0.857142857142857"
#> [1] "0.857143"

Created on 2025-01-28 with reprex v2.1.1

@mstackhouse
Copy link
Author

For some added context, we're using yyjsonr in our package here for that's reading in data from a specific schema. Our package is here.

The standard calls for the data frame writing out as rows, which can have mixed types. On read, we promote numbers to strings because the data reads in as a character matrix which can easily be converted to a data frame, which we then handle all type conversions using the appropriate metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant