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.)
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.
I think you could be interested in Hydra. Have you tried that?
It's a vocabulary for describing Hypermedia links and operations. Here's how it could work for a simple parent link
{
"@context": {
"schema": "http://schema.org",
"parent": {
"@id": "/vocab#parent"
"@type": "@id"
}
},
"@id": "/users/123",
"@type": "schema:Person",
"name": "Jon Snow",
"parent": "/users/Ned_Stark"
}
Note that you don't need to include any data outside your domain in the representation. Instead you describe what the parent predicate means
{
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@id": "/vocab#parent",
"@type": "hydra:Link"
}
You can also describe operations (HTTP methods, ranges, domains, etc.) and properties for classes. Also the operations can be included directly in the representation or attached to the classes and proeprties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With