Skip to content

Commit

Permalink
reorganize R implementation / documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmorgan committed Dec 25, 2023
1 parent 2d4fb6f commit 7f43e23
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 126 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rjsoncons
Title: 'C++' Header-Only 'jsoncons' Library for 'JSON' Queries
Version: 1.1.0.9201
Version: 1.1.0.9202
Authors@R: c(
person(
"Martin", "Morgan", role = c("aut", "cre"),
Expand Down
59 changes: 59 additions & 0 deletions R/as_r.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' @rdname as_r
#'
#' @title Parse JSON to R
#'
#' @description `as_r()` transforms a JSON string to an *R* object.
#'
#' @inheritParams jsonpath
#'
#' @details
#'
#' The `as = "R"` argument to `jsonpath()`, `jmespath()` and
#' `jsonpivot()`, and the `as_r()` function transform a JSON string
#' representation to an *R* object. Main rules are:
#'
#' - JSON arrays of a single type (boolean, integer, double, string)
#' are transformed to *R* vectors of the same length and
#' corresponding type. A JSON scalar and a JSON vector of length 1
#' are represented in the same way in *R*.
#'
#' - If a JSON 64-bit integer array contains a value larger than *R*'s
#' 32-bit integer representation, the array is transformed to an *R*
#' numeric vector. NOTE that this results in loss of precision for
#' 64-bit integer values greater than `2^53`.
#'
#' - JSON arrays mixing integer and double values are transformed to
#' *R* numeric vectors.
#'
#' - JSON objects are transformed to *R* named lists.
#'
#' The vignette reiterates this information and provides additional
#' details.
#'
#' @examples
#' ## as_r()
#' as_r('[1, 2, 3]') # JSON integer array -> R integer vector
#' as_r('[1, 2.0, 3]') # JSON intger and double array -> R numeric vector
#' as_r('[1, 2.0, "3"]') # JSON mixed array -> R list
#' as_r('[1, 2147483648]') # JSON integer > R integer max -> R numeric vector
#'
#' json <- '{"b": 1, "a": ["c", "d"], "e": true, "f": [true], "g": {}}'
#' as_r(json) |> str() # parsing complex objects
#' identical( # JSON scalar and length 1 array identical in R
#' as_r('{"a": 1}'), as_r('{"a": [1]}')
#' )
#'
#' @return `as_r()` returns an *R* object.
#'
#' @export
as_r <-
function(data, object_names = "asis", ...)
{
stopifnot(
.is_scalar_character(data),
.is_scalar_character(object_names)
)

data <- .as_json_string(data, ...)
cpp_as_r(data, object_names)
}
75 changes: 1 addition & 74 deletions R/jsoncons.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
#' @useDynLib rjsoncons, .registration = TRUE
NULL

#' @rdname jsoncons
#'
#' @title Query the jsoncons C++ library
#'
#' @description `version()` reports the version of the C++ jsoncons
#' library in use.
#'
#' @return `version()` returns a character(1) major.minor.patch
#' version string .
#'
#' @examples
#' version()
#'
#' @export
version <- cpp_version

#' @rdname jsoncons
#' @title Query JSON using the jsoncons C++ library
#'
#' @description `jsonpath()` executes a query against a JSON string
#' using the 'jsonpath' specification
Expand Down Expand Up @@ -201,59 +184,3 @@ jsonpivot <-
data <- .as_json_string(data, ...)
cpp_jsonpivot(data, object_names, as)
}

#' @rdname jsoncons
#'
#' @description `as_r()` transforms a JSON string to an *R* object.
#'
#' @details
#'
#' The `as = "R"` argument to `jsonpath()`, `jmespath()` and
#' `jsonpivot()`, and the `as_r()` function transform a JSON string
#' representation to an *R* object. Main rules are:
#'
#' - JSON arrays of a single type (boolean, integer, double, string)
#' are transformed to *R* vectors of the same length and
#' corresponding type. A JSON scalar and a JSON vector of length 1
#' are represented in the same way in *R*.
#'
#' - If a JSON 64-bit integer array contains a value larger than *R*'s
#' 32-bit integer representation, the array is transformed to an *R*
#' numeric vector. NOTE that this results in loss of precision for
#' 64-bit integer values greater than `2^53`.
#'
#' - JSON arrays mixing integer and double values are transformed to
#' *R* numeric vectors.
#'
#' - JSON objects are transformed to *R* named lists.
#'
#' The vignette reiterates this information and provides additional
#' details.
#'
#' @examples
#' ## as_r()
#' as_r('[1, 2, 3]') # JSON integer array -> R integer vector
#' as_r('[1, 2.0, 3]') # JSON intger and double array -> R numeric vector
#' as_r('[1, 2.0, "3"]') # JSON mixed array -> R list
#' as_r('[1, 2147483648]') # JSON integer > R integer max -> R numeric vector
#'
#' json = '{"b": 1, "a": ["c", "d"], "e": true, "f": [true], "g": {}}'
#' as_r(json) |> str() # parsing complex objects
#' identical( # JSON scalar and length 1 array identical in R
#' as_r('{"a": 1}'), as_r('{"a": [1]}')
#' )
#'
#' @return `as_r()` returns an *R* object.
#'
#' @export
as_r <-
function(data, object_names = "asis", ...)
{
stopifnot(
.is_scalar_character(data),
.is_scalar_character(object_names)
)

data <- .as_json_string(data, ...)
cpp_as_r(data, object_names)
}
18 changes: 18 additions & 0 deletions R/version.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' @useDynLib rjsoncons, .registration = TRUE
NULL

#' @rdname version
#'
#' @title Version of jsoncons C++ library
#'
#' @description `version()` reports the version of the C++ jsoncons
#' library in use.
#'
#' @return `version()` returns a character(1) major.minor.patch
#' version string .
#'
#' @examples
#' version()
#'
#' @export
version <- cpp_version
64 changes: 64 additions & 0 deletions man/as_r.Rd

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

52 changes: 1 addition & 51 deletions man/jsoncons.Rd

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

20 changes: 20 additions & 0 deletions man/version.Rd

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

0 comments on commit 7f43e23

Please sign in to comment.