json_vec.Rd
In order to allow method dispatch on a set of json_class
objects without
resorting to iterating over the individual set members, vectors of
json_class
objects are wrapped by a json_vec
class. Iterating over
objects is in some cases inefficient because the openBIS API can for some
functions accept lists of objects. Assembling multiple json_class
objects
as a list in R however breaks method dispatch, as the type of this object
is list
instead of the desired json_class
sub-class. A json_vec
object
therefore represents a list of json_class
objects of the same sub-class
and brings this sub-class to the surface of the compound object.
json_vec(..., .simplify = FALSE) as_json_vec(x, ...) as.json_vec(x, ...) # S3 method for json_vec as_json_vec(x, simplify = FALSE, ...) # S3 method for json_class as_json_vec(x, simplify = FALSE, ...) # S3 method for list as_json_vec(x, recursive = TRUE, force = FALSE, simplify = FALSE, ...) # S3 method for default as_json_vec(x, force = FALSE, ...) # S3 method for json_vec as.list(x, recursive = FALSE, ...) is_json_vec(x) is.json_vec(x) has_common_subclass(x)
... | Individual |
---|---|
x | A single/list of |
simplify, .simplify | Logical switch indicating whether to simplify
|
recursive | Recursively apply the function. |
force | Suppress error when casting an object to |
Multiple json_class
objects of the same sub-type can be
represented as S3 objects with type json_vec
and the common sub-type as
second class attribute.
A json_vec
object can be instantiated using the json_vec()
constructor
which takes a list of json_class
objects of the same sub-class. An
existing list of json_class
objects can be coerced to json_vec
using
as_json_vec()
/as.json_vec()
and applying as_list()
/as.list()
to a
json_vec
object reverses the action of as_json_vec()
by removing all
json_vec
related class information.
The function is_json_vec()
and its alias is.json_vec()
can be used to
test whether an object is a proper json_vec
object. This requires that
all child elements have to be of the same sub-class
all child elements are required to be properly formed json_class
objects
the json_vec
class attribute has to be in last position
the remaining class attributes have to be equal to the common sub-class determined for the children.
Testing whether a list structure consists of json_class
objects which are
of the same sub-class can be done with has_common_subclass()
. This always
returns TRUE
if a json_class
object is passed and FALSE
if a non-list
structure is passed.
Other json object handling functions: has_fields.json_class
,
json_class
, print.json_class
a <- json_class(field = "a", class = "foo") b <- json_class(field = "b", class = "foo") ab <- json_vec(a, b) print(ab)#> ┌─█─foo #> │ └─field = a #> └─█─foo #> └─field = b#> [1] TRUE# as_json_vec() is idempotent identical(as_json_vec(list(a, b)), as_json_vec(as_json_vec(list(a, b))))#> [1] TRUE# a json_class object can be turned into a json_vec of length 1 ab_class <- json_class(foo1 = a, foo2 = b, class = "bar") length(ab_class)#> [1] 2#> [1] 1#> [1] TRUE# this might not be desirable in all cases; the argument simplify can be # used to only create json_vec objects of length greater than 1 identical(as_json_vec(list(a), simplify = TRUE), a)#> [1] TRUE# has_common_subclass() will alway return true for json_class objects has_common_subclass(a)#> [1] TRUE#> [1] TRUE# this includes json_vec objects has_common_subclass(ab)#> [1] TRUE#> [1] FALSE#> [1] FALSEis_json_vec(a)#> [1] FALSE#> [1] FALSEis_json_vec(ab)#> [1] TRUE