Skip to content

Commit 4136fe7

Browse files
committed
Merge branch 'no-suggests-packages'
2 parents 72a80a8 + de05e80 commit 4136fe7

10 files changed

+165
-136
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Description: Functions to query (filter or transform), pivot (convert
2525
<https://danielaparker.github.io/jsoncons/> header-only library;
2626
the library is easily linked to other packages for direct access
2727
to 'C++' functionality not implemented here.
28-
Imports: cli
29-
Suggests: jsonlite, tibble, tinytest, BiocStyle, knitr, rmarkdown
28+
Imports: cli, tibble
29+
Suggests: jsonlite, tinytest, BiocStyle, knitr, rmarkdown
3030
LinkingTo: cpp11, cli
3131
License: BSL-1.0
3232
NeedsCompilation: yes

R/patch.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ j_patch_from <-
251251
#' be used in `j_patch_apply()`.
252252
#'
253253
#' @examples
254+
#' if (requireNamespace("jsonlite", quietly = TRUE)) {
254255
#' ## helper for constructing patch operations from R objects
255256
#' j_patch_op(
256257
#' "add", path = "/biscuits/1", value = list(name = "Ginger Nut"),
@@ -282,7 +283,7 @@ j_patch_from <-
282283
#' j_patch_op("add", "/biscuits/1", value = value) |>
283284
#' j_patch_op("copy", path = "/best_biscuit", from = "/biscuits/0")
284285
#' ops
285-
#'
286+
#' }
286287
#' @export
287288
j_patch_op <-
288289
function(op, path, ...)

R/paths_and_pointer.R

+8-4
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,24 @@
5757
#' jsonpath(json, "$..name", as = "R")
5858
#'
5959
#' ## create a list with state and name as scalar vectors
60-
#' lst <- jsonlite::fromJSON(json, simplifyVector = FALSE)
60+
#' lst <- as_r(json)
6161
#'
62+
#' if (requireNamespace("jsonlite", quietly = TRUE)) {
6263
#' ## objects other than scalar character vectors are automatically
6364
#' ## coerced to JSON; use `auto_unbox = TRUE` to represent R scalar
6465
#' ## vectors in the object as JSON scalar vectors
6566
#' jsonpath(lst, "$..name", auto_unbox = TRUE) |>
6667
#' cat("\n")
6768
#'
69+
#' ## use I("Seattle") to coerce to a JSON object ["Seattle"]
70+
#' jsonpath(I("Seattle"), "$[0]") |> cat("\n")
71+
#' }
72+
#'
6873
#' ## a scalar character vector like "Seattle" is not valid JSON...
6974
#' try(jsonpath("Seattle", "$"))
7075
#' ## ...but a double-quoted string is
7176
#' jsonpath('"Seattle"', "$")
7277
#'
73-
#' ## use I("Seattle") to coerce to a JSON object ["Seattle"]
74-
#' jsonpath(I("Seattle"), "$[0]") |> cat("\n")
75-
#'
7678
#' ## different ordering of object names -- 'asis' (default) or 'sort'
7779
#' json_obj <- '{"b": "1", "a": "2"}'
7880
#' jsonpath(json_obj, "$") |> cat("\n")
@@ -97,6 +99,7 @@ jsonpath <-
9799
#' jmespath(json, path) |>
98100
#' cat("\n")
99101
#'
102+
#' if (requireNamespace("jsonlite", quietly = TRUE)) {
100103
#' ## original filter always fails, e.g., '["WA"] != 'WA'
101104
#' jmespath(lst, path) # empty result set, '[]'
102105
#'
@@ -107,6 +110,7 @@ jsonpath <-
107110
#' ## automatically unbox scalar values when creating the JSON string
108111
#' jmespath(lst, path, auto_unbox = TRUE) |>
109112
#' cat("\n")
113+
#' }
110114
#'
111115
#' @export
112116
jmespath <-

inst/tinytest/test_as_r.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,5 @@ expect_identical(as_r(ndjson_file), json$locations)
151151

152152
expect_identical(as_r(readLines(ndjson_file)), json$locations)
153153

154-
expect_identical(as_r(json), json) # R -> R
154+
if (has_jsonlite)
155+
expect_identical(as_r(json), json) # R -> R

inst/tinytest/test_jsoncons.R

+52-46
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ json <- '{
77
]
88
}'
99

