json: Convert Objects to JSON

Description Usage Arguments Details Examples

Description

When building a web server or API it is often useful to send more complex data obects. Within prairie one can do this by converting the object to JSON. Additionally, prairie exposes the generic as.json function allowing users to specify how their custom classes need to be converted to JSON.

Usage

1
2
3
4
5
6
7
8
9
is.json(x)

as.json(x, ...)

## S3 method for class 'list'
as.json(x, ...)

## S3 method for class 'data.frame'
as.json(x, ...)

Arguments

x

Any R object.

...

Additional arguments to pass on to jsonlite::toJSON.

Details

For the included as.json functions, the excellent package jsonlite does all the heavy lifting behind the scenes. This package is straightforward to use and is recommended for those who wish to create further as.json functions.

Setting a response object body as an object with class json will automatically set the response object's Content-Type to application/json.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
as.json(list(one = 'fish', two = 'fish'))
as.json(data.frame(red = 'fish', blue = 'fish'))

res <- response()
body(res) <- as.json(
  list(
    list(name = 'ged', job = 'wizard'),
    list(name = 'sparrowhawk', job = 'wizard')
  )
)

is.json(body(res))
res[['Content-Type']] == 'application/json'

nteetor/prairie documentation built on May 24, 2019, 9:56 a.m.