Title: | Serialize 'vctrs' Objects to 'JSON' |
---|---|
Description: | The 'vctrs' package provides a concept of vector prototype that can be especially useful when deploying models and code. Serialize these object prototypes to 'JSON' so they can be used to check and coerce data in production systems, and deserialize 'JSON' back to the correct object prototypes. |
Authors: | Julia Silge [cre, aut] , Davis Vaughan [aut], Posit Software, PBC [cph, fnd] |
Maintainer: | Julia Silge <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2024-12-01 03:24:29 UTC |
Source: | https://github.com/r-lib/cereal |
Create a vctrs::vec_ptype()
from a JSON-serialized prototype created with
cereal_encode()
.
cereal_decode(x)
cereal_decode(x)
x |
An object with class "cereal_*", such as "cereal_integer" or "cereal_factor" |
A vector of zero length, such as integer()
or vctrs::new_factor()
cereal_decode(structure(list(), class = "cereal_integer")) cereal_decode(structure(list(), class = "cereal_Date"))
cereal_decode(structure(list(), class = "cereal_integer")) cereal_decode(structure(list(), class = "cereal_Date"))
Find needed details for vctrs prototype
cereal_details(x)
cereal_details(x)
x |
A vector |
A list
cereal_details(factor(letters[1:5], labels = "letter")) cereal_details(factor(LETTERS[3:1], ordered = TRUE)) cereal_details(as.POSIXct("2023-01-01", tz = "America/New_York"))
cereal_details(factor(letters[1:5], labels = "letter")) cereal_details(factor(LETTERS[3:1], ordered = TRUE)) cereal_details(as.POSIXct("2023-01-01", tz = "America/New_York"))
Create a list encoding the vctrs prototype (metadata) that can be stored as JSON.
cereal_encode(x)
cereal_encode(x)
x |
A vector |
Use the digits
option to specify how many digits after the decimal point
to record in JSON, for example via withr::local_options()
.
A list that can be converted to JSON with jsonlite::toJSON()
vctrs::vec_ptype()
, cereal_decode()
cereal_encode(1:10) cereal_encode(Sys.Date()) cereal_encode(sample(letters, 5)) cereal_encode(factor(letters[1:5], labels = "letter")) cereal_encode(factor(LETTERS[3:1], ordered = TRUE)) ## you can encode a ptype as well: ptype <- vctrs::vec_ptype(factor(LETTERS[3:1], ordered = TRUE)) ## but "example" is NULL: cereal_encode(ptype)
cereal_encode(1:10) cereal_encode(Sys.Date()) cereal_encode(sample(letters, 5)) cereal_encode(factor(letters[1:5], labels = "letter")) cereal_encode(factor(LETTERS[3:1], ordered = TRUE)) ## you can encode a ptype as well: ptype <- vctrs::vec_ptype(factor(LETTERS[3:1], ordered = TRUE)) ## but "example" is NULL: cereal_encode(ptype)
The function cereal_to_json()
serializes the
vctrs prototype
of a data frame to JSON, and the function cereal_from_json()
deserializes
from a JSON prototype back to a vctrs prototype.
cereal_to_json(data) cereal_from_json(x)
cereal_to_json(data) cereal_from_json(x)
data |
A data frame |
x |
A JSON string |
cereal_to_json()
returns a JSON string like jsonlite::toJSON()
,
and cereal_from_json()
returns a vctrs ptype, like vctrs::vec_ptype()
.
cereal_encode()
, cereal_decode()
df <- tibble::tibble( a = 1, b = 2L, c = Sys.Date(), d = as.POSIXct("2019-01-01", tz = "America/New_York"), e = "x", f = factor("blue", levels = c("blue", "green", "red")), g = ordered("small", levels = c("small", "medium", "large")) ) json <- cereal_to_json(df) json str(cereal_from_json(json)) ## same as: str(vctrs::vec_ptype(df))
df <- tibble::tibble( a = 1, b = 2L, c = Sys.Date(), d = as.POSIXct("2019-01-01", tz = "America/New_York"), e = "x", f = factor("blue", levels = c("blue", "green", "red")), g = ordered("small", levels = c("small", "medium", "large")) ) json <- cereal_to_json(df) json str(cereal_from_json(json)) ## same as: str(vctrs::vec_ptype(df))