Compare commits
5 Commits
53631c6425
...
27f9084976
Author | SHA1 | Date |
---|---|---|
FloatingGhost | 27f9084976 | |
Marcel Otto | e54dec5b92 | |
Marcel Otto | ad61223c53 | |
Marcel Otto | f3bc0a1d84 | |
Marcel Otto | 363309a739 |
12
CHANGELOG.md
12
CHANGELOG.md
|
@ -5,20 +5,24 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
|
|||
[Keep a CHANGELOG](http://keepachangelog.com).
|
||||
|
||||
|
||||
## Unreleased
|
||||
## 0.3.5 - 2022-04-26
|
||||
|
||||
### Added
|
||||
|
||||
- the `JSON.LD.Encoder` now supports implicit compaction by providing a context
|
||||
as a map or a URL string for a remote context with the new `:context` option
|
||||
as a map, a `RDF.PropertyMap` or a URL string for a remote context with the
|
||||
new `:context` option
|
||||
|
||||
### Changed
|
||||
|
||||
- context maps can be given now with atom keys or as `RDF.PropertyMap` to
|
||||
- context maps can be given now with atom keys or as a `RDF.PropertyMap` to
|
||||
`JSON.LD.context/2` and `JSON.LD.compact/3`
|
||||
- the base IRI of a `RDF.Graph` or the `RDF.default_base_iri/0` is used as the
|
||||
default `:base` in the `JSON.LD.Encoder`
|
||||
- `RDF.Vocabulary.Namespace` modules can be set as base IRI
|
||||
|
||||
|
||||
[Compare v0.3.4...HEAD](https://github.com/rdf-elixir/jsonld-ex/compare/v0.3.4...HEAD)
|
||||
[Compare v0.3.4...v0.3.5](https://github.com/rdf-elixir/jsonld-ex/compare/v0.3.4...v0.3.5)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -121,10 +121,16 @@ 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.
|
||||
|
||||
```elixir
|
||||
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 of 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.
|
||||
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.
|
||||
|
||||
```elixir
|
||||
JSON.LD.write_file!(dataset, "file.jsonld", pretty: true)
|
||||
|
|
|
@ -13,6 +13,8 @@ defmodule JSON.LD.Encoder do
|
|||
compaction is performed using this context
|
||||
- `:base`: : Allows to specify a base URI to be used during compaction
|
||||
(only when `:context` is provided).
|
||||
Default is the base IRI of the encoded graph or if none present or in case of
|
||||
encoded datasets the `RDF.default_base_iri/0`.
|
||||
- `:use_native_types`: If this flag is set to `true`, RDF literals with a datatype IRI
|
||||
that equals `xsd:integer` or `xsd:double` are converted to a JSON numbers and
|
||||
RDF literals with a datatype IRI that equals `xsd:boolean` are converted to `true`
|
||||
|
@ -54,12 +56,26 @@ defmodule JSON.LD.Encoder do
|
|||
@impl RDF.Serialization.Encoder
|
||||
@spec encode(RDF.Data.t(), keyword) :: {:ok, String.t()} | {:error, any}
|
||||
def encode(data, opts \\ []) do
|
||||
opts = set_base_iri(data, opts)
|
||||
|
||||
with {:ok, json_ld_object} <- from_rdf(data, opts),
|
||||
{:ok, json_ld_object} <- maybe_compact(json_ld_object, opts) do
|
||||
encode_json(json_ld_object, opts)
|
||||
end
|
||||
end
|
||||
|
||||
defp set_base_iri(%Graph{base_iri: base_iri}, opts) when not is_nil(base_iri) do
|
||||
Keyword.put_new(opts, :base, IRI.to_string(base_iri))
|
||||
end
|
||||
|
||||
defp set_base_iri(_, opts) do
|
||||
if base = RDF.default_base_iri() do
|
||||
Keyword.put_new(opts, :base, IRI.to_string(base))
|
||||
else
|
||||
opts
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: unless we find a way to allow more optimized encode! versions, remove this since it's never used (see the respective warning)
|
||||
@impl RDF.Serialization.Encoder
|
||||
@spec encode!(RDF.Data.t(), Options.convertible()) :: String.t()
|
||||
|
|
|
@ -5,6 +5,8 @@ defmodule JSON.LD.Options do
|
|||
as specified at <https://www.w3.org/TR/json-ld-api/#the-jsonldoptions-type>
|
||||
"""
|
||||
|
||||
alias RDF.IRI
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
base: String.t() | nil,
|
||||
compact_arrays: boolean,
|
||||
|
@ -32,5 +34,14 @@ defmodule JSON.LD.Options do
|
|||
|
||||
@spec new(convertible) :: t
|
||||
def new(%__MODULE__{} = options), do: options
|
||||
def new(options), do: struct(__MODULE__, options)
|
||||
|
||||
def new(options) do
|
||||
struct(__MODULE__, options)
|
||||
|> set_base(options[:base])
|
||||
end
|
||||
|
||||
@spec set_base(t, IRI.coercible()) :: t
|
||||
def set_base(%__MODULE__{} = options, base) do
|
||||
%__MODULE__{options | base: base && base |> IRI.coerce_base() |> to_string()}
|
||||
end
|
||||
end
|
||||
|
|
11
mix.exs
11
mix.exs
|
@ -63,7 +63,9 @@ defmodule JSON.LD.Mixfile do
|
|||
|
||||
defp deps do
|
||||
[
|
||||
rdf_ex_dep(:rdf, "~> 0.9"),
|
||||
{:rdf, git: "https://akkoma.dev/AkkomaGang/rdf-ex.git",
|
||||
ref: "6fe1c613884c41da213966ea3fb2531f59417e8d"
|
||||
},
|
||||
{:jason, "~> 1.2"},
|
||||
{:httpoison, "~> 1.6"},
|
||||
{:dialyxir, "~> 1.1", only: :dev, runtime: false},
|
||||
|
@ -73,13 +75,6 @@ defmodule JSON.LD.Mixfile do
|
|||
]
|
||||
end
|
||||
|
||||
defp rdf_ex_dep(dep, version) do
|
||||
case System.get_env("RDF_EX_PACKAGES_SRC") do
|
||||
"LOCAL" -> {dep, path: "../#{dep}"}
|
||||
_ -> {dep, version}
|
||||
end
|
||||
end
|
||||
|
||||
defp dialyzer do
|
||||
[
|
||||
plt_file: {:no_warn, "priv/plts/dialyzer.plt"}
|
||||
|
|
5
mix.lock
5
mix.lock
|
@ -4,7 +4,7 @@
|
|||
"cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"},
|
||||
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.1", "ebd1a1d7aff97f27c66654e78ece187abdc646992714164380d8a041eda16754", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a6efd3366130eab84ca372cbd4a7d3c3a97bdfcfb4911233b035d117063f0af"},
|
||||
"cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"},
|
||||
"decimal": {:hex, :decimal, "1.9.0", "83e8daf59631d632b171faabafb4a9f4242c514b0a06ba3df493951c08f64d07", [:mix], [], "hexpm", "b1f2343568eed6928f3e751cf2dffde95bfaa19dd95d09e8a9ea92ccfd6f7d85"},
|
||||
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
|
||||
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
|
||||
"earmark_parser": {:hex, :earmark_parser, "1.4.25", "2024618731c55ebfcc5439d756852ec4e85978a39d0d58593763924d9a15916f", [:mix], [], "hexpm", "56749c5e1c59447f7b7a23ddb235e4b3defe276afc220a6227237f3efe83f51e"},
|
||||
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
|
||||
|
@ -27,7 +27,8 @@
|
|||
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
|
||||
"protocol_ex": {:hex, :protocol_ex, "0.4.4", "c9717d1c0bdabe37d7653965dc02e78580d0d06a1f86d737b7941b55241f70d6", [:mix], [], "hexpm", "2b78ed0e5ec76f62b0debaf92dc8e795551cdaf7fc22b8816b3d57225020ac99"},
|
||||
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
|
||||
"rdf": {:hex, :rdf, "0.12.0", "8c0c1c3237f5186419f9a528e34bd463e513594afe3fd1c6e5ea749925272ba9", [:mix], [{:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:elixir_uuid, "~> 1.2", [hex: :elixir_uuid, repo: "hexpm", optional: true]}, {:protocol_ex, "~> 0.4.4", [hex: :protocol_ex, repo: "hexpm", optional: false]}], "hexpm", "f454570c928d8be73dea28468f92446ac4e8efa34fea8d0039d546efd8c0a711"},
|
||||
"rdf": {:git, "https://akkoma.dev/AkkomaGang/rdf-ex.git", "6fe1c613884c41da213966ea3fb2531f59417e8d", [ref: "6fe1c613884c41da213966ea3fb2531f59417e8d"]},
|
||||
"rdf_ex": {:git, "https://akkoma.dev/AkkomaGang/rdf-ex.git", "6fe1c613884c41da213966ea3fb2531f59417e8d", [ref: "6fe1c613884c41da213966ea3fb2531f59417e8d"]},
|
||||
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
|
||||
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
|
||||
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
|
||||
|
|
|
@ -3,7 +3,7 @@ defmodule JSON.LD.EncoderTest do
|
|||
|
||||
doctest JSON.LD.Encoder
|
||||
|
||||
alias RDF.{Dataset, Graph, Description}
|
||||
alias RDF.{Dataset, Graph, Description, IRI}
|
||||
alias RDF.NS
|
||||
|
||||
import RDF.Sigils
|
||||
|
@ -683,29 +683,87 @@ defmodule JSON.LD.EncoderTest do
|
|||
"familyName" => "http://schema.org/familyName"
|
||||
}
|
||||
|
||||
expected_result =
|
||||
"""
|
||||
{
|
||||
"@context": {
|
||||
"familyName": "http://schema.org/familyName",
|
||||
"givenName": "http://schema.org/givenName"
|
||||
},
|
||||
"@id": "http://manu.sporny.org/about#manu",
|
||||
"familyName": "Sporny",
|
||||
"givenName": "Manu",
|
||||
"http://example.com/bar": {
|
||||
"@id": "Bar"
|
||||
},
|
||||
"http://example.com/foo": 3.14,
|
||||
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type": {
|
||||
"@id": "http://schema.org/Person"
|
||||
}
|
||||
}
|
||||
"""
|
||||
|> String.trim()
|
||||
|
||||
assert JSON.LD.Encoder.encode!(graph,
|
||||
context: context,
|
||||
base: EX.__base_iri__(),
|
||||
use_native_types: true,
|
||||
use_rdf_type: true,
|
||||
pretty: true
|
||||
) == expected_result
|
||||
|
||||
assert JSON.LD.Encoder.encode!(graph,
|
||||
context: context,
|
||||
base: EX,
|
||||
use_native_types: true,
|
||||
use_rdf_type: true,
|
||||
pretty: true
|
||||
) == expected_result
|
||||
end
|
||||
|
||||
test "base_iri of a RDF.Graph is used as the default for :base" do
|
||||
context = %{
|
||||
"p" => %{
|
||||
"@id" => IRI.to_string(EX.p()),
|
||||
"@type" => "@id"
|
||||
}
|
||||
}
|
||||
|
||||
assert JSON.LD.Encoder.encode!(
|
||||
Graph.new({EX.S, EX.p(), EX.O}, base_iri: EX),
|
||||
context: context,
|
||||
pretty: true
|
||||
) ==
|
||||
"""
|
||||
{
|
||||
"@context": {
|
||||
"familyName": "http://schema.org/familyName",
|
||||
"givenName": "http://schema.org/givenName"
|
||||
"p": {
|
||||
"@id": "#{EX.p()}",
|
||||
"@type": "@id"
|
||||
}
|
||||
},
|
||||
"@id": "http://manu.sporny.org/about#manu",
|
||||
"familyName": "Sporny",
|
||||
"givenName": "Manu",
|
||||
"http://example.com/bar": {
|
||||
"@id": "Bar"
|
||||
"@id": "S",
|
||||
"p": "O"
|
||||
}
|
||||
"""
|
||||
|> String.trim()
|
||||
|
||||
assert JSON.LD.Encoder.encode!(
|
||||
Graph.new({EX.S, EX.p(), S.O}, base_iri: EX),
|
||||
context: context,
|
||||
base: S,
|
||||
pretty: true
|
||||
) ==
|
||||
"""
|
||||
{
|
||||
"@context": {
|
||||
"p": {
|
||||
"@id": "#{EX.p()}",
|
||||
"@type": "@id"
|
||||
}
|
||||
},
|
||||
"http://example.com/foo": 3.14,
|
||||
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type": {
|
||||
"@id": "http://schema.org/Person"
|
||||
}
|
||||
"@id": "http://example.com/S",
|
||||
"p": "O"
|
||||
}
|
||||
"""
|
||||
|> String.trim()
|
||||
|
|
Loading…
Reference in New Issue