New features in jsonlite 0.9.14

[This article was first published on OpenCPU, 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.

opencpu logo

The jsonlite package implements a robust, high performance JSON parser and generator for R, optimized for statistical data and the web. This week version 0.9.14 appeared on CRAN which adds some handy new features.

Significant Digits

By default, the digits argument in toJSON specifies the number of decimal digits to print:

toJSON(pi, digits=3)
# [3.142]

A feature requested by Winston Chang was to control precision of number formatting. You can now specify the number of significant digits, analogous to the signif function in base R. Either set signif = TRUE or specify the digits argument using I():

> toJSON(pi, digits = 3, use_signif = TRUE)
# [3.14]

toJSON(pi, digits = I(3))
# [3.14]

Prettify Indent

A feature requested by Yihui Xie was to control the number of spaces to indent prettified json. The default is still 4 spaces:

toJSON(pi, pretty = TRUE)
# [
#     3.1416
# ]

The number of indent spaces can be changed by setting the pretty argument to an integer. For example to indent by only 2 spaces:

toJSON(pi, pretty = 2)
# [
#   3.1416
# ]

Support for 64bit integers in toJSON

Another new feature is support for 64bit integers from the bit64 package. R does not support 64 bit integers by default, and doubles have limited precision:

x <- 2^60 + 1:3
toJSON(x)
# [1.15292150460685e+18,1.15292150460685e+18,1.15292150460685e+18]

But when the number is stored as 64 bit integer, jsonlite will print the full integer in the JSON output:

library(bit64)
x <- as.integer64(2)^60 + 1:3
toJSON(x)
# [1152921504606846977,1152921504606846978,1152921504606846979]

Currently this is only supported in toJSON. The parser in fromJSON still uses doubles for very large integers.

To leave a comment for the author, please follow the link and comment on their blog: OpenCPU.

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.

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)