Writes an R object to JSON with its type and attributes alongside the
value, so the round trip returns THE SAME OBJECT rather than something
that merely prints the same. The counterpart of jsonlite's
serializeJSON() / unserializeJSON(), computed by this
package's own codec with no jsonlite dependency.
Value
bricklayer_json_serialize() returns a length-1 character
vector of class json; bricklayer_json_unserialize()
returns the original object.
Details
Use this, not
bricklayer_json_to_json(),
whenever the JSON has to reconstruct the object faithfully.
bricklayer_json_to_json() writes the DATA – which is what an API
or a human wants, and which loses factor levels, matrix dimensions,
classes and every other attribute. These two keep them, at the cost of
JSON no other tool will understand.
See also
bricklayer_json_to_json() for
plain data JSON, core_sha256() for
fingerprinting the result.
Examples
# A factor survives the round trip with its levels intact.
f <- factor(c("b", "a", "b"), levels = c("a", "b", "c"))
back <- bricklayer_json_unserialize(bricklayer_json_serialize(f))
identical(back, f)
#> [1] TRUE
# So does a matrix, with its dimensions.
m <- matrix(1:6, nrow = 2)
identical(bricklayer_json_unserialize(bricklayer_json_serialize(m)), m)
#> [1] TRUE
# Plain data JSON does not keep either, which is the trade-off.
bricklayer_json_to_json(f)
#> ["b","a","b"]
# Nested lists, names and NULLs round trip too.
x <- list(a = 1:3, b = list(c = "x", d = NULL), e = TRUE)
identical(bricklayer_json_unserialize(bricklayer_json_serialize(x)), x)
#> [1] TRUE
# The serialised form is JSON, so it can be pinned like any other text.
nchar(core_sha256(bricklayer_json_serialize(m)))
#> [1] 64