json_base: Base generics for JSON objects

print.json_classR Documentation

Base generics for JSON objects

Description

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.

Usage

## S3 method for class 'json_class'
print(x, depth = 1L, width = getOption("width"),
  length = 100L, fancy = TRUE, ...)

## S3 method for class 'json_class'
x[i, ...]

## S3 method for class 'json_class'
c(x, ...)

## S3 method for class 'json_class'
rep(x, ...)

## S3 method for class 'json_vec'
print(x, depth = 1L, width = getOption("width"),
  length = 100L, fancy = TRUE, ...)

## S3 method for class 'json_vec'
x[i, ...]

## S3 replacement method for class 'json_vec'
x[i, ...] <- value

## S3 method for class 'json_vec'
x[[i, ...]]

## S3 replacement method for class 'json_vec'
x[[i, ...]] <- value

## S3 method for class 'json_vec'
c(x, ...)

## S3 method for class 'json_vec'
rep(x, ...)

as_list(x, ...)

Arguments

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 base::[() and base::[[().

value

New values for replacement. See base::[<-() and base::[[<-().

Details

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.

Value

Depending on whether a single or a set of multiple objects is represented, the S3 classes json_class or json_vec are applied respectively.

See Also

Other json object handling functions: has_fields.json_class, json_class, json_vec

Examples

obj_c <- json_class(a = json_class(b = "c", class = "foo"),
                    d = json_class(e = "f", class = "bar"),
                    class = "foobar")
obj_c
print(obj_c, depth = 2L)
print(obj_c, depth = 2L, length = 4L)
print(obj_c, depth = 2L, fancy = FALSE)

# sub-setting with single brackets preserves class information
obj_c["a"]
# whereas double brackets extract the selected element
obj_c[["a"]]

# vectors of json_class objects are json_vec objects
obj_cc <- rep(obj_c, 2)
identical(obj_cc, c(obj_c, obj_c))

print(obj_cc, depth = 2L, length = 8L)

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])
# and with double brackets, the selected json_class object is extracted
class(obj_cg[[1L]])
identical(obj_cg[1L], json_vec(obj_cg[[1L]]))

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

# and repeated using rep
length(rep(obj_cgi, 2))

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


nbenn/infx documentation built on May 20, 2022, 7:44 a.m.