An implementation of the JSON-LD standard for Elixir and RDF.ex.
- fully conforming JSON-LD API processor
- JSON-LD reader/writer for RDF.ex
- tests of the JSON-LD test suite (see here for a detailed status report)
- remote contexts
- JSON-LD Framing
- JSON-LD 1.1 support
The JSON-LD.ex Hex package can be installed as usual, by adding json_ld
to your list of dependencies in mix.exs
:
def deps do
[{:json_ld, "~> 0.3"}]
end
"""
{
"@context":
{
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
"""
|> Jason.decode!
|> JSON.LD.expand
produces
[%{"http://xmlns.com/foaf/0.1/homepage" => [%{"@id" => "http://manu.sporny.org/"}],
"http://xmlns.com/foaf/0.1/name" => [%{"@value" => "Manu Sporny"}]}]
context = Jason.decode! """
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
}
}
"""
"""
[
{
"http://xmlns.com/foaf/0.1/name": [ "Manu Sporny" ],
"http://xmlns.com/foaf/0.1/homepage": [
{
"@id": "http://manu.sporny.org/"
}
]
}
]
"""
|> Jason.decode!
|> JSON.LD.compact(context)
produces
%{"@context" => %{
"homepage" => %{
"@id" => "http://xmlns.com/foaf/0.1/homepage",
"@type" => "@id"},
"name" => "http://xmlns.com/foaf/0.1/name"
},
"homepage" => "http://manu.sporny.org/",
"name" => "Manu Sporny"}
JSON-LD.ex can be used to serialize or deserialize RDF graphs by using it as a RDF.ex reader and writer.
dataset = JSON.LD.read_file!("file.jsonld")
JSON.LD.write_file!(dataset, "file.jsonld")
Pretty printing is possible on all writer functions with all of the formatter options of Jason, the underlying JSON encoder, to which the given options are passed through.
JSON.LD.write_file!(dataset, "file.jsonld", pretty: true)
JSON.LD.write_string(dataset, "file.jsonld", pretty: [indent: "\t"])
see CONTRIBUTING for details.
(c) 2017 Marcel Otto. MIT Licensed, see LICENSE for details.