R/manifest.R

Defines functions to_json json_string json_number

# ============================ #
# The manifest                 #
# ============================ #

# The manifest travels inside the page, so its bytes are part of the artefact
# and three things about them are load bearing. A whole-valued double keeps its
# trailing ".0" and an integer does not have one, so the R type of every field
# matters. A double is written with the fewest digits that read back as the same
# number. The indented form puts one space per level and a space after the
# colon, and the compact form has no spaces at all. jsonlite writes valid JSON
# and not this JSON, which is why the writer is here.

#' @noRd
json_number <- function(x) {
    if (is.integer(x)) return(as.character(x))
    if (!is.finite(x)) stop("JSON has no representation for ", x, call. = FALSE)
    if (x == 0) return(if (1 / x < 0) "-0.0" else "0.0")
    d <- 0L
    repeat {
        s <- sprintf(paste0("%.", d, "e"), x)
        if (as.numeric(s) == x || d >= 17L) break
        d <- d + 1L
    }
    e <- as.integer(sub(".*e", "", s))
    if (e > -5L && e < 16L) {
        out <- sprintf(paste0("%.", max(0L, d - e), "f"), x)
        if (grepl(".", out, fixed = TRUE)) {
            out <- sub("0+$", "", out)
            if (grepl("\\.$", out)) out <- paste0(out, "0")
        } else out <- paste0(out, ".0")
        out
    } else {
        sprintf("%se%s%02d", sub("e.*", "", s), if (e < 0L) "-" else "+", abs(e))
    }
}

#' @noRd
json_string <- function(x) {
    x <- gsub("\\", "\\\\", x, fixed = TRUE)
    x <- gsub("\"", "\\\"", x, fixed = TRUE)
    x <- gsub("\n", "\\n", x, fixed = TRUE)
    x <- gsub("\t", "\\t", x, fixed = TRUE)
    x <- gsub("\r", "\\r", x, fixed = TRUE)
    paste0("\"", x, "\"")
}

# `indent` of NULL gives the compact form. A named list is an object; a list
# without names, or an atomic vector of length other than one, is an array.
#' @noRd
to_json <- function(x, indent = NULL, level = 0L) {
    nl <- function(k) if (is.null(indent)) "" else
        paste0("\n", strrep(" ", indent * (level + k)))
    colon <- if (is.null(indent)) ":" else ": "
    if (is.list(x)) {
        if (length(x) == 0L) return(if (!is.null(names(x))) "{}" else "[]")
        parts <- vapply(x, to_json, "", indent = indent, level = level + 1L)
        if (!is.null(names(x))) {
            body <- paste0(json_string(names(x)), colon, parts, collapse = paste0(",", nl(1)))
            paste0("{", nl(1), body, nl(0), "}")
        } else {
            paste0("[", nl(1), paste(parts, collapse = paste0(",", nl(1))), nl(0), "]")
        }
    } else if (is.character(x)) {
        if (length(x) == 1L && !inherits(x, "AsIs")) json_string(x)
        else paste0("[", nl(1), paste(vapply(x, json_string, ""),
                                      collapse = paste0(",", nl(1))), nl(0), "]")
    } else if (is.logical(x) && length(x) == 1L) {
        if (x) "true" else "false"
    } else {
        if (length(x) == 1L && !inherits(x, "AsIs")) json_number(x)
        else paste0("[", nl(1), paste(vapply(x, json_number, ""),
                                      collapse = paste0(",", nl(1))), nl(0), "]")
    }
}

Try the volumr package in your browser

Any scripts or data that you put into this service are public.

volumr documentation built on Dec. 2, 2026, 5:07 p.m.