10-
datalist <- jsonlite::fromJSON(json, simplifyVector = FALSE)
10+
if (has_jsonlite)
11+
datalist <- jsonlite::fromJSON(json, simplifyVector = FALSE)
1112

1213
## version
1314

@@ -20,26 +21,28 @@ expect_identical(
2021
'["Seattle","New York","Bellevue","Olympia"]'
2122
)
2223

23-
expect_identical(
24-
## auto_unbox = FALSE
25-
jsonpath(datalist, "$..name"),
26-
'[["Seattle"],["New York"],["Bellevue"],["Olympia"]]'
27-
)
24+
if (has_jsonlite) {
25+
expect_identical(
26+
## auto_unbox = FALSE
27+
jsonpath(datalist, "$..name"),
28+
'[["Seattle"],["New York"],["Bellevue"],["Olympia"]]'
29+
)
2830

29-
expect_identical(
30-
jsonpath(datalist, "$..name", auto_unbox = TRUE),
31-
'["Seattle","New York","Bellevue","Olympia"]'
32-
)
31+
expect_identical(
32+
jsonpath(datalist, "$..name", auto_unbox = TRUE),
33+
'["Seattle","New York","Bellevue","Olympia"]'
34+
)
35+
36+
expect_identical(
37+
jsonpath(I("Seattle"), "$[0]"),
38+
'["Seattle"]'
39+
)
40+
}
3341

3442
expect_error(
3543
jsonpath("Seattle", "$[0]")
3644
)
3745

38-
expect_identical(
39-
jsonpath(I("Seattle"), "$[0]"),
40-
'["Seattle"]'
41-
)
42-
4346
expect_identical(
4447
jsonpath('{"b":"1","a":"2"}', "$", "asis"),
4548
'[{"b":"1","a":"2"}]'
@@ -64,37 +67,40 @@ expect_identical(
6467
'["Bellevue","Olympia","Seattle"]'
6568
)
6669

67-
expect_identical(
68-
## auto_unbox = FALSE, boxed 'state' (e.g., ['WA']) does not match
69-
## original filter
70-
jmespath(datalist, "locations[?state == 'WA'].name | sort(@)"),
71-
'[]'
72-
)
73-
74-
expect_identical(
75-
## auto_unbox = FALSE, query unboxed (`?state[0] == 'WA'`) state
76-
jmespath(datalist, "locations[?state[0] == 'WA'].name") ,
77-
'[["Seattle"],["Bellevue"],["Olympia"]]'
78-
)
79-
80-
expect_identical(
81-
## auto_unbox = FALSE, sort unboxed (`@[]`) result values
82-
jmespath(datalist, "locations[?state[0] == 'WA'].name | sort(@[])") ,
83-
'["Bellevue","Olympia","Seattle"]'
84-
)
85-
86-
expect_identical(
87-
## auto_unbox = FALSE, unbox name
88-
jmespath(datalist, "locations[?state[0] == 'WA'].name[] | sort(@)") ,
89-
'["Bellevue","Olympia","Seattle"]'
90-
)
91-
92-
expect_identical(
93-
jmespath(
94-
datalist, "locations[?state == 'WA'].name | sort(@)", auto_unbox = TRUE
95-
),
96-
'["Bellevue","Olympia","Seattle"]'
97-
)
70+
if (has_jsonlite) {
71+
expect_identical(
72+
## auto_unbox = FALSE, boxed 'state' (e.g., ['WA']) does not match
73+
## original filter
74+
jmespath(datalist, "locations[?state == 'WA'].name | sort(@)"),
75+
'[]'
76+
)
77+
78+
expect_identical(
79+
## auto_unbox = FALSE, query unboxed (`?state[0] == 'WA'`) state
80+
jmespath(datalist, "locations[?state[0] == 'WA'].name") ,
81+
'[["Seattle"],["Bellevue"],["Olympia"]]'
82+
)
83+
84+
expect_identical(
85+
## auto_unbox = FALSE, sort unboxed (`@[]`) result values
86+
jmespath(datalist, "locations[?state[0] == 'WA'].name | sort(@[])") ,
87+
'["Bellevue","Olympia","Seattle"]'
88+
)
89+
90+
expect_identical(
91+
## auto_unbox = FALSE, unbox name
92+
jmespath(datalist, "locations[?state[0] == 'WA'].name[] | sort(@)") ,
93+
'["Bellevue","Olympia","Seattle"]'
94+
)
95+
96+
expect_identical(
97+
jmespath(
98+
datalist, "locations[?state == 'WA'].name | sort(@)",
99+
auto_unbox = TRUE
100+
),
101+
'["Bellevue","Olympia","Seattle"]'
102+
)
103+
}
98104

99105
expect_identical(
100106
jmespath('{"b":"1","a":"2"}', "@", "asis"),

inst/tinytest/test_patch.R

+84-76
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ expect_identical(j_patch_apply(json, '[]', as = "R"), as_r(json))
99
patch <- '[{"op": "remove", "path": "/biscuits"}]'
1010
expect_identical(j_patch_apply(json, patch), "{}")
1111

12-
json_r = as_r(json) # 'data' is an R object
13-
expect_identical(j_patch_apply(json_r, patch, auto_unbox = TRUE), "{}")
12+
if (has_jsonlite) {
13+
json_r = as_r(json) # 'data' is an R object
14+
expect_identical(j_patch_apply(json_r, patch, auto_unbox = TRUE), "{}")
1415

15-
patch_r = as_r(patch) # 'patch' is an R object
16-
expect_identical(j_patch_apply(json, patch_r, auto_unbox = TRUE), "{}")
17-
expect_identical(j_patch_apply(json_r, patch_r, auto_unbox = TRUE), "{}")
16+
patch_r = as_r(patch) # 'patch' is an R object
17+
expect_identical(j_patch_apply(json, patch_r, auto_unbox = TRUE), "{}")
18+
expect_identical(j_patch_apply(json_r, patch_r, auto_unbox = TRUE), "{}")
19+
}
1820

1921
patch <- '{"op": "remove", "path": "/biscuits"}' # not an array
2022
expect_error(j_patch_apply(json, patch))
@@ -45,78 +47,82 @@ expect_identical(
4547
value = list(name = "Choco Leibniz")
4648
))
4749
)
48-
expect_identical(
49-
j_patch_from(j_patch_apply(json, patch, as = "R"), json, auto_unbox = TRUE),
50-
'[{"op":"add","path":"/biscuits/1","value":{"name":"Choco Leibniz"}}]'
51-
)
52-
expect_identical(
53-
j_patch_from(j_patch_apply(json, patch), json_r, auto_unbox = TRUE),
54-
'[{"op":"add","path":"/biscuits/1","value":{"name":"Choco Leibniz"}}]'
55-
)
56-
57-
## j_patch_op
5850

