Schema | R Documentation |
Base schema class with which to define custom schemas
staypuft::SchemaABC
-> Schema
schema_name
the schema name
fields
field names
post_load
field names
many
xxxx
only
xxxx
exclude
xxxx
ordered
xxxx
load_only
xxxx
dump_only
xxxx
partial
xxxx
unknown
xxxx
context
xxxx
opts
field names
new()
Create a new Schema
object
Schema$new( schema_name, ..., post_load = NULL, only = NULL, exclude = NULL, many = FALSE, context = NULL, load_only = NULL, dump_only = NULL, partial = FALSE, unknown = "raise" )
schema_name
(character) the schema name
...
additional arguments, passed to fields
only
Whitelist of the declared fields to select when instantiating the Schema. If None, all fields are used. Nested fields can be represented with dot delimiters.
exclude
Blacklist of the declared fields to exclude
when instantiating the Schema. If a field appears in both only
and
exclude
, it is not used. Nested fields can be represented with dot
delimiters.
many
Should be set to True
if obj
is a collection
so that the object will be serialized to a list.
context
Optional context passed to :class:fields.Method
and
:class:fields.Function
fields.
load_only
Fields to skip during serialization (write-only fields)
dump_only
Fields to skip during deserialization (read-only fields)
partial
Whether to ignore missing fields and not require
any fields declared. Propagates down to Nested
fields as well. If
its value is an iterable, only missing fields listed in that iterable
will be ignored. Use dot delimiters to specify nested fields.
unknown
Whether to exclude, include, or raise an error for unknown
fields in the data. Use EXCLUDE
, INCLUDE
or RAISE
.
print()
print method for Schema
objects
Schema$print(x, ...)
x
self
...
ignored
dump()
Convert various objects to a list
Schema$dump(x, many = FALSE)
x
input
many
(logical) Should be set to TRUE
if obj
is a list of
lists. default: FALSE
list
dump_json()
Same as dump()
, but returns JSON
Schema$dump_json(x, ...)
x
input
...
additional params passed to jsonlite::toJSON()
JSON (character)
load()
Load data
Schema$load(data, many = FALSE, partial = FALSE, unknown = NULL, as_df = FALSE)
data
a named list
many
(logical) Should be set to TRUE
if obj
is a list of
lists. default: FALSE
partial
(logical) not implemented yet
unknown
(character) one or "raise", "exclude", or "include". default: "raise"
as_df
(logical) convert to tibble? default: FALSE
xxxx
load_json()
Same as load()
, but takes JSON as input
Schema$load_json(data, many = FALSE, partial = FALSE, unknown = NULL, ...)
data
a named list
many
(logical) Should be set to TRUE
if obj
is a list of
lists. default: FALSE
partial
(logical) not implemented yet
unknown
(character) one or "raise", "exclude", or "include". default: "raise"
...
additional params passed to jsonlite::fromJSON()
a list
load_df()
Same as load()
, but takes a data.frame as input
Schema$load_df(data, many = FALSE, partial = FALSE, unknown = NULL, ...)
data
a data.frame
many
(logical) Should be set to TRUE
if obj
is a list of
lists. default: FALSE
partial
(logical) not implemented yet
unknown
(character) one or "raise", "exclude", or "include". default: "raise"
...
additional params passed to jsonlite::fromJSON()
a list
clone()
The objects of this class are cloneable with this method.
Schema$clone(deep = FALSE)
deep
Whether to make a deep clone.
z <- Schema$new("FooBar", name = fields$character(), title = fields$character() ) z z$fields names(z$fields) x <- list(name = "Jane Doe", title = "Howdy doody") x z$dump(x) z$dump_json(x) z$dump_json(x, auto_unbox = TRUE) z <- Schema$new("MySchema", name = fields$character(), title = fields$character() ) z x <- list(name = "Jane Doe", title = "Howdy doody") z$load(x) z$load_json(jsonlite::toJSON(x, auto_unbox=TRUE)) # unknown field # x <- list(name = "Jane Doe", my_title = "Howdy doody") # z$load(x) # z$load_json(jsonlite::toJSON(x, auto_unbox=TRUE)) # as data.frame z <- Schema$new("MySchema", name = fields$character(), title = fields$character() ) x <- list(name = "Jane Doe", title = "hello world") z$load(x, as_df = TRUE) # list of lists z <- Schema$new("MySchema", name = fields$character(), title = fields$character() ) x <- list( list(name = "Jane Doe", title = "hello world"), list(name = "Alice Water", title = "bye mars") ) z$load(x, many = TRUE) # z$load(x, many = FALSE) # data.frame's x <- data.frame(name = "jill", title = "boss", stringsAsFactors = FALSE) x2 <- data.frame(name = c("jill", "jane"), title = c("boss", "ceo"), stringsAsFactors = FALSE) x2 <- data.frame(name = c("jill", "jane"), title = c("boss", "ceo"), stringsAsFactors = FALSE) z <- Schema$new("FooBar", name = fields$character(), title = fields$character() ) z$load_df(x) z$load_df(x2) z$load_df(x2, many = TRUE, simplifyVector = FALSE) # nested artist_schema <- Schema$new("ArtistSchema", name = fields$character(), role = fields$character(), instrument = fields$character() ) album_schema <- Schema$new("AlbumSchema", title = fields$character(), release_date = fields$date(), artist = fields$nested(artist_schema) ) artist_schema album_schema bowie <- list(name="David Bowie", role="lead", instrument="voice") album <- list(title="Hunky Dory", release_date="12-17-1971", artist=bowie) album_schema$dump(album) album_schema$load(album) ## many albums <- list( list(title="Hunky Dory", release_date="12-17-1971", artist=bowie), list(title="Mars and Venus", release_date="03-05-1974", artist=bowie) ) album_schema$dump(albums, many=TRUE) album_schema$load(albums, many=TRUE) ## bad album$artist <- list(stuff = "things") if (interactive()) album_schema$load(album) # Deserialize/load and create object with post_load z <- Schema$new("ArtistSchema", name = fields$character(), role = fields$character(), instrument = fields$character(), post_load = { function(x) structure(x, class = "Artist", attr = "hello") } ) z$post_load w <- list(name="David Bowie", role="lead", instrument="voice") z$load(w) print.Artist <- function(x) { cat("Artist\n") cat(sprintf(" name: %s\n", x$name)) cat(sprintf(" role: %s\n", x$role)) cat(sprintf(" instrument: %s\n", x$instrument)) } z$load(w) # from json json <- jsonlite::toJSON(w) z$load_json(json) ## many ww <- list( list(name="David Bowie", role="lead", instrument="voice"), list(name="Michael Jackson", role="lead", instrument="voice") ) json <- jsonlite::toJSON(ww) z$load_json(json, simplifyVector = FALSE, many = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.