Search in sources :

Example 11 with JsonLDWriteContext

use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.

the class Ex_WriteJsonLD method controllingAtContext.

/**
 * Shows how to customize the "@context" in "compacted" and "flattened" format.
 *
 * To set it to the URI of a vocab, {@link #settingAtContextToURI()}
 */
void controllingAtContext() {
    Model m = aSimpleModel();
    m.setNsPrefix("ex", "http://www.ex.com/");
    m.setNsPrefix("sh", "http://schema.org/");
    DatasetGraph g = DatasetFactory.wrap(m).asDatasetGraph();
    JsonLDWriteContext ctx = new JsonLDWriteContext();
    // When no value for the "@context" is provided,
    // Jena computes one from the defined prefixes, and from the RDF content.
    // This default is probably good enough in most of the cases,
    // but you may want to customize it.
    // Or, if it is always the same one, you may consider that computing it again and again
    // (each time that you output data), is a waste of time.
    // (the computing of the "@context" implies to loop through all the triples).
    // You may therefore want to compute it once for all, and to pass it to the output process
    // To pass a given "@context" to the writing process,
    // you pass the corresponding value as a JSON string
    // using the setJsonLDContext(String) method.
    // (Alternatively, you can directly pass the object expected by the JSON-LD API)
    // For instance, we can pass a simple context
    // that uses jsonld "@vocab" keyord to set the "default vocabulary"
    // to schema.org.
    String atContextAsJson = "{\"@vocab\":\"http://schema.org/\"}";
    ctx.setJsonLDContext(atContextAsJson);
    System.out.println("\n--- COMPACT using a Context that defines @vocab ---");
    write(g, RDFFormat.JSONLD_COMPACT_PRETTY, ctx);
}
Also used : JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Example 12 with JsonLDWriteContext

use of org.apache.jena.riot.JsonLDWriteContext in project jena by apache.

the class Ex_WriteJsonLD method settingAtContextToURI.

/**
 * Shows how to set "@context" to a URI.
 */
void settingAtContextToURI() {
    // One thing you'll probably want to do is to set the "@context" to the URL of a file
    // containing the actual JSON-LD context.
    // Let's take one Model that only uses schema.org terms,
    // and let's try to set the "@Context" to the URL of schema.org
    // "@context" : "http://schema.org/"
    Model m = aModelThatOnlyUsesSchemaDotOrg();
    DatasetGraph g = DatasetFactory.wrap(m).asDatasetGraph();
    JsonLDWriteContext ctx = new JsonLDWriteContext();
    // 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);
    // 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 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);
// Final note: BEWARE when replacing the context:
// if you let some things undefined, the output will be json, not jsonld
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonLDWriteContext(org.apache.jena.riot.JsonLDWriteContext) Model(org.apache.jena.rdf.model.Model) DocumentLoader(com.github.jsonldjava.core.DocumentLoader) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph)

Aggregations

JsonLDWriteContext (org.apache.jena.riot.JsonLDWriteContext)12 Model (org.apache.jena.rdf.model.Model)8 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)8 JsonLdOptions (com.github.jsonldjava.core.JsonLdOptions)4 JsonString (org.apache.jena.atlas.json.JsonString)4 Test (org.junit.Test)4 Resource (org.apache.jena.rdf.model.Resource)2 DocumentLoader (com.github.jsonldjava.core.DocumentLoader)1 JsonObject (org.apache.jena.atlas.json.JsonObject)1