Lightweight JSON Parsing and Serialization

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

Lifecycle: experimental R-CMD-check native-checks coverage

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.

Installation

Install the released version from CRAN:

install.packages("zujson")

Or the development version from GitHub:

# install.packages("pak")
pak::pak("pedrobtz/zujson")

Usage

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.

Testing

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.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("zujson")

0.1.0 by Pedro Baltazar, 9 days ago


https://github.com/pedrobtz/zujson, https://pedrobtz.github.io/zujson/


Report a bug at https://github.com/pedrobtz/zujson/issues


Browse source code at https://github.com/cran/zujson


Authors: Pedro Baltazar [aut, cre, cph] , YaoYuan [ctb, cph] (author of the bundled yyjson library)


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports utils

Suggests jsonlite, knitr, rmarkdown, testthat, withr


See at CRAN