Hi Elie,
(I wrote the comment in the line you point to. And then forgot it).
The fix is probably more than one line (it must take care of the other parts of
the definition of the “context" passed to the JSON-LD java API, and of the
behavior of that API when it includes an @vocab)
I’ll try to see what can be done. In the meantime, you maybe can use a new
possibility in 3.2.0: you can access the object that jena creates and pass to
the JSON-LD java API. You can modify it before sending it to the JSON-LD java
API. Please find below a quick example.
HTH
fps
/* Created on 20 févr. 2017 */
package com.renault.sicg.rasse.commons;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.system.PrefixMap;
import org.apache.jena.riot.system.RiotLib;
import org.apache.jena.riot.writer.JsonLDWriter;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.util.Context;
import org.apache.jena.sparql.vocabulary.FOAF;
import org.apache.jena.vocabulary.RDF;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.github.jsonldjava.core.JsonLdError;
import com.github.jsonldjava.utils.JsonUtils;
public class AtVocab {
@Test public final void test() throws JsonParseException, JsonLdError,
IOException {
Model m = ModelFactory.createDefaultModel();
String ns = "http://schema.org/";
Resource person = m.createResource(ns + "Person");
Resource s = m.createResource();
m.add(s, m.createProperty(ns + "name"), "Jane Doe");
m.add(s, m.createProperty(ns + "url"), "http://www.janedoe.com");
m.add(s, m.createProperty(ns + "jobTitle"), "Professor");
m.add(s, FOAF.nick, "jd");
m.add(s, RDF.type, person);
m.setNsPrefix("", ns);
DatasetGraph g = DatasetFactory.create(m).asDatasetGraph();
PrefixMap pm = RiotLib.prefixMap(g);
String base = null;
Context jenaContext = null;
// the JSON-LD API object. It's a map
Map map = (Map)
JsonLDWriter.toJsonLDJavaAPI((RDFFormat.JSONLDVariant)RDFFormat.JSONLD.getVariant()
, g, pm, base, jenaContext);
// get the @context:
Map<String, Object> ctx = (Map<String, Object>) map.get("@context");
// add the "@vocab" key, to ctx
// and remove from it declaration of props in ns
// remove from ctx declaration of props in ns
List<String> remove = new ArrayList<>();
for (Entry<String, Object> e : ctx.entrySet()) {
// is it the declaration of a prop in ns?
Object o = e.getValue();
if (o instanceof Map) {
o = ((Map) o).get("@id");
}
if ((o != null) && (o instanceof String)) {
if (((String) o).equals(ns + e.getKey())) {
remove.add(e.getKey());
}
}
}
for (String key : remove) {
ctx.remove(key);
}
// add the "@vocab" key to ctx
ctx.put("@vocab", "http://schema.org/");
JsonUtils.writePrettyPrint(new PrintWriter(System.out), map) ;
}
}