2019-07-21 23:58:44 +02:00
<img style="border:0px;" width="64" src="https://json-ld.org/images/json-ld-logo-64.png" alt="JSON-LD-logo-64" align="right"/>
2017-06-25 02:11:10 +02:00
# JSON-LD.ex
2016-12-08 00:20:12 +01:00
2020-10-13 16:44:29 +02:00
[](https://github.com/rdf-elixir/jsonld-ex/actions?query=branch%3Amaster+workflow%3ACI)
2017-06-25 21:55:24 +02:00
[](https://hex.pm/packages/json_ld)
2020-06-01 17:52:01 +02:00
[](https://coveralls.io/github/rdf-elixir/jsonld-ex?branch=master)
2017-08-11 14:34:34 +02:00
2017-06-25 21:55:24 +02:00
2017-06-25 02:11:10 +02:00
An implementation of the [JSON-LD] standard for Elixir and [RDF.ex].
2016-12-08 00:20:12 +01:00
2022-01-26 22:28:35 +01:00
The API documentation can be found [here ](https://hexdocs.pm/json_ld/ ). For a guide and more information about RDF.ex and it's related projects, go to <https://rdf-elixir.dev>.
2016-12-08 00:20:12 +01:00
2017-06-25 02:11:10 +02:00
## Features
- fully conforming JSON-LD API processor
- JSON-LD reader/writer for [RDF.ex]
2020-06-01 17:52:01 +02:00
- tests of the [JSON-LD test suite][] (see [here ](https://github.com/rdf-elixir/jsonld-ex/wiki/JSON-LD.ex-implementation-report ) for a detailed status report)
2017-06-25 02:11:10 +02:00
## TODO
- [JSON-LD Framing]
- [JSON-LD 1.1] support
2016-12-08 00:20:12 +01:00
2017-06-25 02:11:10 +02:00
## Installation
2017-06-26 19:27:13 +02:00
The [JSON-LD.ex ](https://hex.pm/packages/json_ld ) Hex package can be installed as usual, by adding `json_ld` to your list of dependencies in `mix.exs` :
2017-02-10 15:16:51 +01:00
2017-06-25 02:11:10 +02:00
```elixir
def deps do
2018-09-17 04:14:35 +02:00
[{:json_ld, "~> 0.3"}]
2017-06-25 02:11:10 +02:00
end
2017-02-10 15:16:51 +01:00
```
## Usage
2016-12-08 00:20:12 +01:00
2017-06-25 02:11:10 +02:00
### Expand a document
```elixir
"""
{
"@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/"
}
"""
2018-03-16 00:08:39 +01:00
|> Jason.decode!
2017-06-25 02:11:10 +02:00
|> JSON.LD.expand
```
produces
```elixir
[%{"http://xmlns.com/foaf/0.1/homepage" => [%{"@id " => "http://manu.sporny.org/"}],
"http://xmlns.com/foaf/0.1/name" => [%{"@value " => "Manu Sporny"}]}]
```
### Compact a document
```elixir
2018-03-16 00:08:39 +01:00
context = Jason.decode! """
2017-06-25 02:11:10 +02:00
{
"@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/"
}
]
}
]
"""
2018-03-16 00:08:39 +01:00
|> Jason.decode!
2017-06-25 02:11:10 +02:00
|> JSON.LD.compact(context)
```
produces
```elixir
%{"@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"}
```
## RDF Reader and Writer
JSON-LD.ex can be used to serialize or deserialize RDF graphs by using it as a RDF.ex reader and writer.
```elixir
dataset = JSON.LD.read_file!("file.jsonld")
JSON.LD.write_file!(dataset, "file.jsonld")
```
2022-04-25 21:36:38 +02:00
When a context is provided via the `:context` option (as a map, a `RDF.PropertyMap` or a string with a URL to a remote context), the document gets automatically compacted on serialization.
```elixir
JSON.LD.write_file!(dataset, "file.jsonld", context: %{ex: "https://example.com/"})
JSON.LD.write_file!(dataset, "file.jsonld", context: "https://schema.org/")
```
2017-06-25 02:11:10 +02:00
2018-07-11 00:17:36 +02:00
## Pretty printing
2022-04-25 21:36:38 +02:00
Pretty printing is possible on all writer functions with all the formatter options of [Jason ](https://hexdocs.pm/jason/Jason.Formatter.html#pretty_print/2 ), the underlying JSON encoder, to which the given options are passed through.
2018-07-11 00:17:36 +02:00
```elixir
JSON.LD.write_file!(dataset, "file.jsonld", pretty: true)
JSON.LD.write_string(dataset, "file.jsonld", pretty: [indent: "\t"])
```
2017-06-25 02:11:10 +02:00
## Contributing
see [CONTRIBUTING ](CONTRIBUTING.md ) for details.
2022-04-20 01:47:19 +02:00
## Consulting
If you need help with your Elixir and Linked Data projects, just contact [NinjaConcept ](https://www.ninjaconcept.com/ ) via <contact@ninjaconcept .com>.
2017-06-25 02:11:10 +02:00
## License and Copyright
2022-04-20 01:47:19 +02:00
(c) 2017-present Marcel Otto. MIT Licensed, see [LICENSE ](LICENSE.md ) for details.
2017-06-25 02:11:10 +02:00
[RDF.ex]: https://hex.pm/packages/rdf
[JSON-LD]: http://www.w3.org/TR/json-ld/ "JSON-LD 1.0"
[JSON-LD 1.1]: https://json-ld.org/spec/latest/json-ld/ "JSON-LD 1.1"
[JSON-LD API]: http://www.w3.org/TR/json-ld-api/ "JSON-LD 1.0 Processing Algorithms and API"
[JSON-LD Framing]: http://json-ld.org/spec/latest/json-ld-framing/
[JSON-LD test suite]: http://json-ld.org/test-suite/