Converts between JSON text and ordinary R vectors and lists through a small, predictable set of functions, backed by vendored 'yyjson' < https://github.com/ibireme/yyjson> and requiring no system JSON library. Parsing accepts character, raw and file input and reports failures through structured conditions; serialization writes UTF-8 bytes suitable for use directly as an HTTP request body. The type mapping is deliberately narrow and fully documented, so what goes in and what comes out are both predictable.
zujson converts between JSON and ordinary R vectors and lists, using vendored
yyjson sources, so no system JSON library
is required. It exists to serve zuhttp: parsing a response body and building a
request body are the two things it is designed around, which makes it
deliberately narrower than jsonlite, with a type mapping that is fully documented.
Install the released version from CRAN:
install.packages("zujson")
Or the development version from GitHub:
# install.packages("pak")
pak::pak("pedrobtz/zujson")
json_parse() turns JSON into R objects. An array becomes an atomic vector when
its elements agree on a type, and a list when they do not — by default the type
is never coerced away, though simplify = "coerce" opts into R's own promotion
when you would rather have the vector.
library(zujson)
json_parse('{"ok": true, "ids": [1, 2, 3]}')
#> $ok
#> [1] TRUE
#>
#> $ids
#> [1] 1 2 3
json_write() goes the other way. A fully named vector or list becomes an
object, anything else becomes an array.
json_write(list(query = "cats", limit = 10L))
#> [1] "{\"query\":\"cats\",\"limit\":10}"
A response body arrives as raw bytes and a request body should leave as raw bytes, so both directions take and return them without a detour through a string:
json_write_raw(list(q = "cats"))
#> [1] 7b 22 71 22 3a 22 63 61 74 73 22 7d
json_parse(json_write_raw(list(q = "cats")))
#> $q
#> [1] "cats"
json_parse_ndjson() and json_write_ndjson() handle
application/x-ndjson; json_parse_file(), json_parse_raw(),
json_validate() and json_write_ndjson_raw() round out the set. The getting
started article carries
the full type mapping in both directions, the safety limits, and what the
package deliberately does not do.
zujson's tests cover its type mapping, errors and round trips, and run on every
pull request across five platform and R version combinations. Differential
tests use jsonlite as an external oracle, while JSONTestSuite checks 95/95
required acceptances and 188/188 required rejections. Sanitizer and fuzzing
jobs exercise the C boundary and cleanup paths. See
Testing and conformance
for the test map, full results and intentional divergences.