Skip to content

Commit

Permalink
make jsonld an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Sep 15, 2023
1 parent d7cc27d commit 3516b67
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 16 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rdflib
Title: Tools to Manipulate and Query Semantic Data
Version: 0.2.6
Version: 0.2.7
Authors@R: c(person("Carl", "Boettiger",
email = "cboettig@gmail.com",
role = c("aut", "cre", "cph"),
Expand Down Expand Up @@ -35,7 +35,6 @@ URL: https://github.com/ropensci/rdflib
BugReports: https://github.com/ropensci/rdflib/issues
Imports:
redland,
jsonld,
methods,
utils,
stringi,
Expand All @@ -60,6 +59,7 @@ Suggests:
jsonlite,
repurrrsive,
nycflights13,
spelling
spelling,
jsonld
VignetteBuilder: knitr
Language: en-US
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ importClassesFrom(redland,Statement)
importClassesFrom(redland,Storage)
importClassesFrom(redland,World)
importFrom(dplyr,left_join)
importFrom(jsonld,jsonld_compact)
importFrom(jsonld,jsonld_to_rdf)
importFrom(methods,new)
importFrom(readr,read_csv)
importFrom(redland,is.null.externalptr)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rdflib 0.2.7

make jsonld an optional dependency

# rdflib 0.2.6

* bugfix vroom warning
Expand Down
7 changes: 6 additions & 1 deletion R/rdf_parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#' and model objects
#' @importClassesFrom redland World Storage Model Parser
#' @importMethodsFrom redland parseFileIntoModel freeParser
#' @importFrom jsonld jsonld_to_rdf
#' @export
#'
#' @examples
Expand Down Expand Up @@ -50,6 +49,12 @@ rdf_parse <- function(doc,
## We use tmp to avoid altering input doc, since parsing a local file should
## be a read-only task!
if(format == "jsonld"){

has_jsonld <- requireNamespace("jsonld", quietly = TRUE)
if (!has_jsonld) {
stop("please install the jsonld package to use this functionality.")
}

x <- jsonld::jsonld_to_rdf(doc,
options =
list(base = getOption("rdf_base_uri", "localhost://"),
Expand Down
10 changes: 8 additions & 2 deletions R/rdf_serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#' @importFrom methods new
#' @importClassesFrom redland Serializer
#' @importMethodsFrom redland setNameSpace serializeToFile freeSerializer
#' @importFrom jsonld jsonld_compact
#'
#' @export
#' @examples
Expand Down Expand Up @@ -86,13 +85,20 @@ rdf_serialize <- function(rdf,
}

if(jsonld_output){

has_jsonld <- requireNamespace("jsonld", quietly = TRUE)
if (!has_jsonld) {
stop("please install the jsonld package to use this functionality.")
}


txt <- paste(readLines(doc), collapse = "\n")
if(length(txt) > 0){ ## don't attempt to write empty file into json
json <- jsonld::jsonld_from_rdf(txt,
options = list(
base = base,
format = "application/nquads"))
compact_json <- jsonld_compact(json, "{}")
compact_json <- jsonld::jsonld_compact(json, "{}")
writeLines(compact_json, doc)
}
}
Expand Down
9 changes: 1 addition & 8 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ URI
URIs
URNs
UUIDs
VM
Wickham
XMLSchema
Zenodo
Expand All @@ -34,22 +33,17 @@ bugfix
charset
consise
csv
dajobe
datatypes
dev
doi
dsn
getNextResult
github
homebrew
https
ifying
javascript
json
jsonld
ld
libdb
librdf
lossy
md
mysql
Expand All @@ -69,7 +63,6 @@ readr
rectangling
redland
rofooter
ropensci
serializeToFile
serializer
serializers
Expand All @@ -83,5 +76,5 @@ triplestores
ubiquitious
un
uri
vroom
yml
zenodo
13 changes: 13 additions & 0 deletions tests/testthat/test-parse-serialize.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ testthat::test_that("we can add a namespace on serializing", {


testthat::test_that("we can parse and serialize json-ld", {

skip_if_not_installed("jsonld")
rdf <- rdf_parse(doc)
rdf_serialize(rdf, "out.json")
roundtrip <- rdf_parse("out.json")
Expand Down Expand Up @@ -130,6 +132,10 @@ testthat::test_that("we can serialize turtle with a baseUri", {
## JSON-LD tests with default base uri

testthat::test_that("@id is not a URI, we should get localhost", {

skip_if_not_installed("jsonld")


ex <- '{
"@context": "http://schema.org/",
"@id": "person_id",
Expand All @@ -142,6 +148,9 @@ testthat::test_that("we can serialize turtle with a baseUri", {
})

testthat::test_that("@id is a URI, we should not get localhost", {

skip_if_not_installed("jsonld")

ex <- '{
"@context": "http://schema.org/",
"@id": "uri:person_id",
Expand All @@ -153,6 +162,10 @@ testthat::test_that("we can serialize turtle with a baseUri", {
})

testthat::test_that("we can alter the base URI", {

skip_if_not_installed("jsonld")


ex <- '{
"@id": "person_id",
"schema:name": "Jane Doe"
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-rdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ testthat::test_that("we can concatenate rdfs", {


testthat::test_that("we can add, parse and serialize json-ld", {

skip_if_not_installed("jsonld")

x <- rdf()
x <- rdf_add(x,
subject="http://www.dajobe.org/",
Expand Down

0 comments on commit 3516b67

Please sign in to comment.