据我所知,任何包含类型值的 JSON-LD 上下文都必须作为字符串提供。我编写了一个小型 Java 类,通过合并简单的前缀值和类型值来生成必要的 JSON 字符串。
import static java.util.stream.Collectors.joining;
import static org.apache.jena.rdf.model.ResourceFactory.createResource;
import java.util.Map;
import java.util.TreeMap;
public class JsonLDContext {
private static final String ID = "\"@id\": ";
private static final String TYPE = "\"@type\": ";
private final Map<String, String> cxt = new TreeMap<>();
public JsonLDContext(){}
public JsonLDContext(Map<String, String> prefixes) {
putPrefixes(prefixes);
}
public JsonLDContext putPrefixes(Map<String, String> prefixes) {
prefixes.forEach(this::putPrefix);
return this;
}
public JsonLDContext putPrefix(String key, String value) {
cxt.put(key.isEmpty() ? "@vocab" : key, quote(value));
return this;
}
public JsonLDContext putTypedValue(String id, String type) {
String key = createResource(id).getLocalName();
cxt.put(key, generateJsonTypedValue(id, type));
return this;
}
private static String generateJsonTypedValue(String id, String type) {
return '{' + ID + quote(id) + ", " + TYPE + quote(type) + '}';
}
public String json() {
return cxt.entrySet().stream()
.map(entry -> quote(entry.getKey()) + ": " + entry.getValue())
.collect(joining(", ", "{", "}"));
}
private static String quote(String s) {
return '"' + s + '"';
}
}
上面的类使用如下。
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.riot.JsonLDWriteContext;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.WriterDatasetRIOT;
JsonLDWriteContext cxt = new JsonLDWriteContext();
cxt.setJsonLDContext(
new JsonLDContext(/* map of prefixes */)
.putTypedValue("http://purl.org/dc/terms/modified", "http://www.w3.org/2001/XMLSchema#dateTime")
.json()
);
WriterDatasetRIOT writer = RDFDataMgr.createDatasetWriter(RDFFormat.JSONLD);
writer.write(System.out, DatasetFactory.create().asDatasetGraph(), null, null, cxt);