CRAN_Status_Badge downloadsCRAN RStudio mirror downloads Travis build status Coverage status

jsonify converts R objects to JSON.

There are already JSON converters, why did you build this one?

Because I wanted it available at the source ( C++ ) level for integrating into other packages.

I thought you had an example of it being MUCH quicker than jsonlite ?

Yeah, but I realised it was comparing two different methods. When jsonify was parsing nested lists, it was parsing data.frames column-wise, whereas jsonlite was row-wise. Which is a slower operation

There’s no Date type in JSON, how have you handled this?

At its core Dates in R are numeric, so they are treated as numbers when converted to JSON. However, the user can coerce to character through the numeric_dates argument.

df <- data.frame(dte = as.Date("2018-01-01"))
jsonify::to_json( df )
#  [{"dte":17532.0}]

df <- data.frame(dte = as.Date("2018-01-01"))
jsonify::to_json( df, numeric_dates = FALSE )
#  [{"dte":"2018-01-01"}]

And POSIXct and POSIXlt?

The same


jsonify::to_json( as.POSIXct("2018-01-01 10:00:00") )
#  [1514761200.0]
jsonify::to_json( as.POSIXct("2018-01-01 10:00:00"), numeric_dates = FALSE)
#  ["2017-12-31T23:00:00"]

However, here the POSIXct values are returned in UTC timezone. This is by design.

POSIXlt will return each component of the date-time

What about lists?

The purpose of this library is speed. A lot of overhead is incurred iterating over a list to find and convert objects from one type to another.

For v0.2.0 I’ve managed to get the date handling at the c++ level, so there’s no penalty for recursing through the list to coerce to character.

Therefore, lists will work too

And it’s still fast because of the design choice to coerce dates to UTC. All the date handling is done at the C++ leve, not R. So it’s faster.

That output looks nice, is that pretty_json() function new?

Yep, it’s a new feature in v0.2.0

What do you mean by “available at the source” ?

I want to be able to call the C++ code from another package, without going to & from R. Therefore, the C++ code is implemented in headers, so you can “link to” it in your own package.

For example, the LinkingTo section in DESCRIPTION will look something like

And in a c++ source file you can #include the header and use the available functions

Can I call it from R if I want to?

Yes. Just like the examples in this readme use to_json()

I see factors are converted to strings

Yep. Even though I constructed a data.frame() without setting stringsAsFactros = FALSE, jsonify automatically treats factors as strings.

Has this changed from v0.1?

Yes. And it’s to keep the data more inline with modern concepts and design patterns.

If you want factors, use factors_as_string = FALSE in the to_json() call

How do I install it?

Get the latest release version from CRAN

install.packages("jsonify")

Or the development version from GitHub with: