Using The HAL Vocab With JSON-LD
I was wondering, is there a way to use the HAL concepts with JSON-LD?
I have the current jsonld document:
{
"@context": {
"hal": "http://stateless.co/hal#",
"schema": "http://schema.org",
"_links": {
"@id": "hal:link",
"@container": "@index"
}
},
"@type": ["schema:Person", "hal:Resource"],
"name": "Jon Snow",
"_links": {
"self": {
"href": "/users/123"
}
}
}
but I am not sure how to define that the href
has a @type
of @id
, and so on...
Is there a way to define a HAL vocab based on RDF(S) and import it somehow in the @context of my jsonld documents, or should I do something else?
(I am trying to describe hyperlinks with various properties, like link relation, HTTP method, accepted media type, language, IRI template, input fields, etc... so the @id
type is not enough for me to describe links.)
Answer
As Tomasz already suggested, you should really consider using Hydra as it does more or less what you want. The example you've included in your questions, would look somewhat like this using Hydra and JSON-LD:
{
"@context": {
"schema": "http://schema.org",
"ex": "http://example.com/myvocab#"
},
"@id": "/users/123",
"@type": [ "schema:Person", "hydra:Resource" ],
"name": "Jon Snow",
"ex:link": { "@id": "/another-resource" }
}
As there's no need for a "self" link (@id
already specifies that explicitly), I've added another link, ex:link
. Its link relation is consequently http://example.com/myvocab#link
and its "href" is /another-resource
. If you need to describe that link/property in more details, you can do so by creating a document which defines it in exactly the same manner as other things are described (as Tomasz also already explained):
{
"@context": {
"ex": "http://example.com/myvocab#",
"hydra": "http://www.w3.org/ns/hydra#"
},
"@id": "ex:link",
"@type": "hydra:Link",
"hydra:title": "My new link relation",
"hydra:supportedOperation": [
{
"@type": "hydra:Operation",
"hydra:method": "POST",
"hydra:expects": ....
}
]
}
Regarding your comment
Btw. I am more or less familiar with the Hydra vocab, but I don't like the idea to map the resources to real classes and objects on a server side language and automatically transform the operation parameters into those objects. Maybe it is possible to use the Hydra vocab in another way, but I don't have the time to experiment with that.
Hydra is really just a vocabulary. It is up to you to decide how to use it. I think you are talking about the HydraBundle above. That's just one way to use it. It is just a proof of concept to show that it is easily possible. So please don't get confused by that.
I would like to invite you to join the Hydra W3C Community Group. We can then discuss this in more detail on our mailing list.
Disclaimer: I'm the creator of Hydra and the chair of the Hydra W3C Community Group.
Related Questions
- → OctoberCMS Rain User plugin not working or redirecting
- → Good solution to work with rest-api like SPA with redux?
- → Transformer usage on laravel/dingo API
- → Ember.js JSON API confusion
- → Express - better pattern for passing data between middleware functions
- → Checking passed object existence in Laravel delayed queue job
- → Restrict .htm Pages and Partials in OctoberCMS with Nginx
- → Nested array validation laravel
- → Uploading a file and sending it to the backend with React and Node
- → Javascript onClick not firing when assigned inside of array.map
- → How to replace the 3rd product with an Image in product list for Perstashop?
- → How should I implement canonical urls in my prestashop theme?
- → Best way to do front-end public and restricted area with October CMS?