Package 'cereal'

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-11-01 03:23:45 UTC
Source: https://github.com/r-lib/cereal

Help Index


Convert a JSON-serialized prototype to a vctrs prototype

Description

Create a vctrs::vec_ptype() from a JSON-serialized prototype created with cereal_encode().

Usage

cereal_decode(x)

Arguments

x

An object with class "cereal_*", such as "cereal_integer" or "cereal_factor"

Value

A vector of zero length, such as integer() or vctrs::new_factor()

Examples

cereal_decode(structure(list(), class = "cereal_integer"))
cereal_decode(structure(list(), class = "cereal_Date"))

Find needed details for vctrs prototype

Description

Find needed details for vctrs prototype

Usage

cereal_details(x)

Arguments

x

A vector

Value

A list

Examples

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"))

Encode a vector as JSON

Description

Create a list encoding the vctrs prototype (metadata) that can be stored as JSON.

Usage

cereal_encode(x)

Arguments

x

A vector

Details

Use the digits option to specify how many digits after the decimal point to record in JSON, for example via withr::local_options().

Value

A list that can be converted to JSON with jsonlite::toJSON()

See Also

vctrs::vec_ptype(), cereal_decode()

Examples

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)

Serialize and deserialize the prototype of a data frame to JSON

Description

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.

Usage

cereal_to_json(data)

cereal_from_json(x)

Arguments

data

A data frame

x

A JSON string

Value

cereal_to_json() returns a JSON string like jsonlite::toJSON(), and cereal_from_json() returns a vctrs ptype, like vctrs::vec_ptype().

See Also

cereal_encode(), cereal_decode()

Examples

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))