diff --git a/.iex.exs b/.iex.exs index f6fa9c5..683663c 100644 --- a/.iex.exs +++ b/.iex.exs @@ -1,6 +1,7 @@ import RDF.Sigils -alias RDF.NS.{XSD, RDFS, OWL, SKOS} +alias RDF.NS +alias RDF.NS.{RDFS, OWL, SKOS} alias RDF.{ Term, @@ -15,6 +16,8 @@ alias RDF.{ Description, Graph, Dataset, + + XSD } alias RDF.BlankNode, as: BNode diff --git a/lib/json/ld/decoder.ex b/lib/json/ld/decoder.ex index 2b22a57..3bc98f5 100644 --- a/lib/json/ld/decoder.ex +++ b/lib/json/ld/decoder.ex @@ -6,8 +6,7 @@ defmodule JSON.LD.Decoder do import JSON.LD.{NodeIdentifierMap, Utils} alias JSON.LD.NodeIdentifierMap - alias RDF.{Dataset, Graph} - alias RDF.NS.{XSD} + alias RDF.{Dataset, Graph, NS} @impl RDF.Serialization.Decoder @@ -117,30 +116,34 @@ defmodule JSON.LD.Decoder do {value, datatype} = cond do is_boolean(value) -> - value = value |> RDF.Boolean.new |> RDF.Literal.canonical |> RDF.Literal.lexical - datatype = if is_nil(datatype), do: XSD.boolean, else: datatype + value = value |> RDF.XSD.Boolean.new() |> RDF.XSD.Boolean.canonical() |> RDF.XSD.Boolean.lexical() + datatype = if is_nil(datatype), do: NS.XSD.boolean, else: datatype {value, datatype} - is_float(value) or (is_number(value) and datatype == to_string(XSD.double)) -> - value = value |> RDF.Double.new |> RDF.Literal.canonical |> RDF.Literal.lexical - datatype = if is_nil(datatype), do: XSD.double, else: datatype + is_float(value) or (is_number(value) and datatype == to_string(NS.XSD.double)) -> + value = value |> RDF.XSD.Double.new() |> RDF.XSD.Double.canonical() |> RDF.XSD.Double.lexical() + datatype = if is_nil(datatype), do: NS.XSD.double, else: datatype {value, datatype} - is_integer(value) or (is_number(value) and datatype == to_string(XSD.integer)) -> - value = value |> RDF.Integer.new |> RDF.Literal.canonical |> RDF.Literal.lexical - datatype = if is_nil(datatype), do: XSD.integer, else: datatype + is_integer(value) or (is_number(value) and datatype == to_string(NS.XSD.integer)) -> + value = value |> RDF.XSD.Integer.new() |> RDF.XSD.Integer.canonical() |> RDF.XSD.Integer.lexical() + datatype = if is_nil(datatype), do: NS.XSD.integer, else: datatype {value, datatype} is_nil(datatype) -> datatype = if Map.has_key?(item, "@language") do RDF.langString else - XSD.string + NS.XSD.string end {value, datatype} true -> {value, datatype} end - RDF.Literal.new(value, - %{datatype: datatype, language: item["@language"], canonicalize: true}) + + if language = item["@language"] do + RDF.Literal.new(value, language: language, canonicalize: true) + else + RDF.Literal.new(value, datatype: datatype, canonicalize: true) + end end defp list_to_rdf(list, node_id_map) do diff --git a/lib/json/ld/encoder.ex b/lib/json/ld/encoder.ex index be35509..631167c 100644 --- a/lib/json/ld/encoder.ex +++ b/lib/json/ld/encoder.ex @@ -4,8 +4,7 @@ defmodule JSON.LD.Encoder do use RDF.Serialization.Encoder - alias RDF.{IRI, BlankNode, Literal} - alias RDF.NS.{XSD} + alias RDF.{IRI, BlankNode, Literal, XSD, NS} @rdf_type to_string(RDF.NS.RDF.type) @rdf_nil to_string(RDF.NS.RDF.nil) @@ -279,38 +278,39 @@ defmodule JSON.LD.Encoder do %{"@id" => to_string(bnode)} end - defp rdf_to_object(%Literal{value: value, datatype: datatype} = literal, use_native_types) do + defp rdf_to_object(%Literal{literal: %datatype{}} = literal, use_native_types) do result = %{} + value = Literal.value(literal) converted_value = literal type = nil {converted_value, type, result} = if use_native_types do cond do - datatype == XSD.string -> + datatype == XSD.String -> {value, type, result} - datatype == XSD.boolean -> - if RDF.Boolean.valid?(literal) do + datatype == XSD.Boolean -> + if RDF.XSD.Boolean.valid?(literal) do {value, type, result} else - {converted_value, XSD.boolean, result} + {converted_value, NS.XSD.boolean, result} end - datatype in [XSD.integer, XSD.double] -> - if RDF.Literal.valid?(literal) do + datatype in [XSD.Integer, XSD.Double] -> + if Literal.valid?(literal) do {value, type, result} else {converted_value, type, result} end true -> - {converted_value, datatype, result} + {converted_value, Literal.datatype(literal), result} end else cond do - datatype == RDF.langString -> - {converted_value, type, Map.put(result, "@language", literal.language)} - datatype == XSD.string -> + datatype == RDF.LangString -> + {converted_value, type, Map.put(result, "@language", Literal.language(literal))} + datatype == XSD.String -> {converted_value, type, result} true -> - {converted_value, datatype, result} + {Literal.lexical(literal), Literal.datatype(literal), result} end end @@ -327,5 +327,4 @@ defmodule JSON.LD.Encoder do defp encode_json!(value, opts) do Jason.encode!(value, opts) end - end diff --git a/test/unit/encoder_test.exs b/test/unit/encoder_test.exs index aecf76a..cc4f76c 100644 --- a/test/unit/encoder_test.exs +++ b/test/unit/encoder_test.exs @@ -5,7 +5,6 @@ defmodule JSON.LD.EncoderTest do alias RDF.{Dataset, Graph, Description} alias RDF.NS - alias RDF.NS.{XSD, RDFS} import RDF.Sigils @@ -167,14 +166,14 @@ defmodule JSON.LD.EncoderTest do integer: 1, unsignedInt: 1, nonNegativeInteger: 1, - float: 1, + float: 1.0, nonPositiveInteger: -1, negativeInteger: -1, } |> Enum.each(fn ({type, _} = data) -> @tag data: data test "#{type}", %{data: {type, value}} do - {EX.a, EX.b, RDF.literal(value, datatype: apply(XSD, type, []))} + {EX.a, EX.b, RDF.literal(value, datatype: apply(NS.XSD, type, []))} |> gets_serialized_to([%{ "@id" => "http://example.com/a", "http://example.com/b" => [%{"@value" => "#{value}", "@type" => "http://www.w3.org/2001/XMLSchema##{type}"}] @@ -440,7 +439,7 @@ defmodule JSON.LD.EncoderTest do describe "problems" do %{ "xsd:boolean as value" => { - {~I, RDFS.range, XSD.boolean}, + {~I, NS.RDFS.range, NS.XSD.boolean}, [%{ "@id" => "http://data.wikia.com/terms#playable", "http://www.w3.org/2000/01/rdf-schema#range" => [