Add handling of RDF.Graphs and RDF.Descriptions to JSON.LD.Encoder

This commit is contained in:
Marcel Otto 2018-03-13 00:56:32 +01:00
commit b9f2205243
5 changed files with 27 additions and 11 deletions

View file

@ -5,6 +5,17 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
[Keep a CHANGELOG](http://keepachangelog.com).
## Unreleased
### Added
- JSON-LD encoder can handle `RDF.Graph`s and `RDF.Description`s
[Compare v0.2.1...HEAD](https://github.com/marcelotto/jsonld-ex/compare/v0.2.1...HEAD)
## 0.2.1 - 2018-03-10
### Changed

View file

@ -1 +1 @@
0.2.1
0.2.2-dev

View file

@ -33,7 +33,9 @@ defmodule JSON.LD.Encoder do
end
end
def from_rdf!(dataset, options \\ %JSON.LD.Options{}) do
def from_rdf!(rdf_data, options \\ %JSON.LD.Options{})
def from_rdf!(%RDF.Dataset{} = dataset, options) do
with options = JSON.LD.Options.new(options) do
graph_map =
Enum.reduce RDF.Dataset.graphs(dataset), %{},
@ -99,6 +101,9 @@ defmodule JSON.LD.Encoder do
end
end
def from_rdf!(rdf_data, options),
do: rdf_data |> RDF.Dataset.new() |> from_rdf!(options)
# 3.5)
defp node_map_from_graph(graph, current, use_native_types, use_rdf_type) do
Enum.reduce(graph, current, fn ({subject, predicate, object}, node_map) ->

View file

@ -9,7 +9,7 @@ defmodule JSON.LD do
@extension "jsonld"
@media_type "application/ld+json"
def options, do: JSON.LD.Options.new
def options, do: JSON.LD.Options.new
@keywords ~w[
@base

View file

@ -3,7 +3,7 @@ defmodule JSON.LD.EncoderTest do
doctest JSON.LD.Encoder
alias RDF.{Dataset}
alias RDF.{Dataset, Graph, Description}
alias RDF.NS
alias RDF.NS.{XSD, RDFS}
@ -19,7 +19,7 @@ defmodule JSON.LD.EncoderTest do
def gets_serialized_to(input, output, opts \\ []) do
data_structs = Keyword.get(opts, :only, [Dataset])
data_structs = Keyword.get(opts, :data_structs, [Dataset, Graph])
Enum.each data_structs, fn data_struct ->
assert JSON.LD.Encoder.from_rdf!(data_struct.new(input), opts) == output
end
@ -36,7 +36,7 @@ defmodule JSON.LD.EncoderTest do
|> gets_serialized_to([%{
"@id" => "http://a/b",
"http://a/c" => [%{"@id" => "http://a/d"}]
}])
}], data_structs: [Dataset, Graph, Description])
end
test "should generate object list" do
@ -47,7 +47,7 @@ defmodule JSON.LD.EncoderTest do
%{"@id" => "http://example.com/d"},
%{"@id" => "http://example.com/e"}
]
}])
}], data_structs: [Dataset, Graph, Description])
end
test "should generate property list" do
@ -56,7 +56,7 @@ defmodule JSON.LD.EncoderTest do
"@id" => "http://example.com/b",
"http://example.com/c" => [%{"@id" => "http://example.com/d"}],
"http://example.com/e" => [%{"@id" => "http://example.com/f"}]
}])
}], data_structs: [Dataset, Graph, Description])
end
test "serializes multiple subjects" do
@ -77,7 +77,7 @@ defmodule JSON.LD.EncoderTest do
|> gets_serialized_to([%{
"@id" => "http://example.com/a",
"http://example.com/b" => [%{"@value" => "foo", "@type" => "http://example.com/d"}]
}])
}], data_structs: [Dataset, Graph, Description])
end
test "integer" do
@ -183,7 +183,7 @@ defmodule JSON.LD.EncoderTest do
|> gets_serialized_to([%{
"@id" => "_:a",
"http://example.com/a" => [%{"@id" => "http://example.com/b"}]
}])
}], data_structs: [Dataset, Graph, Description])
end
test "should generate blank nodes as object" do
@ -409,7 +409,7 @@ defmodule JSON.LD.EncoderTest do
|> Enum.each(fn ({title, data}) ->
@tag data: data
test title, %{data: %{input: input, output: output}} do
input |> gets_serialized_to(output, only: [Dataset])
input |> gets_serialized_to(output, data_structs: [Dataset])
end
end)
end