Skip to contents

zu_body_json() and zu_resp_json() use zujson by default (zujson::json_write() and zujson::json_parse()); this replaces it with anything else, jsonlite included.

Usage

zu_set_json_backend(encode = NULL, decode = NULL)

Arguments

encode

A function (x, ...) returning a single JSON string. Must accept ..., since request-level options are forwarded to it. NULL restores zujson's.

decode

A function (txt, ...) returning an R object. NULL restores zujson's.

Value

The previous backend, invisibly.

Examples

# Any pair of functions will do; a real one would wrap a JSON package,
# e.g. encode = function(x, ...) RcppSimdJson::serialize(x).
old <- zu_set_json_backend(
  encode = function(x, ...) sprintf('{"n":%d}', length(x)),
  decode = function(txt, ...) list(text = txt)
)
req <- zu_request("POST", "https://api.example.com")
req <- zu_body_json(req, list(1, 2))
rawToChar(req$body)
#> [1] "{\"n\":2}"

zu_set_json_backend(old$encode, old$decode)   # restore