**OpenCPU**, and kindly contributed to R-bloggers)

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!

**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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...