@@ -78,8 +78,15 @@ text_parse <- function(text, ..., parse_options = NULL) {
78
78
check_string(text )
79
79
parse_options <- parse_options %|| % parse_options()
80
80
check_no_text_parse_errors(" parse" , text , parse_options = parse_options )
81
- out <- js_call(jsonc , " ffi_text_parse" , text , parse_options )
82
- out
81
+
82
+ # In particular, JSON arrays can have mixed types like `[1, "a"]` and we
83
+ # don't want those to be forcibly simplified to `c("1", "a")` on the way in.
84
+ #
85
+ # We don't need this for every function, but when we parse a JSON file
86
+ # we want predictable output, and for us that means no simplification
87
+ simplify <- FALSE
88
+
89
+ jsonc $ call(" ffi_text_parse" , text , parse_options , simplify = simplify )
83
90
}
84
91
85
92
# ' @rdname parse
@@ -98,8 +105,13 @@ text_parse_at_path <- function(text, path, ..., parse_options = NULL) {
98
105
path <- check_and_normalize_path(path )
99
106
parse_options <- parse_options %|| % parse_options()
100
107
check_no_text_parse_errors(" parse" , text , parse_options = parse_options )
101
- out <- js_call(jsonc , " ffi_text_parse_at_path" , text , path , parse_options )
102
- out
108
+ jsonc $ call(
109
+ " ffi_text_parse_at_path" ,
110
+ text ,
111
+ path ,
112
+ parse_options ,
113
+ simplify = FALSE
114
+ )
103
115
}
104
116
105
117
# ' @rdname parse
@@ -141,7 +153,7 @@ text_parse_errors <- function(text, ..., parse_options = NULL) {
141
153
check_dots_empty0(... )
142
154
check_string(text , .internal = TRUE )
143
155
parse_options <- parse_options %|| % parse_options()
144
- js_call( jsonc , " ffi_text_parse_errors" , text , parse_options )
156
+ jsonc $ call( " ffi_text_parse_errors" , text , parse_options , simplify = FALSE )
145
157
}
146
158
147
159
check_no_text_parse_errors <- function (
@@ -253,46 +265,3 @@ error_code_to_error_message <- function(code) {
253
265
254
266
lookup [[code ]]
255
267
}
256
-
257
- # Call a JavaScript function
258
- #
259
- # This is `jsonc$call()` but with two changes:
260
- # - A `NULL` returns visibly
261
- # - The conversion from JSON does NO simplification
262
- #
263
- # In particular, JSON arrays can have mixed types like `[1, "a"]` and we
264
- # don't want those to be forcibly simplified to `c("1", "a")` on the way in.
265
- #
266
- # We don't need this for every function, but when we parse a JSON file
267
- # we want predictable output, and for us that means no simplification
268
- js_call <- function (context , fun , ... ) {
269
- args <- list (... )
270
-
271
- if (! is.null(names(args ))) {
272
- stop(" Named arguments are not supported in JavaScript." )
273
- }
274
-
275
- args <- vapply(
276
- args ,
277
- function (x ) jsonlite :: toJSON(x , auto_unbox = TRUE ),
278
- character (1 )
279
- )
280
-
281
- args <- paste(args , collapse = " ," )
282
- src <- paste0(" (" , fun , " )(" , args , " );" )
283
-
284
- out <- context $ eval(src , serialize = TRUE , await = FALSE )
285
-
286
- if (is.null(out )) {
287
- # A JavaScript `undefined` becomes `NULL` and `fromJSON()` doesn't want that
288
- NULL
289
- } else {
290
- # Otherwise, assume JSON but DON'T SIMPLIFY for stability and predictability
291
- jsonlite :: fromJSON(
292
- out ,
293
- simplifyVector = FALSE ,
294
- simplifyDataFrame = FALSE ,
295
- simplifyMatrix = FALSE
296
- )
297
- }
298
- }
0 commit comments