Schema: Schema

SchemaR Documentation

Schema

Description

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

ropensci/staypuft documentation built on Sept. 9, 2022, 4:35 p.m.