Skip to content

Commit e384ddd

Browse files
committed
Add parse_json() which does not try to detect urls or filepaths.
See also #265
1 parent d96cfdb commit e384ddd

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export(base64_enc)
77
export(flatten)
88
export(fromJSON)
99
export(minify)
10+
export(parse_json)
1011
export(prettify)
1112
export(rbind_pages)
1213
export(read_json)

R/fromJSON.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ fromJSON <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVec
9797
}
9898

9999
# call the actual function (with deprecated arguments)
100-
fromJSON_string(txt = txt, simplifyVector = simplifyVector, simplifyDataFrame = simplifyDataFrame,
100+
parse_and_simplify(txt = txt, simplifyVector = simplifyVector, simplifyDataFrame = simplifyDataFrame,
101101
simplifyMatrix = simplifyMatrix, flatten = flatten, ...)
102102
}
103103

104-
fromJSON_string <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector,
104+
parse_and_simplify <- function(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector,
105105
simplifyMatrix = simplifyVector, flatten = FALSE, unicode = TRUE, validate = TRUE, bigint_as_char = FALSE, ...){
106106

107107
if(!missing(unicode)){

R/read_json.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#' Read/write JSON
22
#'
3-
#' Convenience wrappers around \link{toJSON} and \link{fromJSON} to read and
4-
#' write directly to/from disk.
3+
#' These functions are similar to \link{toJSON} and \link{fromJSON} except they
4+
#' explicitliy distinguish between path and literal input, and do not simplify
5+
#' by default.
56
#'
6-
#' @rdname read_json
77
#' @export
8+
#' @rdname read_json
89
#' @param path file on disk
910
#' @param simplifyVector simplifies nested lists into vectors and data frames. See \link{fromJSON}.
1011
#' @examples tmp <- tempfile()
@@ -16,13 +17,20 @@
1617
#' # A data frame
1718
#' read_json(tmp, simplifyVector = TRUE)
1819
read_json <- function(path, simplifyVector = FALSE, ...){
19-
fromJSON(file(path), simplifyVector = simplifyVector, ...)
20+
parse_json(file(path), simplifyVector = simplifyVector, ...)
2021
}
2122

23+
#' @export
2224
#' @rdname read_json
25+
#' @param json string with literal json or connection object to read from
26+
parse_json <- function(json, simplifyVector = FALSE, ...){
27+
parse_and_simplify(json, simplifyVector = simplifyVector, ...)
28+
}
29+
2330
#' @export
31+
#' @rdname read_json
2432
#' @param x an object to be serialized to JSON
25-
#' @param ... additional arguments passed to \link{toJSON} or \link{fromJSON}
33+
#' @param ... additional conversion arguments, see also \link{toJSON} or \link{fromJSON}
2634
write_json <- function(x, path, ...) {
2735
json <- jsonlite::toJSON(x, ...)
2836
writeLines(json, path, useBytes = TRUE)

man/read_json.Rd

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)