You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2020/09/25 15:47:08 UTC
[jena] branch master updated: Bumped JsonLD Java dependency fixing
test
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jena.git
The following commit(s) were added to refs/heads/master by this push:
new 45c30df Bumped JsonLD Java dependency fixing test
new 248f585 Merge pull request #802 from umbreak/docs_and_test_bump
45c30df is described below
commit 45c30df1e98d928bea57b30106682ea3dd25d223
Author: Didac Montero <di...@epfl.ch>
AuthorDate: Thu Sep 24 08:35:55 2020 +0200
Bumped JsonLD Java dependency fixing test
---
.../arq/examples/riot/Ex_WriteJsonLD.java | 46 ++++++++--------------
.../org/apache/jena/riot/JsonLDWriteContext.java | 4 +-
.../org/apache/jena/riot/TestJsonLDReader.java | 37 +++++++----------
.../apache/jena/riot/writer/TestJsonLDWriter.java | 37 +++++------------
pom.xml | 2 +-
5 files changed, 43 insertions(+), 83 deletions(-)
diff --git a/jena-arq/src-examples/arq/examples/riot/Ex_WriteJsonLD.java b/jena-arq/src-examples/arq/examples/riot/Ex_WriteJsonLD.java
index d66f592..309eddc 100644
--- a/jena-arq/src-examples/arq/examples/riot/Ex_WriteJsonLD.java
+++ b/jena-arq/src-examples/arq/examples/riot/Ex_WriteJsonLD.java
@@ -200,42 +200,30 @@ public class Ex_WriteJsonLD
DatasetGraph g = DatasetFactory.wrap(m).asDatasetGraph();
JsonLDWriteContext ctx = new JsonLDWriteContext();
- // The following should work for Uris returning JSON-LD,
- // but unfortunately it doesn't for schema.org due to the following bug: https://github.com/jsonld-java/jsonld-java/issues/289:
+ // The following works with Uris returning JSON-LD or Uris returning an Alternate document location that is JSON-LD
+ // https://www.w3.org/TR/json-ld11/#alternate-document-location
+ // NOTE: This example will download the "@context" from the passed URL before processing the output, which can be slow.
ctx.setJsonLDContext("\"http://schema.org/\"");
System.out.println("\n--- Setting the context to a URI, WRONG WAY: it's slow, and the output is not JSON-LD. Sorry about that. ---");
write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
- // But don't worry (be happy):
- // - there is a solution
- // - and what we tried is not what we would want to do, anyway.
-
- // The output process needs to have the content of the "@context" at hand
- // in order to compute the output. So, if passing the URL of the vocab,
- // the output process must download the vocab before anything.
- // (that's why the previous attempt was slow)
- // -> that would not be an very efficient way to output your data.
-
- // To achieve the expected result,
- // you have to do 2 things:
-
- // 1)
- // you have to pass the dereferenced content of http://schema.org/
- // - or the relevant subset of it (as we only use very few terms).
- // Here it is:
- String atContextAsJson = "{\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}";
- ctx.setJsonLDContext(atContextAsJson);
- // Alternatively, we could just pass "null":
- // ctx.setJsonLDContext(null);
- // and let jena compute the context (as the model only uses schema.org vocab)
-
- // 2)
- // and then you pass the schema.org url using:
+ // Alternatively, if we know beforehand the resolved context, we can use the DocumentLoader as follows (much more performant):
+ DocumentLoader dl = new DocumentLoader();
+ String resolvedContext = "\"@context\": {\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}"
+ dl.addInjectedDoc("http://schema.org", resolvedContext);
+ JsonLdOptions options = new JsonLdOptions();
+ options.setDocumentLoader(dl);
+ ctx.setOptions(options);
+
+ // Alternatively, we could just pass "null" as context and let jena compute it (as the model only uses schema.org vocab)
+ // After that, we can substitute the output "@context" from Jena by whatever we want, in this case the URL http://schema.org/
+ ctx.setJsonLDContext(null);
ctx.setJsonLDContextSubstitution("\"http://schema.org/\"");
// To summarize:
- // - ctx.setJsonLDContext allows to define the @context used to compute the output
- // - ctx.setJsonLDContextSubstitution allows to change the value of the "@context" in the output
+ // - ctx.setJsonLDContext allows to define the @context used to produce the output in compacted/frame/flatten algorithms
+ // - ctx.setOptions allows to define the Json-LD options and override the remote context URI resolutions (using DocumentLoader)
+ // - ctx.setJsonLDContextSubstitution allows to override the output value of the "@context" after the compaction/frame/flattening algorithms have already been executed
System.out.println("\n--- COMPACT with @context replaced by schema.org URI ---");
write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/JsonLDWriteContext.java b/jena-arq/src/main/java/org/apache/jena/riot/JsonLDWriteContext.java
index f40cc5d..11b793d 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/JsonLDWriteContext.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/JsonLDWriteContext.java
@@ -60,7 +60,7 @@ public class JsonLDWriteContext extends Context {
* Only useful for "Compact" and "Flattened" JSON-LD outputs, and not required: if not set,
* a value for the "@Context" node is computed, based on the content of the dataset and its prefix mappings.
*
- * @param jsonLdContext the value of the "@context" node (a JSON value). Some remote contexts might not be resolved property.
+ * @param jsonLdContext the value of the "@context" node (a JSON value).
* @see #setJsonLDContextSubstitution(String) for a way to overcome this problem.
*
* @see #setJsonLDContext(Object)
@@ -75,7 +75,7 @@ public class JsonLDWriteContext extends Context {
* Only useful for "Compact" and "Flattened" JSON-LD outputs, and not required: if not set,
* a value for the "@Context" node is computed, based on the content of the dataset and its prefix mappings.
*
- * @param jsonLdContext the context as expected by JSON-LD java API. Some remote contexts might not be resolved property.
+ * @param jsonLdContext the context as expected by JSON-LD java API.
* @see #setJsonLDContextSubstitution(String) for a way to overcome this problem.
*
* @see #setJsonLDContext(String)
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java b/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java
index 80bb6ec..7adc9ff 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/TestJsonLDReader.java
@@ -21,11 +21,9 @@ package org.apache.jena.riot;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.github.jsonldjava.core.DocumentLoader;
import com.github.jsonldjava.core.JsonLdOptions;
-import com.github.jsonldjava.utils.JsonUtils;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
-import org.apache.jena.riot.lang.JsonLDReader;
import org.apache.jena.riot.system.ErrorHandlerFactory;
import org.apache.jena.sparql.util.Context;
import org.apache.jena.vocabulary.RDF;
@@ -55,13 +53,9 @@ public class TestJsonLDReader {
// some jsonld using schema.org's URI as "@context"
String jsonld = someSchemaDorOrgJsonld();
- // a subset of schema.org that can be used as @context for jsonld
- String jsonldContext = "{\"name\":{\"@id\":\"http://schema.org/name\", \"@type\": \"http://www.w3.org/2001/XMLSchema#other\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}";
-
// pass the jsonldContext to the read using a jena Context
- Context jenaCtx = new Context();
- Object jsonldContextAsMap = JsonUtils.fromInputStream(new ByteArrayInputStream(jsonldContext.getBytes(StandardCharsets.UTF_8)));
- jenaCtx.set(JsonLDReader.JSONLD_CONTEXT, jsonldContextAsMap);
+ JsonLDReadContext jenaCtx = new JsonLDReadContext();
+ jenaCtx.setJsonLDContext(schemaOrgResolvedContext());
// read the jsonld, replacing its "@context"
Dataset ds = jsonld2dataset(jsonld, jenaCtx);
@@ -70,26 +64,18 @@ public class TestJsonLDReader {
assertJohnDoeIsOK(ds.getDefaultModel());
}
- /**
- * Not really useful, but one can replace the @context by a URI: in this case, this URI is used when expanding the json
- * (letting JSON-LD java API taking care of downloading the context.
- */
@Test
- public final void overrideJsonLdOptionsAndContextUri() throws JsonGenerationException, IOException {
- // some jsonld using a (fake) pseudo.schema.org's URI as "@context"
- String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"Person\",\"name\":\"John Doe\",\"@context\":\"http://pseudo.schema.org/\"}";
-
- // a subset of schema.org that can be used as @context for jsonld
- String jsonldContext = "\"http://schema.org\"";
+ public final void overrideJsonLdOptions() throws JsonGenerationException, IOException {
+ // some jsonld using a (fake) http://pseudo.schema.org's URI as "@context"
+ String jsonld = "{\"@id\":\"_:b0\",\"@type\":\"Person\",\"name\":\"John Doe\",\"@context\":\"http://pseudo.schema.org\"}";
JsonLdOptions options = new JsonLdOptions();
DocumentLoader dl = new DocumentLoader();
- dl.addInjectedDoc("http://schema.org", String.format("{%s}", schemaOrgContext()));
+ dl.addInjectedDoc("http://pseudo.schema.org", String.format("{ \"@context\": %s }", schemaOrgResolvedContext()));
options.setDocumentLoader(dl);
// pass the jsonldContext and JsonLdOptions to the read using a jena Context
JsonLDReadContext jenaCtx = new JsonLDReadContext();
- jenaCtx.setJsonLDContext(jsonldContext);
jenaCtx.setOptions(options);
// read the jsonld, replacing its "@context"
@@ -131,7 +117,13 @@ public class TestJsonLDReader {
}
private String schemaOrgContext() {
- return "\"@context\": {\"@vocab\": \"http://schema.org/\", \"name\": {\"@type\": \"http://www.w3.org/2001/XMLSchema#other\"} }";
+ return "\"@context\": \"http://schema.org/\"";
+ }
+
+ // a subset of schema.org that can be used as @context for jsonld
+ private String schemaOrgResolvedContext() {
+ return "{\"name\":{\"@id\":\"http://schema.org/name\"},\"Person\": {\"@id\": \"http://schema.org/Person\"}}";
+
}
/**
@@ -139,8 +131,7 @@ public class TestJsonLDReader {
*/
private void assertJohnDoeIsOK(Model m) {
assertTrue(m.contains(null, RDF.type, m.createResource("http://schema.org/Person")));
- assertTrue(m.contains(null, m.createProperty("http://schema.org/name"),
- m.createTypedLiteral("John Doe", "http://www.w3.org/2001/XMLSchema#other")));
+ assertTrue(m.contains(null, m.createProperty("http://schema.org/name"), "John Doe"));
}
diff --git a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java b/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
index 966e099..208b1bf 100644
--- a/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
+++ b/jena-arq/src/test/java/org/apache/jena/riot/writer/TestJsonLDWriter.java
@@ -17,6 +17,7 @@
*/
package org.apache.jena.riot.writer;
+import static com.github.jsonldjava.core.JsonLdOptions.JSON_LD_1_1;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -31,6 +32,7 @@ import java.util.Map.Entry;
import com.fasterxml.jackson.core.JsonParseException;
import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.core.JsonLdOptions;
+import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;
import org.apache.jena.atlas.json.JsonObject;
@@ -250,9 +252,8 @@ public class TestJsonLDWriter {
/**
* Checks that one can pass a context defined by its URI
*
- * -- well NO, this doesn't work in a test setup.
*/
- //@Test
+ @Test
public final void testContextByUri() {
Model m = ModelFactory.createDefaultModel();
String ns = "http://schema.org/";
@@ -263,35 +264,15 @@ public class TestJsonLDWriter {
// we can pass an uri in the context, as a quoted string (it is a JSON string)
JsonLDWriteContext jenaContext = new JsonLDWriteContext();
- try {
- jenaContext.set(JsonLDWriter.JSONLD_CONTEXT, "{\"@context\" : \"http://schema.org/\"}");
- String jsonld = toString(m, RDFFormat.JSONLD, jenaContext);
- // check it parses ok
- Model m2 = parse(jsonld);
-
- // assertTrue(m2.isIsomorphicWith(m)); // It should be the case, but no.
-
- } catch (Throwable e) {
- // maybe test run in a setting without external connectivity - not a real problem
- String mess = e.getMessage();
- if ((mess != null) && (mess.contains("loading remote context failed"))) {
- LoggerFactory.getLogger(getClass()).info(mess);
- e.printStackTrace();
- } else {
- throw e;
- }
- }
-
- // But anyway, that's not what we want to do:
- // there's no point in passing the uri of a context to have it dereferenced by jsonld-java
- // (this is for a situation where one would want to parse a jsonld file containing a context defined by a uri)
- // What we want is to pass a context to jsonld-java (in order for json-ld java to produce the correct jsonld output)
- // and then we want to replace the @context in the output by "@context":"ourUri"
-
- // How would we do that? see testSubstitutingContext()
+ jenaContext.setJsonLDContext("{\"@context\" : \"http://schema.org/\"}");
+ String jsonld = toString(m, RDFFormat.JSONLD, jenaContext);
+ // check it parses ok
+ Model m2 = parse(jsonld);
+ assertTrue(m2.isIsomorphicWith(m));
}
+
/**
* Test using a context to compute the output, and replacing the @context with a given value
*/
diff --git a/pom.xml b/pom.xml
index 2f28792..c92e114 100644
--- a/pom.xml
+++ b/pom.xml
@@ -70,7 +70,7 @@
POM for the correct dependency versions
and use that or later.
-->
- <ver.jsonldjava>0.13.1</ver.jsonldjava>
+ <ver.jsonldjava>0.13.2</ver.jsonldjava>
<ver.jackson>2.10.1</ver.jackson>
<ver.jackson-databind>${ver.jackson}</ver.jackson-databind>