as.json {Proc4} | R Documentation |
These methods extend the toJSON
function
providing an extensible protocol for serializing S4 objects. The
function as.json
turns the object into a string containing a
JSON document by first calling as.jlist
to convert the object
into a list and then calling toJSON
to do the work.
as.json(x, serialize=TRUE) ## S4 method for signature 'ANY' as.json(x, serialize=TRUE) as.jlist(obj,ml, serialize=TRUE)
x |
An (S4) object to be serialized. |
obj |
The object being serialized |
ml |
A list of fields of the object; usually |
serialize |
A logical flag. If true,
|
The existing toJSON
does not support S4
objects, and the serializeJSON
provides too
much detail; so while it is good for saving and restoring R objects,
it is not good for sharing data between programs. The function
as.json
and as.jlist
are S4 generics, so they can be
easily extended to other classes.
The default method for as.json
is essentially toJSON(
as.jlist(x, attributes(x)))
. The function attributes(x)
turns
the fields of the object into a list, and then the appropriate method
for as.jlist
further processes those objects. For example, it
can set the "_id"
field used by the Mongo DB as a unique
identifier (or other derived fields) to NULL
.
Another important step is to call unboxer
on fields which should
not be stored as vectors. The function toJSON
by default wraps
all R objects in ‘[]’ (after all, they are all vectors), but
that is probably not useful if the field is to be used as an index.
Wrapping the field in unboxer()
, i.e., using ml$field <-
unboxer(ml$field)
, suppresses the brackets. The function
unboxer()
in this package is an extension of the
jsonlite::unbox
function, which does not
properly unbox POSIXt objects.
Finally, for a field that can contain arbitrary R objects, the
function unparseData
coverts the data into a JSON string
which will completely recover the data. The serialize
argument
is passed to this function. If true, then
serializeJSON
is used which produces safe, but
not particularly human editable JSON. If false, a simpler method is
employed which produes more human readable code. This with should
work for simpler data types, but does not support objects, and may
fail with complex lists.
The function as.json
returns a unicode string with a serialized
version of the object.
The function as.jlist
returns a list of the fields of the
object which need to be serialized (usually through a call to
toJSON
.
Russell Almond
In this package:
parseMessage
, saveRec
,
parseData
In the jsonlite package:
toJSON
, serializeJSON
,
jsonlite::unbox
mess1 <- P4Message("Fred","Task 1","Evidence ID","Scored Response", as.POSIXct("2018-11-04 21:15:25 EST"), list(correct=TRUE,seletion="D")) as.json(mess1) as.json(mess1,FALSE) ## Not run: ## This is the method for P4 Messages. setMethod("as.jlist",c("P4Message","list"), function(obj,ml) { ml$"_id" <- NULL ml$class <-NULL ## Use manual unboxing for finer control. ml$app <- unboxer(ml$app) ml$uid <- unboxer(ml$uid) if (!is.null(ml$context) && length(ml$context)==1L) ml$context <- unboxer(ml$context) if (!is.null(ml$sender) && length(ml$sender)==1L) ml$sender <- unboxer(ml$sender) if (!is.null(ml$mess) && length(ml$mess)==1L) ml$mess <- unboxer(ml$mess) ml$timestamp <- unboxer(ml$timestamp) # Auto_unboxer bug. ## Saves name data; need recursvie version. ml$data <- unparseData(ml$data) ml }) ## End(Not run)