json_utils.Rd
Several utility functions for working with json_class
and json_vec
objects are provided. This includes has_fields()
for checking whether
certain fields are available in an object, get_field()
to extract
values from an object that correspond to a field with a certain name,
has_subclass()
for testing that an object is of a certain class and
get_subclass()
to extract this class. Finally, NULL fields can be
recursively removed using remove_null()
. More information is available
in the details section.
# S3 method for json_class has_fields(x, fields, ...) # S3 method for json_class get_field(x, field, ...) # S3 method for json_class has_subclass(x, class, ...) # S3 method for json_class get_subclass(x) has_fields(x, fields, ...) # S3 method for default has_fields(x, ...) get_field(x, field, ...) has_subclass(x, class, ...) # S3 method for default has_subclass(x, ...) get_subclass(x) # S3 method for list get_subclass(x, ...) remove_null(x) # S3 method for json_vec has_fields(x, fields, ...) # S3 method for json_vec get_field(x, field, ...) # S3 method for json_vec has_subclass(x, class, ...) # S3 method for json_vec get_subclass(x, ...)
x | Object to test. |
---|---|
fields | Character vector of nonzero length, holding the field names for which to check. |
... | Generic compatibility. |
field | Character vector of length 1, holding the field name to extract. |
class | Character vector of nonzero length, holding the class names to test for. |
Depending on whether a single or a set of multiple objects is
represented, the S3 classes json_class
or json_vec
are applied
respectively.
The generic function has_fields()
tests whether a single json_class
object contains all of the specified fields or whether each json_class
object contained in a json_vec
object passes this test. If dispatch
occurs on an object that is neither of class json_class
, nor of class
json_vec
, has_fields()
returns FALSE
. A single field can be extracted
from a json_class
or a json_vec
object, using get_field()
. Iteration
for json_vec
objects happens via base::sapply()
so that when possible
the result is simplified.
In order to test whether a json_class
or a json_vec
object is of a
certain sub-class (can also be a vector of sub-classes), the generic
function has_subclass()
can be used. Dispatch on objects that do not
inherit from either json_class
or json_vec
will return FALSE
. The
sub-class of a json_class
or a json_vec
object can be determined, using
get_subclass
. This will also work if dispatched on a list
of objects if
that list object passes has_common_subclass()
.
The function remove_null()
recursively removes all NULL fields from a
nested list structure while preserving json_class
and json_vec
class
attributes. This can be useful when fetching an object form openBIS and
subsequently using this object for a further query: whenever the object
returned by the first API call contains NULL fields, it is safer to remove
all of them, as in some cases this might cause an error in the following
API requests.
Other json object handling functions: json_class
,
json_vec
, print.json_class
obj_1 <- json_class(a = 1, b = 2, class = "foo") obj_2 <- json_class(a = 2, b = 4, class = "foo") obj_3 <- json_class(a = 3, c = 6, class = "foo") # one or more fields can be tested has_fields(obj_1, "a")#> [1] TRUE#> [1] TRUE#> [1] TRUE#> [1] TRUE#> [1] FALSE#> [1] FALSE#> [1] FALSEget_field(obj_1, "a")#> [1] 1#> [1] 1 3if (FALSE) { # the requested field must be available in every instance get_field(c(obj_1, obj_3), "b") # only a single field may be requested get_field(c(obj_1, obj_2), c("a", "b")) } obj_4 <- json_class(a = 4, c = 8, class = "bar") # dispatch on json_class has_subclass(obj_1, "foo")#> [1] TRUE#> [1] TRUE#> [1] FALSE# dispatch on json_class get_subclass(obj_1)#> [1] "foo"#> [1] "foo"# dispatch on list is possible if the list passes has_common_subclass() get_subclass(list(obj_1, obj_2))#> [1] "foo"if (FALSE) { get_subclass(list(obj_1, obj_4)) } tmp <- json_class(a = json_class(b = "c", d = NULL, class = "foo"), e = json_class(f = "g", class = "bar"), h = NULL, class = "foobar") print(tmp, 2)#> █─foobar #> ├─a = █─foo #> │ ├─b = c #> │ └─d = #> ├─e = █─bar #> │ └─f = g #> └─h =#> █─foobar #> ├─a = █─foo #> │ └─b = c #> └─e = █─bar #> └─f = g