Go to file
FloatingGhost 27f9084976 bump rdf version 2022-08-03 22:27:46 +01:00
.github/workflows Add Elixir 1.13.2 to CI 2022-01-21 20:34:12 +01:00
lib Use base of graph or default_base_iri as default in encoder 2022-04-23 01:11:09 +02:00
priv/plts Run Dialyzer in CI 2020-06-22 01:44:57 +02:00
test Use base of graph or default_base_iri as default in encoder 2022-04-23 01:11:09 +02:00
.editorconfig Add .editorconfig 2017-06-25 02:06:24 +02:00
.formatter.exs Apply mix formatter 2020-06-20 04:25:58 +02:00
.gitignore Run Dialyzer in CI 2020-06-22 01:44:57 +02:00
.iex.exs Adapt to new RDF.ex literal datatype system 2020-05-07 17:21:12 +02:00
CHANGELOG.md Prepare release 0.3.5 2022-04-26 20:43:04 +02:00
CODE_OF_CONDUCT.md Add CODE_OF_CONDUCT 2017-06-25 02:07:04 +02:00
CONTRIBUTING.md Update contribution guidelines 2020-06-22 01:29:32 +02:00
LICENSE.md Prepare release 0.3.1 2020-06-01 21:44:16 +02:00
README.md Add example on automatic compaction during encoding 2022-04-25 21:36:38 +02:00
VERSION bump rdf version 2022-08-03 22:27:46 +01:00
mix.exs bump rdf version 2022-08-03 22:27:46 +01:00
mix.lock bump rdf version 2022-08-03 22:27:46 +01:00

README.md

JSON-LD-logo-64

JSON-LD.ex

CI Hex.pm Coverage Status

An implementation of the JSON-LD standard for Elixir and RDF.ex.

The API documentation can be found here. For a guide and more information about RDF.ex and it's related projects, go to https://rdf-elixir.dev.

Features

  • 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)

TODO

Installation

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

Usage

Expand a document

"""
{
 "@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"}]}]

Compact a document

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"}

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.

dataset = JSON.LD.read_file!("file.jsonld")
JSON.LD.write_file!(dataset, "file.jsonld")

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.

JSON.LD.write_file!(dataset, "file.jsonld", context: %{ex: "https://example.com/"})
JSON.LD.write_file!(dataset, "file.jsonld", context: "https://schema.org/")

Pretty printing

Pretty printing is possible on all writer functions with all 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"])

Contributing

see CONTRIBUTING for details.

Consulting

If you need help with your Elixir and Linked Data projects, just contact NinjaConcept via contact@ninjaconcept.com.

(c) 2017-present Marcel Otto. MIT Licensed, see LICENSE for details.