Update jsonlite 1.2

January 4, 2017
By

[This article was first published on rOpenSci Blog - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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/[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.

To leave a comment for the author, please follow the link and comment on their blog: rOpenSci Blog - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)