Skip to content

Commit

Permalink
update raw example
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Nov 20, 2023
1 parent 7360c78 commit 56556f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
74 changes: 37 additions & 37 deletions R/json.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Convert JSON in a character string to R
#'
#'
#' @param str a single character string
#' @param opts Named list of options for parsing. Usually created by \code{opts_read_json()}
#' @param ... Other named options can be used to override any options in \code{opts}.
Expand All @@ -13,79 +13,79 @@
#' \dontrun{
#' read_json_str(str, opts = parse_opts(int64 = 'string'))
#' }
#'
#'
#' @family JSON Parsers
#' @return R object
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
read_json_str <- function(str, opts = list(), ...) {
.Call(
parse_from_str_,
str,
parse_from_str_,
str,
modify_list(opts, list(...))
)
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Convert JSON in a raw vector to R
#'
#'
#' @inheritParams read_json_str
#' @param raw_vec raw vector
#'
#'
#' @examples
#' \dontrun{
#' a <- nanonext::ncurl("https://postman-echo.com/get", convert = FALSE)
#' read_json_raw(a$raw)
#' read_json_raw(a$data)
#' }
#'
#'
#' @family JSON Parsers
#' @return R object
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
read_json_raw <- function(raw_vec, opts = list(), ...) {
.Call(
parse_from_raw_,
raw_vec,
parse_from_raw_,
raw_vec,
modify_list(opts, list(...))
)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Convert JSON to R
#'
#'
#' @inheritParams read_json_str
#' @param filename full path to text file containing JSON.
#'
#'
#'
#'
#' @examples
#' \dontrun{
#' read_json_file("myfile.json")
#' }
#'
#'
#' @family JSON Parsers
#' @return R object
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
read_json_file <- function(filename, opts = list(), ...) {
.Call(
parse_from_file_,
filename,
parse_from_file_,
filename,
modify_list(opts, list(...))
)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Parse JSON from an R connection object.
#'
#' Currently, this isn't very efficient as the entire contents of the connection are
#'
#' Currently, this isn't very efficient as the entire contents of the connection are
#' read into R as a string and then the JSON parsed from there.
#'
#'
#' For plain text files it is faster to use
#' \code{read_json_file()}.
#'
#'
#' @inheritParams read_json_str
#' @param conn connection object. e.g. \code{url('https://jsonplaceholder.typicode.com/todos/1')}
#'
Expand All @@ -94,8 +94,8 @@ read_json_file <- function(filename, opts = list(), ...) {
#' \dontrun{
#' read_json_conn(url("https://api.github.com/users/hadley/repos"))
#' }
#'
#'
#'
#'
#' @family JSON Parsers
#' @return R object
#' @export
Expand All @@ -108,54 +108,54 @@ read_json_conn <- function(conn, opts = list(), ...) {

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Convert R object to JSON string
#'
#'
#' @param x the object to be encoded
#' @param opts Named list of serialization options. Usually created by [opts_write_json()]
#' @param ... Other named options can be used to override any options in \code{opts}.
#' The valid named options are identical to arguments to [opts_write_json()]
#'
#'
#' @return Character string
#'
#'
#' @examples
#' \dontrun{
#' write_json_str(iris, pretty = TRUE)
#' write_json_str(iris, opts = opts_write_json(auto_unbox = FALSE))
#' }
#'
#'
#' @family JSON Serializer
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
write_json_str <- function(x, opts = list(), ...) {
.Call(
serialize_to_str_,
x,
serialize_to_str_,
x,
modify_list(opts, list(...))
)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Convert R object to JSON file
#'
#'
#' @inheritParams write_json_str
#' @param filename filename
#'
#'
#' @examples
#' \dontrun{
#' write_json_str(iris, filename = "iris.json")
#' }
#'
#'
#' @family JSON Serializer
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
write_json_file <- function(x, filename, opts = list(), ...) {
.Call(
serialize_to_file_,
serialize_to_file_,
x,
filename,
filename,
modify_list(opts, list(...))
)

invisible(NULL)
}

Expand All @@ -166,15 +166,15 @@ write_json_file <- function(x, filename, opts = list(), ...) {
#' @inheritParams read_json_file
#' @param filename path to file containing JSON
#' @param str character string containing JSON
#' @param verbose logical. If the JSON is not valid, should a warning be
#' @param verbose logical. If the JSON is not valid, should a warning be
#' shown giving details?
#'
#' @return Logical value. TRUE if JSON validates as OK, otherwise FALSE
#' @export
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
validate_json_file <- function(filename, verbose = FALSE, opts = list(), ...) {
opts <- modify_list(opts, list(...))

.Call(
validate_json_file_,
filename,
Expand All @@ -189,7 +189,7 @@ validate_json_file <- function(filename, verbose = FALSE, opts = list(), ...) {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
validate_json_str <- function(str, verbose = FALSE, opts = list(), ...) {
opts <- modify_list(opts, list(...))

.Call(
validate_json_str_,
str,
Expand Down
2 changes: 1 addition & 1 deletion man/read_json_raw.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 56556f4

Please sign in to comment.