Skip to content

Commit c9196f8

Browse files
committed
Preserve the collapse parameter in the remaining recursive callsites without it
Two of the three callsites affected calls to toJSON() with the ANY signature, but the most obvious defect was when calling toJSON() on a list(). The test tries to serialize what could be thought of as a nested hash, and fails if run on the code before this commit.
1 parent 53d6d31 commit c9196f8

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

R/json.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ setMethod("toJSON", "ANY",
7171
if(isS4(x)) {
7272
paste("{", paste(dQuote(slotNames(x)), sapply(slotNames(x),
7373
function(id)
74-
toJSON(slot(x, id), ..., .level = .level + 1L,
74+
toJSON(slot(x, id), ..., .level = .level + 1L, collapse = collapse,
7575
.na = .na, .escapeEscapes = .escapeEscapes, asIs = asIs,
7676
.inf = .inf)),
7777
sep = ": ", collapse = ","),
7878
"}", collapse = collapse)
7979
} else {
8080
#cat(class(x), "\n")
8181
if(is.language(x)) {
82-
return(toJSON(as.list(x), asIs = asIs, .inf = .inf, .na = .na))
82+
return(toJSON(as.list(x), asIs = asIs, .inf = .inf, .na = .na, collapse = collapse))
8383
stop("No method for converting ", class(x), " to JSON")
8484
}
8585

@@ -271,7 +271,7 @@ setMethod("toJSON", "list",
271271
return(if(is.null(names(x))) "[]" else "{}")
272272
}
273273

274-
els = lapply(x, toJSON, ..., .level = .level + 1L, .na = .na, .escapeEscapes = .escapeEscapes, asIs = asIs, .inf = .inf)
274+
els = lapply(x, toJSON, ..., .level = .level + 1L, .na = .na, .escapeEscapes = .escapeEscapes, asIs = asIs, .inf = .inf, collapse = collapse)
275275

276276
if(all(sapply(els, is.name)))
277277
names(els) = NULL

tests/collapse.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library(RJSONIO)
2+
3+
json = toJSON(list( foo = c( one = 1, two = 2 ), bar = c( three = 3, four = 4 )), collapse = "")
4+
stopifnot( !grep("\n", json) )

0 commit comments

Comments
 (0)