Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Today a new version of the jsonlite package was released to CRAN. This update includes a few internal enhancements and one new feature.

## Doubles vs integers

The new always_decimal parameter forces formatting of doubles in decimal notation. That is to include at least one digit right of the decimal dot. This allows us to distingish them from integers, if you need this.

x <- 1:5
y <- as.numeric(x)
(json_x <- jsonlite::toJSON(x, always_decimal = TRUE))
# [1,2,3,4,5]

(json_y <- jsonlite::toJSON(y, always_decimal = TRUE))
# [1.0,2.0,3.0,4.0,5.0]


By formatting doubles this way they naturally get parsed back into doubles. So we can roundtrip numbers between R and json without losing type:

identical(x, jsonlite::fromJSON(json_x))
# TRUE

identical(y, jsonlite::fromJSON(json_y))
# TRUE


You should only use this if you really need it. The json format itself does not specify number types, hence there is no guarantee that an arbitrary json parser will distinguish between integers and doubles. Indeed, most json parsers might simply parse any number into a double, which is totally correct as well.

Also setting always_decimal = TRUE introduces some performance overhead.

## Numbers in MongoDB and Mongolite

The main motivation for this feature was to insert data from R into MongoDB using the mongolite package. Several users of mongolite had requested that it would be nice to retain number types, especially when reading the data from MongoDB back into a strong typed language such as C++.

The latest version of mongolite automatically takes advantage of this feature:

# Get latest mongolite
devtools::install_github("jeroenooms/mongolite")

# Assuming you have a local mongod running
library(mongolite)
df <- data.frame(x = 1:5, y = as.numeric(1:5))
m <- mongo("testnum")
m$insert(df) out <- m$find()
identical(out, df)
# TRUE


This makes it even more seamless to use MongoDB as a backend for storing data frames in 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.