rOpenSci | Update jsonlite 1.2

Update jsonlite 1.2

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:

  • Add read_json and write_json convenience wrappers, #161
  • Update modp_numtoa from upstream, fixes a rounding issue in #148.
  • Ensure asJSON.POSIXt does not use sci notation for negative values, #155
  • Tweak num_to_char to properly print large negative numbers
  • Performance optimization for simplyfing data frames (see below)

Use the Github compare page to see the full diff on metacran.

🔗 New read/write API

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.

🔗 Performance enhancements

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/jsonlite@1.1")
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/jsonlite@1.2")
microbenchmark::microbenchmark(jsonlite::fromJSON(json), times = 50)

On my Macbook this has reduced the median time from approx 0.91s to 0.76s.