jsonlite::toJSON sometimes outputs -0 instead of 0. dput breaks reprex

I am experiencing a bizarre issue. A few senior R devs have worked on this for hours with no discernible solution and need an R expert to help!

A dataframe that was created in R output from models contains 0 values that yield a -0 when translated to json via jsonlite::toJSON.

I have distilled this to its most basic form:

> df
# A tibble: 1 × 1
1     0
> df |> jsonlite::toJSON()

Is it actually 0?

> df[[1]]
[1] 0
> df[[1]] == 0.0
[1] TRUE
> identical(df[[1]], 0.0000000)
[1] TRUE

Let's dput it!

> df |> dput()
structure(list(value = 0), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"))

Does the dput also yield a -0 ? No!

structure(list(value = 0), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")) |> jsonlite::toJSON()

So I can't reprex with dput. It does work to reprex with an RDS:

> df |> saveRDS('value.rds')
> readRDS('value.rds') |> jsonlite::toJSON()

How did I end up with this corrupt type of 0 that causes this issue in jsonlite? I can't attach the RDS to this forum, it doesn't accept RDS. Is there anything I can do to further debug?

> df$value[[1]] |> jsonlite::toJSON()
> df$value[[1]] |> class()
[1] "numeric"


Of note: fromJSON seems to cleanse -0 values:

> jsonlite::fromJSON('[{"value":-0}]') |> jsonlite::toJSON()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.