
Write a list to .json, .json.gz, or both – byte-equal by construction
Source:R/write_json_or_gz.R
write_json_or_gz.RdSerialises data_list to JSON text once and writes that single
in-memory string to either the plain .json file, the gzipped
.json.gz file, or both – as selected by the write_json and
write_gz arguments. Because both outputs are written from the same
byte string, the .json.gz always decompresses to a file byte-
identical to the .json (when both are produced); the pair cannot
drift.
Replaces the older write_json_with_optional_gz() "sidecar" model.
Both flags are first-class peers: any combination except
write_json = FALSE, write_gz = FALSE is allowed.
Details
Both files come from a single jsonlite::toJSON() call. There is no
parallel serialisation, no patch-and-rewrite loop, and no possibility
for the .json and .json.gz to diverge.
At least one of write_json / write_gz must be TRUE; passing
both as FALSE errors immediately (the function would otherwise
produce nothing).
Examples
if (FALSE) { # \dontrun{
# Plain JSON only (default)
write_json_or_gz(my_config, "config.json")
# gzip only
write_json_or_gz(my_config, "config.json", write_json = FALSE, write_gz = TRUE)
# Both, guaranteed byte-equal
write_json_or_gz(my_config, "config.json", write_json = TRUE, write_gz = TRUE)
} # }