New in jsonlite 0.9.22: distinguish between double and integer

June 15, 2016

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))

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

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

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

This makes it even more seamless to use MongoDB as a backend for storing data frames in R!