json_base.Rd
Available base generic functions for objects that inherit json_class
are
base::print()
, base::[()
, base::c()
and
base::rep()
and for json_vec
objects, all of the above in addition
to base::[<-()
and
base::[[<-()
are implemented. For further
information on how these class-specific functions differ from their base
counterparts, refer to the details section.
# S3 method for json_class print(x, depth = 1L, width = getOption("width"), length = 100L, fancy = TRUE, ...) # S3 method for json_class [(x, i, ...) # S3 method for json_class c(x, ...) # S3 method for json_class rep(x, ...) # S3 method for json_vec print(x, depth = 1L, width = getOption("width"), length = 100L, fancy = TRUE, ...) # S3 method for json_vec [(x, i, ...) # S3 method for json_vec [(x, i, ...) <- value # S3 method for json_vec [[(x, i, ...) # S3 method for json_vec [[(x, i, ...) <- value # S3 method for json_vec c(x, ...) # S3 method for json_vec rep(x, ...) as_list(x, ...)
x | Object to print/combine/subset, etc. |
---|---|
depth | The maximum recursion depth for printing. |
width | Number of columns to maximally print. |
length | Number of lines to maximally print. |
fancy | Logical switch to enable font styles, colors and UTF box characters for printing. |
... | Generic compatibility. |
i | Index for sub-setting. See |
value | New values for replacement. See
|
Depending on whether a single or a set of multiple objects is
represented, the S3 classes json_class
or json_vec
are applied
respectively.
Single bracket sub-setting of json_class
objects preserves
class information, such that the resulting object has the same type but only
a subset of fields. Double bracket sub-setting of json_class
objects
removes the enclosing type, as would be expected considering the list nature
of json_class
objects. Combining or repeating json_class
objects yields
json_vec
objects with the same sub-type. Additionally, when combining
json_class
objects using base::c()
, only json_class
objects with the
same subtype as the first argument are allowed as further arguments.
Analogously to sub-setting of json_class
objects, sub-setting a json_vec
object with base::[()
returns a json_vec
object
with the same sub type as the one used as input, whereas sub-setting a
json_vec
object with base::[[()
yields the
selected json_class
object. Replacement operators
base::[<-()
and
base::[[<-()
mainly ensure that the objects
being inserted are of the correct sub-type, guaranteeing that all
json_class
members of a given json_vec
object are of the same sub-type.
Combining json_vec
objects with base::c()
is possible whenever the
object passed as first argument has the same sub-type as the objects passed
as further arguments, which additionally are required to be json_vec
objects as well. Repeating a json_vec
object using base::rep()
, results
in a json_vec
object of the same sub-type.
Printing of both json_class
and json_vec
objects is inspired by the
ast
printing function of Hadley's
lobstr package and borrows code from there.
Printing style can either be fancy (colors, UTF box characters. etc.) or
simple (controlled by the fancy
flag) and several options are available
for setting the max printing width/length, as well as a max recursion depth
for nested json_class
objects.
Other json object handling functions: has_fields.json_class
,
json_class
, json_vec
obj_c <- json_class(a = json_class(b = "c", class = "foo"), d = json_class(e = "f", class = "bar"), class = "foobar") obj_c#> █─foobar #> ├─a = █─foo #> │ └─... #> └─d = █─bar #> └─...#> █─foobar #> ├─a = █─foo #> │ └─b = c #> └─d = █─bar #> └─e = f#> █─foobar #> ├─a = █─foo #> │ └─b = c #> ...#> X-foobar #> +-a = X-foo #> | \-b = c #> \-d = X-bar #> \-e = f# sub-setting with single brackets preserves class information obj_c["a"]#> █─foobar #> └─a = █─foo #> └─...# whereas double brackets extract the selected element obj_c[["a"]]#> █─foo #> └─b = c# vectors of json_class objects are json_vec objects obj_cc <- rep(obj_c, 2) identical(obj_cc, c(obj_c, obj_c))#> [1] TRUE#> ┌─█─foobar #> │ ├─a = █─foo #> │ │ └─b = c #> │ └─d = █─bar #> │ └─e = f #> └─█─foobar #> ├─a = █─foo #> ...obj_g <- json_class(a = json_class(b = "g", class = "foo"), d = json_class(e = "h", class = "bar"), class = "foobar") obj_cg <- c(obj_c, obj_g) # sub-setting json_vec objects with single brackets yields json_vec objects class(obj_cg[1L])#> [1] "foobar" "json_vec"#> [1] "foobar" "json_class"#> [1] TRUE# json_vec objects can also be combined using c obj_i <- json_class(a = json_class(b = "i", class = "foo"), d = json_class(e = "j", class = "bar"), class = "foobar") obj_cgi <- c(obj_cg, json_vec(obj_i)) length(obj_cgi)#> [1] 3#> [1] 6# additionally replacement operators are available obj_cg[[2L]] <- obj_i obj_cgi[1L:2L] <- obj_cg identical(obj_cgi, c(obj_c, obj_i, obj_i))#> [1] TRUE