Base schema class with which to define custom schemas

Super class

staypuft::SchemaABC -> Schema

Public fields

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

Methods

Public methods

Inherited methods

Method new()

Create a new Schema object

Usage

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

Arguments

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.


Method print()

print method for Schema objects

Usage

Schema$print(x, ...)

Arguments

x

self

...

ignored


Method dump()

Convert various objects to a list

Usage

Schema$dump(x, many = FALSE)

Arguments

x

input

many

(logical) Should be set to TRUE if obj is a list of lists. default: FALSE

Returns

list


Method dump_json()

Same as dump(), but returns JSON

Usage

Schema$dump_json(x, ...)

Arguments

x

input

...

additional params passed to jsonlite::toJSON()

Returns

JSON (character)


Method load()

Load data

Usage

Schema$load(data, many = FALSE, partial = FALSE, unknown = NULL, as_df = FALSE)

Arguments

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

Returns

xxxx


Method load_json()

Same as load(), but takes JSON as input

Usage

Schema$load_json(data, many = FALSE, partial = FALSE, unknown = NULL, ...)

Arguments

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

Returns

a list


Method load_df()

Same as load(), but takes a data.frame as input

Usage

Schema$load_df(data, many = FALSE, partial = FALSE, unknown = NULL, ...)

Arguments

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

Returns

a list


Method clone()

The objects of this class are cloneable with this method.

Usage

Schema$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

