Journeyman1234
Journeyman1234

Reputation: 557

How to parse JSON-LD feed using dotNetRDF in C#

I'm trying to consume a json-ld formatted endpoint in a dotnet app.

I've not come across this format before but most of the examples are for JavaScript.

I've tried a couple of libraries and simply failing because there is so little reference.

I've loaded the contents of the endpoint into memory, now I want to see how best to traverse the nodes, but I can't take the contents and do anything with them.

The simplest example OUGHT to look something like:

JsonLdParser parser = new JsonLdParser();
parser.Load(contentsfromuri)

However, the above requires you to have an IRdfReader declared, which cannot be instantiated as its an abstract class.

Upvotes: 1

Views: 1871

Answers (1)

Kal
Kal

Reputation: 1932

You'll find the dotNetRDF documentation all at https://github.com/dotnetrdf/dotnetrdf/wiki. There are some examples of parsing RDF data from various sources at https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Reading-RDF. In RDF there are syntaxes that only ever serialize a single graph and syntaxes that can serialize multiple graphs - JSON-LD is one of the latter, so you need to also read the section on Store Readers.

The following examples all show loading the data into an in-memory store.

If your content source is "well-behaved" (sends back the right sort of Content-Type headers, or has the expected file name suffix if it is a local file), then loading the data can be as simple as creating a new in-memory graph and calling its Load method:

var store = new TripleStore();

# This is a convenience wrapper that simply invokes UriLoader.Load()
store.LoadFromUri(contentSourceUri)

NOTE: This uses an extension method (as described at https://github.com/dotnetrdf/dotnetrdf/wiki/UserGuide-Extension-Methods) which is just a convenience wrapper around:

# Create the store
var store  = new TripleStore();

# UriLoader will make an HTTP request and parse the response, 
# selecting the parser to be used based on the Content-Type header returned.
UriLoader.Load(store, contentSourceUri);

If you are parsing from a string that you have already retrieved, or if you need to be explicit about the parser instance to use (this may be the case if you want to pass some options to the parser when you create it for example), then you need a slightly more verbose approach:

var store = new TripleStore();

# Create the parser (we can pass in options here if needed)
var parser = new JsonLdParser();

# Wrap the string content in a StringReader and pass the target graph and the reader
parser.Load(store, new StringReader(contentsFromUri));

One final thing to note as you specifically refer to JSON-LD. The parser is a conformant JSON-LD 1.0 parser but it's JSON-LD 1.1 support is based on an earlier draft of the spec. I'm currently working on updating the implementation and hope to have a new release that supports the JSON-LD 1.1 Proposed Recommendation in a few weeks.

Upvotes: 1

Related Questions