59-
value0 <- list(name = "Ginger Nut")
60-
value1 <- list(name = jsonlite::unbox("Ginger Nut"))
61-
path <- "/biscuits/1"
51+
if (has_jsonlite) {
52+
expect_identical(
53+
j_patch_from(j_patch_apply(json, patch, as = "R"), json, auto_unbox = TRUE),
54+
'[{"op":"add","path":"/biscuits/1","value":{"name":"Choco Leibniz"}}]'
55+
)
56+
expect_identical(
57+
j_patch_from(j_patch_apply(json, patch), json_r, auto_unbox = TRUE),
58+
'[{"op":"add","path":"/biscuits/1","value":{"name":"Choco Leibniz"}}]'
59+
)
60+
}
6261

63-
expect_identical(
64-
unclass(j_patch_op("add", path, value = value0)),
65-
c('[',
66-
'{"op":"add","path":"/biscuits/1","value":{"name":["Ginger Nut"]}}',
67-
']')
68-
)
69-
expect_identical(
70-
unclass(j_patch_op("add", path, value = value1)),
71-
c('[',
72-
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
73-
']')
74-
)
75-
expect_identical(
76-
unclass(j_patch_op("add", path, value = value0, auto_unbox = TRUE)),
77-
c('[',
78-
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
79-
']')
80-
)
81-
expect_identical(
82-
unclass(j_patch_op("remove", path)),
83-
c('[', '{"op":"remove","path":"/biscuits/1"}', ']')
84-
)
85-
expect_identical(
86-
unclass(j_patch_op("replace", path, value = value1)),
87-
c('[',
88-
'{"op":"replace","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
89-
']')
90-
)
91-
expect_identical(
92-
unclass(j_patch_op("copy", path, from = path)),
93-
c('[', '{"op":"copy","path":"/biscuits/1","from":"/biscuits/1"}', ']')
94-
)
95-
expect_identical(
96-
unclass(j_patch_op("move", path, from = path)),
97-
c('[', '{"op":"move","path":"/biscuits/1","from":"/biscuits/1"}', ']')
98-
)
99-
expect_identical(
100-
unclass(j_patch_op("test", path, value = value1)),
101-
c('[',
102-
'{"op":"test","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
103-
']')
104-
)
105-
106-
## concatenation and piping
107-
patch <- j_patch_op("add", path, value = value1)
108-
expected <- c(
109-
'[',
110-
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
111-
',',
112-
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
113-
']'
114-
)
115-
expect_identical(unclass(c(patch, patch)), expected)
116-
117-
patch1 <- patch |> j_patch_op("add", path, value = value1)
118-
expect_identical(unclass(patch1), expected)
62+
## j_patch_op
11963