z <- Schema$new("FooBar", name = fields$character(), title = fields$character() ) z
#> <schema: FooBar> #> fields: name, title
z$fields
#> $name #> <fields.Character> #> default=Missing #> attribute=none #> validate=none #> required=FALSE #> load_only=FALSE #> dump_only=FALSE #> missing=Missing #> allow_none=FALSE #> error_messages=required: 'Missing data for required field.'; null: 'Field may not be null.'; validator_failed: 'Invalid value.'; invalid: 'Not a valid string.'; invalid_utf8: 'Not a valid utf-8 string.' #> #> $title #> <fields.Character> #> default=Missing #> attribute=none #> validate=none #> required=FALSE #> load_only=FALSE #> dump_only=FALSE #> missing=Missing #> allow_none=FALSE #> error_messages=required: 'Missing data for required field.'; null: 'Field may not be null.'; validator_failed: 'Invalid value.'; invalid: 'Not a valid string.'; invalid_utf8: 'Not a valid utf-8 string.' #>
names(z$fields)
#> [1] "name" "title"
x <- list(name = "Jane Doe", title = "Howdy doody") x
#> $name #> [1] "Jane Doe" #> #> $title #> [1] "Howdy doody" #>
z$dump(x)
#> $name #> [1] "Jane Doe" #> #> $title #> [1] "Howdy doody" #>
z$dump_json(x)
#> {"name":["Jane Doe"],"title":["Howdy doody"]}
z$dump_json(x, auto_unbox = TRUE)
#> {"name":"Jane Doe","title":"Howdy doody"}
z <- Schema$new("MySchema", name = fields$character(), title = fields$character() ) z
#> <schema: MySchema> #> fields: name, title
x <- list(name = "Jane Doe", title = "Howdy doody") z$load(x)
#> $name #> [1] "Jane Doe" #> #> $title #> [1] "Howdy doody" #>
z$load_json(jsonlite::toJSON(x, auto_unbox=TRUE))
#> $name #> [1] "Jane Doe" #> #> $title #> [1] "Howdy doody" #>
# 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)
#> # A tibble: 1 x 2 #> name title #> <chr> <chr> #> 1 Jane Doe hello world
# 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)
#> [[1]] #> [[1]]$name #> [1] "Jane Doe" #> #> [[1]]$title #> [1] "hello world" #> #> #> [[2]] #> [[2]]$name #> [1] "Alice Water" #> #> [[2]]$title #> [1] "bye mars" #> #>
# 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)
#> $name #> [1] "jill" #> #> $title #> [1] "boss" #>
z$load_df(x2)
#> $name #> [1] "jill" "jane" #> #> $title #> [1] "boss" "ceo" #>
z$load_df(x2, many = TRUE, simplifyVector = FALSE)
#> [[1]] #> [[1]]$name #> [1] "jill" #> #> [[1]]$title #> [1] "boss" #> #> #> [[2]] #> [[2]]$name #> [1] "jane" #> #> [[2]]$title #> [1] "ceo" #> #>
# 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
#> <schema: ArtistSchema> #> fields: name, role, instrument
album_schema
#> <schema: AlbumSchema> #> fields: title, release_date, artist
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)
#> $title #> [1] "Hunky Dory" #> #> $release_date #> [1] "12-17-1971" #> #> $artist #> $artist$name #> [1] "David Bowie" #> #> $artist$role #> [1] "lead" #> #> $artist$instrument #> [1] "voice" #> #>
album_schema$load(album)
#> $title #> [1] "Hunky Dory" #> #> $release_date #> [1] "1971-12-17 UTC" #> #> $artist #> $artist$name #> [1] "David Bowie" #> #> $artist$role #> [1] "lead" #> #> $artist$instrument #> [1] "voice" #> #>
## 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)
#> [[1]] #> [[1]]$title #> [1] "Hunky Dory" #> #> [[1]]$release_date #> [1] "12-17-1971" #> #> [[1]]$artist #> [[1]]$artist$name #> [1] "David Bowie" #> #> [[1]]$artist$role #> [1] "lead" #> #> [[1]]$artist$instrument #> [1] "voice" #> #> #> #> [[2]] #> [[2]]$title #> [1] "Mars and Venus" #> #> [[2]]$release_date #> [1] "03-05-1974" #> #> [[2]]$artist #> [[2]]$artist$name #> [1] "David Bowie" #> #> [[2]]$artist$role #> [1] "lead" #> #> [[2]]$artist$instrument #> [1] "voice" #> #> #>
album_schema$load(albums, many=TRUE)
#> [[1]] #> [[1]]$title #> [1] "Hunky Dory" #> #> [[1]]$release_date #> [1] "1971-12-17 UTC" #> #> [[1]]$artist #> [[1]]$artist$name #> [1] "David Bowie" #> #> [[1]]$artist$role #> [1] "lead" #> #> [[1]]$artist$instrument #> [1] "voice" #> #> #> #> [[2]] #> [[2]]$title #> [1] "Mars and Venus" #> #> [[2]]$release_date #> [1] "1974-03-05 UTC" #> #> [[2]]$artist #> [[2]]$artist$name #> [1] "David Bowie" #> #> [[2]]$artist$role #> [1] "lead" #> #> [[2]]$artist$instrument #> [1] "voice" #> #> #>
## 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
#> function(x) structure(x, class = "Artist", attr = "hello") #> <environment: 0x5572c149fe90>
w <- list(name="David Bowie", role="lead", instrument="voice") z$load(w)
#> $name #> [1] "David Bowie" #> #> $role #> [1] "lead" #> #> $instrument #> [1] "voice" #> #> attr(,"class") #> [1] "Artist" #> attr(,"attr") #> [1] "hello"
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)
#> $name #> [1] "David Bowie" #> #> $role #> [1] "lead" #> #> $instrument #> [1] "voice" #> #> attr(,"class") #> [1] "Artist" #> attr(,"attr") #> [1] "hello"
# from json json <- jsonlite::toJSON(w) z$load_json(json)
#> $name #> [1] "David Bowie" #> #> $role #> [1] "lead" #> #> $instrument #> [1] "voice" #> #> attr(,"class") #> [1] "Artist" #> attr(,"attr") #> [1] "hello"
## 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)
#> [[1]] #> $name #> [1] "David Bowie" #> #> $role #> [1] "lead" #> #> $instrument #> [1] "voice" #> #> attr(,"class") #> [1] "Artist" #> attr(,"attr") #> [1] "hello" #> #> [[2]] #> $name #> [1] "Michael Jackson" #> #> $role #> [1] "lead" #> #> $instrument #> [1] "voice" #> #> attr(,"class") #> [1] "Artist" #> attr(,"attr") #> [1] "hello" #>