JSON-LD + Hydra Link Detection

I was thinking about how to use JSON-LD to control the application according to the HATEOAS principle.

For example, I could have a simple entrypoint object that defines a link:

{ "@context": { "users": { "@id": "http://example.com/onto#users", "@type": "@id" } }, "@id": "http://example.com/api", "users": "http://example.com/users" } 

And the #users predicate will be defined as Link using Hydra:

 { "@context": "http://www.w3.org/ns/hydra/context.jsonld", "@id": "http://example.com/onto#users", "@type": "Link" } 

Everything is still: the application retrieves the resource, then the onto#users resource will be dereferenced to detect semantics.

The question is how the developer should find the URI of the users property from the JSON-LD document. Of course, this is clearly defined in @context in my example, but this URI can be declared as QName:

 "@context": { "onto": "http://example.com/onto#", "users": { "@id": "onto:users", "@type": "@id" } } 

or an external context or multiple / nested contexts may be used.

Is there any functionality in the Javron JSON-LD library that will return absolute URIs of any property? Or is there an easy way to find it? A way that will work no matter how @context structured? Something like

 var jsonLd = /* some odc */ var usersUri = jsonLd.uriOf('users'); expect(usersUri).toBe('http://example.com/onto#users'); 

In other words, I think I'm looking for a single API to read @context .

+7
angularjs rest hateoas
source share
1 answer

Here you can do what you request using the JSON-LD JavaScript library (jsonld.js):

 var jsonld = require('jsonld'); var data = { "@context": { "onto": "http://example.com/onto#", "users": {"@id": "onto:users", "@type": "@id"} }, "users": "http://example.com/users" }; jsonld.processContext(null, [null, data['@context']], function(err, ctx) { if(err) { console.log('error', err); return; } var value = jsonld.getContextValue(ctx, 'users', '@id'); console.log('users', value); }); 

However, it is doubtful whether this is a good idea. It looks like you just want to use jsonld.expand (), which turns all properties into full URLs. Or you can use jsonld.compact () to convert any JSON-LD entry using a context that is well known to your application.

+4
source share

All Articles