Wednesday, January 4, 2017 From rOpenSci (https://ropensci.org/blog/2017/01/04/jsonlite-12/). Except where otherwise noted, content on this site is licensed under the CC-BY license.
A new version of jsonlite package to CRAN. This is a maintenance release with enhancements and bug fixes. A summary of changes in v1.2 from the NEWS file:
read_json
and write_json
convenience wrappers, #161modp_numtoa
from upstream, fixes a rounding issue in #148.asJSON.POSIXt
does not use sci notation for negative values, #155num_to_char
to properly print large negative numbersUse the Github compare page to see the full diff on metacran.
The package has gained new high level functions read_json
and write_json
. These are wrappers for fromJSON
and toJSON
which read/write json directly from/to disk. This API is consistent with tidyverse packages like readr, readxl and haven (see #161).
The only thing to note is that read_json
does not simplify by default, as is done by fromJSON
. For example:
# Write Data frame to a temp file
tmp <- tempfile()
write_json(iris, tmp)
# Nested lists
read_json(tmp)
# A data frame
read_json(tmp, simplifyVector = TRUE)
Notice how read_json
only returns a data frame when simplifyVector
is explicitly set to TRUE
.
We have ported a bit of C code to optimize simplification for data frame structures. This script compares performance for both versions:
# example json
json <- jsonlite::toJSON(ggplot2::diamonds)
# Test with jsonlite 1.1
devtools::install_github("cran/[email protected]")
microbenchmark::microbenchmark(jsonlite::fromJSON(json), times = 50)
# Unload jsonlite 1.1 (might need restart R on windows)
unloadNamespace("jsonlite")
library.dynam.unload('jsonlite', find.package('jsonlite'))
# Test with jsonlite 1.2
devtools::install_github("cran/[email protected]")
microbenchmark::microbenchmark(jsonlite::fromJSON(json), times = 50)
On my Macbook this has reduced the median time from approx 0.91s to 0.76s.