64+
if (has_jsonlite) {
65+
value0 <- list(name = "Ginger Nut")
66+
value1 <- list(name = jsonlite::unbox("Ginger Nut"))
67+
path <- "/biscuits/1"
68+
69+
expect_identical(
70+
unclass(j_patch_op("add", path, value = value0)),
71+
c('[',
72+
'{"op":"add","path":"/biscuits/1","value":{"name":["Ginger Nut"]}}',
73+
']')
74+
)
75+
expect_identical(
76+
unclass(j_patch_op("add", path, value = value1)),
77+
c('[',
78+
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
79+
']')
80+
)
81+
expect_identical(
82+
unclass(j_patch_op("add", path, value = value0, auto_unbox = TRUE)),
83+
c('[',
84+
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
85+
']')
86+
)
87+
expect_identical(
88+
unclass(j_patch_op("remove", path)),
89+
c('[', '{"op":"remove","path":"/biscuits/1"}', ']')
90+
)
91+
expect_identical(
92+
unclass(j_patch_op("replace", path, value = value1)),
93+
c('[',
94+
'{"op":"replace","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
95+
']')
96+
)
97+
expect_identical(
98+
unclass(j_patch_op("copy", path, from = path)),
99+
c('[', '{"op":"copy","path":"/biscuits/1","from":"/biscuits/1"}', ']')
100+
)
101+
expect_identical(
102+
unclass(j_patch_op("move", path, from = path)),
103+
c('[', '{"op":"move","path":"/biscuits/1","from":"/biscuits/1"}', ']')
104+
)
105+
expect_identical(
106+
unclass(j_patch_op("test", path, value = value1)),
107+
c('[',
108+
'{"op":"test","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
109+
']')
110+
)
111+
112+
## concatenation and piping
113+
patch <- j_patch_op("add", path, value = value1)
114+
expected <- c(
115+
'[',
116+
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
117+
',',
118+
'{"op":"add","path":"/biscuits/1","value":{"name":"Ginger Nut"}}',
119+
']'
120+
)
121+
expect_identical(unclass(c(patch, patch)), expected)
122+
123+
patch1 <- patch |> j_patch_op("add", path, value = value1)
124+
expect_identical(unclass(patch1), expected)
125+
}
120126
expect_error(j_patch_op())
121127
expect_error(j_patch_op("add")) # no 'path'
122128
expect_error(j_patch_op("add", path)) # no 'value'
@@ -126,5 +132,7 @@ expect_error(j_patch_op("copy", path)) # no 'from'
126132
expect_error(j_patch_op("move", path)) # no 'from'
127133
expect_error(j_patch_op("test", path)) # no 'value'
128134

129-
patch <- j_patch_op("remove", "/biscuits")
130-
expect_identical(j_patch_apply(json, patch), "{}")
135+
if (has_jsonlite) {
136+
patch <- j_patch_op("remove", "/biscuits")
137+
expect_identical(j_patch_apply(json, patch), "{}")
138+
}

inst/tinytest/test_rquerypivot.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ ndjson_file <- system.file(package = "rjsoncons", "extdata", "example.ndjson")
2525

2626
## j_query
2727

28-
expect_identical(j_query(""), '[""]') # JSONpointer
28+
if (has_jsonlite)
29+
expect_identical(j_query(""), '[""]') # JSONpointer
2930
expect_identical(j_query('""'), '')
3031
expect_identical(j_query('[]'), '[]')
3132
expect_identical(j_query('{}'), '{}')

0 commit comments

Comments
 (0)