unboxer {Proc4} | R Documentation |
The function toJSON
coverts vectors (which all
R objects are) to vectors in the JSON code. The function
jsonlite::unbox
protects the object from this
behavior, which makes the fields eaiser to search and protects against
loss of name attributes. The function unboxer
extents
unbox
to recursively unbox lists (which preserves names).
The function ununbox
removes the unboxing flag and is mainly
used for testing parser code.
unboxer(x) ununboxer(x)
x |
Object to be boxed/unboxed. |
The jsonlite::unbox
function does not
necessarily preserve the name attributes of elements of the list.
In other words the sequence as.jlist
->
toJSON
-> fromJSON
->
parseMessage
might not be the identity.
The solution is to recursively apply unbox
to
the elements of the list. The function unboxer
can be thought
of as a recursive version of unbox
which handles the entire
tree struction. If x
is not a list, then unboxer
and
unbox
are equivalent.
The typical use of this function is defining methods for the
as.jlist
function. This gives the implementer fine
control of which attributes of a class should be scalars and vectors.
The function ununbox
clears the unboxing flag. Its main
purpose is to be able to test various parsers.
The function unboxer
returns the object with the added class
scalar
, which is the jsonlite
marker for a scalar.
The function ununboxer
returns the object without the
scalar
class marker.
These functions currently rely on some internal mechanisms of the
jsonline pacakge. In particular, it uses the internal function
jsonlite:::as.scalar
, and ununbox
relies on the
“scalar” class mechanism.
There is a bug in the way that POSIXt
classes are
handled, unboxer
fixes that problem.
Russell Almond
unbox
, toJSON
,
as.jlist
, parseMessage
## as.jlist method shows typical use of unboxer. getMethod("as.jlist",c("P4Message","list")) ## Use ununboxer to test as.jlist/parseMessage pair. m4 <- P4Message("Phred","Task1","PP","New Stats", details=list("agents"=c("ramp","ramp","lever"))) m4jl <- as.jlist(m4,attributes(m4)) m4a <- parseMessage(ununboxer(m4jl)) stopifnot(all.equal(m4,m4a))