Debian Codegraph @

R/json-read.R

raw
1
2 renv_json_read <- function(file = NULL, text = NULL) {
3
4 jlerr <- NULL
5
6 # if jsonlite is loaded, use that instead
7 if ("jsonlite" %in% loadedNamespaces()) {
8
9 json <- tryCatch(renv_json_read_jsonlite(file, text), error = identity)
10 if (!inherits(json, "error"))
11 return(json)
12
13 jlerr <- json
14
15 }
16
17 # otherwise, fall back to the default JSON reader
18 json <- tryCatch(renv_json_read_default(file, text), error = identity)
19 if (!inherits(json, "error"))
20 return(json)
21
22 # report an error
23 if (!is.null(jlerr))
24 stop(jlerr)
25 else
26 stop(json)
27
28 }
29
30 renv_json_read_jsonlite <- function(file = NULL, text = NULL) {
31 text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
32 jsonlite::fromJSON(txt = text, simplifyVector = FALSE)
33 }
34
35 renv_json_read_patterns <- function() {
36
37 list(
38
39 # objects
40 list("{", "\t\n\tobject(\t\n\t", TRUE),
41 list("}", "\t\n\t)\t\n\t", TRUE),
42
43 # arrays
44 list("[", "\t\n\tarray(\t\n\t", TRUE),
45 list("]", "\n\t\n)\n\t\n", TRUE),
46
47 # maps
48 list(":", "\t\n\t=\t\n\t", TRUE),
49
50 # newlines
51 list("\\u000a", "\n", FALSE)
52
53 )
54
55 }
56
57 renv_json_read_envir <- function() {
58
59 envir <- new.env(parent = emptyenv())
60
61 envir[["+"]] <- `+`
62 envir[["-"]] <- `-`
63
64 envir[["object"]] <- function(...) {
65 result <- list(...)
66 names(result) <- as.character(names(result))
67 result
68 }
69
70 envir[["array"]] <- list
71
72 envir[["true"]] <- TRUE
73 envir[["false"]] <- FALSE
74 envir[["null"]] <- NULL
75
76 envir
77
78 }
79
80 renv_json_read_remap <- function(object, patterns) {
81
82 # repair names if necessary
83 if (!is.null(names(object))) {
84
85 nms <- names(object)
86 for (pattern in patterns)
87 nms <- gsub(pattern[[2L]], pattern[[1L]], nms, fixed = TRUE)
88 names(object) <- nms
89
90 }
91
92 # repair strings if necessary
93 if (is.character(object)) {
94 for (pattern in patterns)
95 object <- gsub(pattern[[2L]], pattern[[1L]], object, fixed = TRUE)
96 }
97
98 # recurse for other objects
99 if (is.recursive(object))
100 for (i in seq_along(object))
101 object[i] <- list(renv_json_read_remap(object[[i]], patterns))
102
103 # return remapped object
104 object
105
106 }
107
108 renv_json_read_default <- function(file = NULL, text = NULL) {
109
110 # read json text
111 text <- paste(text %||% readLines(file, warn = FALSE), collapse = "\n")
112
113 # convert into something the R parser will understand
114 patterns <- renv_json_read_patterns()
115 transformed <- text
116 for (pattern in patterns)
117 transformed <- gsub(pattern[[1L]], pattern[[2L]], transformed, fixed = TRUE)
118
119 # parse it
120 rfile <- tempfile("renv-json-", fileext = ".R")
121 on.exit(unlink(rfile), add = TRUE)
122 writeLines(transformed, con = rfile)
123 json <- parse(rfile, keep.source = FALSE, srcfile = NULL)[[1L]]
124
125 # evaluate in safe environment
126 result <- eval(json, envir = renv_json_read_envir())
127
128 # fix up strings if necessary -- do so only with reversible patterns
129 patterns <- Filter(function(pattern) pattern[[3L]], patterns)
130 renv_json_read_remap(result, patterns)
131
132 }
133