我使用库 json-ld.net 将 dotNetRDF 的 NQuads 输出转换为 JSON-LD 的解决方案:
var tripleStore = ...
var nQuads = VDS.RDF.Writing.StringWriter.Write(tripleStore, new NQuadsWriter());
var contextMap = JObject.Parse(@"{
'@context' : {
'model': 'http://localhost/model/',
'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'xsd': 'http://www.w3.org/2001/XMLSchema#',
'model:age': {
'@type': 'xsd:int'
}
}
}");
var parser = new NQuadRDFParser();
var rdfDataset = parser.Parse(nQuads);
var jArray = new JsonLdApi().FromRDF(rdfDataset);
var jObjectCompacted = JsonLdProcessor.Compact(jArray, new Context(contextMap), new JsonLdOptions());
var jsonLd = jObjectCompacted.ToString();