Exporting compacted JSON-LD from dotNetRDF

947 views Asked by At

I am using dotNetRDF for RDF in .NET but as far as I know there is no JSON-LD Serialization for it.

Especially the compacted version of JSON-LD has a big benefit for small overhead and readability in comparison to RDF/XML, TriX, ...

So as there is no direct way to export JSON-LD from dotNetRDF I am thinking of a converter that can convert formats that dotNetRDF understands to JSON-LD. Formats like RDF/XML, RDF/JSON (note: it is not the same as JSON-LD), TriX, N-Quads, ...

Which converter do you recommend? Which supports the compacted version? And it should be able to convert from JSON-LD to other formats as well. Maybe there is a .NET library for that?

1

There are 1 answers

0
el-lugy On

My solution using the library json-ld.net for converting dotNetRDF's NQuads output to 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();