Skip to content

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

Usage

cereal_encode(x)

Arguments

x

A vector

Value

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

Details

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

Examples

cereal_encode(1:10)
#> $type
#> [1] "integer"
#> 
#> $example
#> [1] "1"
#> 
#> $details
#> list()
#> 
cereal_encode(Sys.Date())
#> $type
#> [1] "Date"
#> 
#> $example
#> [1] "2023-06-09"
#> 
#> $details
#> list()
#> 
cereal_encode(sample(letters, 5))
#> $type
#> [1] "character"
#> 
#> $example
#> [1] "w"
#> 
#> $details
#> list()
#> 
cereal_encode(factor(letters[1:5], labels = "letter"))
#> $type
#> [1] "factor"
#> 
#> $example
#> [1] "letter1"
#> 
#> $details
#> $details$levels
#> [1] "letter1" "letter2" "letter3" "letter4" "letter5"
#> 
#> 
cereal_encode(factor(LETTERS[3:1], ordered = TRUE))
#> $type
#> [1] "ordered"
#> 
#> $example
#> [1] "C"
#> 
#> $details
#> $details$levels
#> [1] "A" "B" "C"
#> 
#> 

## you can encode a ptype as well:
ptype <- vctrs::vec_ptype(factor(LETTERS[3:1], ordered = TRUE))
## but "example" is NULL:
cereal_encode(ptype)
#> $type
#> [1] "ordered"
#> 
#> $example
#> NULL
#> 
#> $details
#> $details$levels
#> [1] "A" "B" "C"
#> 
#>