Skip to content

Commit

Permalink
Handle possibly invalid JSON (fixes #41)
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinleinweber committed Mar 20, 2018
1 parent 949a675 commit cae151d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions R/retrieve_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,17 @@ retrieve_data <- function(searchTerm,
#'
#' @return A serialised JSON string.
download <- function(URL, userpwd = paste(get_credentials(), collapse = ":")) {
gsub(
pattern = "[[:space:]]+",
replacement = " ",
perl = TRUE,
# Prevent "lexical error: invalid character inside string."
# https://github.com/jeroen/jsonlite/issues/47
RCurl::getURL(
URL,
userpwd = userpwd,
httpauth = 1L
)
)
}

Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-retrieve_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ test_that("extracting a single field from a taxon-wide search works", {
)
)))
})

test_that("normalising invalid JSON newlines works", {
is.list(retrieve_data(searchTerm = "Bacillus cytotoxicus",
force_taxon_download = TRUE))
# https://bacdive.dsmz.de/api/bacdive/bacdive_id/1323/?format=json
# contains "sample_type": "Vegetable puree,severe food poisoning\noutbreak in France"

is.list(retrieve_data(searchTerm = "Bacillus halotolerans",
force_taxon_download = TRUE))
# https://bacdive.dsmz.de/api/bacdive/bacdive_id/1847/?format=json
# contains "medium_composition": "Name: ISP 2 / Yeast Malt Agar (5265); 5265\r\nComposition
})

0 comments on commit cae151d

Please sign in to comment.