Switch from Poison to Jason for JSON encoding and decoding

This commit is contained in:
Marcel Otto 2018-03-16 00:08:39 +01:00
parent b9f2205243
commit 29524560bb
12 changed files with 46 additions and 46 deletions

View file

@ -11,6 +11,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
- JSON-LD encoder can handle `RDF.Graph`s and `RDF.Description`s - JSON-LD encoder can handle `RDF.Graph`s and `RDF.Description`s
### Changed
- Use Jason instead of Poison for JSON encoding and decoding, since it's faster and more standard conform
[Compare v0.2.1...HEAD](https://github.com/marcelotto/jsonld-ex/compare/v0.2.1...HEAD) [Compare v0.2.1...HEAD](https://github.com/marcelotto/jsonld-ex/compare/v0.2.1...HEAD)

View file

@ -53,7 +53,7 @@ end
"homepage": "http://manu.sporny.org/" "homepage": "http://manu.sporny.org/"
} }
""" """
|> Poison.Parser.parse! |> Jason.decode!
|> JSON.LD.expand |> JSON.LD.expand
``` ```
@ -67,7 +67,7 @@ produces
### Compact a document ### Compact a document
```elixir ```elixir
context = Poison.Parser.parse! """ context = Jason.decode! """
{ {
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",
@ -91,7 +91,7 @@ context = Poison.Parser.parse! """
} }
] ]
""" """
|> Poison.Parser.parse! |> Jason.decode!
|> JSON.LD.compact(context) |> JSON.LD.compact(context)
``` ```

View file

@ -86,14 +86,12 @@ defmodule JSON.LD.Decoder do
end end
end end
# TODO: This should not be dependent on Poison as a JSON parser in general,
# but determine available JSON parsers and use one heuristically or by configuration
def parse_json(content, _opts \\ []) do def parse_json(content, _opts \\ []) do
Poison.Parser.parse(content) Jason.decode(content)
end end
def parse_json!(content, _opts \\ []) do def parse_json!(content, _opts \\ []) do
Poison.Parser.parse!(content) Jason.decode!(content)
end end
def node_to_rdf(nil), do: nil def node_to_rdf(nil), do: nil

View file

@ -319,14 +319,12 @@ defmodule JSON.LD.Encoder do
end end
# TODO: This should not be dependent on Poison as a JSON encoder in general,
# but determine available JSON encoders and use one heuristically or by configuration
defp encode_json(value, opts \\ []) do defp encode_json(value, opts \\ []) do
Poison.encode(value) Jason.encode(value)
end end
defp encode_json!(value, opts \\ []) do defp encode_json!(value, opts \\ []) do
Poison.encode!(value) Jason.encode!(value)
end end
end end

View file

@ -60,7 +60,7 @@ defmodule JSON.LD.Mixfile do
defp deps do defp deps do
[ [
{:rdf, "~> 0.4"}, {:rdf, "~> 0.4"},
{:poison, "~> 3.0"}, {:jason, "~> 1.0"},
{:dialyxir, "~> 0.4", only: [:dev, :test], runtime: false}, {:dialyxir, "~> 0.4", only: [:dev, :test], runtime: false},
{:credo, "~> 0.6", only: [:dev, :test], runtime: false}, {:credo, "~> 0.6", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.14", only: :dev, runtime: false}, {:ex_doc, "~> 0.14", only: :dev, runtime: false},

View file

@ -9,10 +9,10 @@
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]}, "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"hackney": {:hex, :hackney, "1.11.0", "4951ee019df102492dabba66a09e305f61919a8a183a7860236c0fde586134b6", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, optional: false]}, {:idna, "5.1.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]}, "hackney": {:hex, :hackney, "1.11.0", "4951ee019df102492dabba66a09e305f61919a8a183a7860236c0fde586134b6", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, optional: false]}, {:idna, "5.1.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, optional: false]}]}, "idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, optional: false]}]},
"jason": {:hex, :jason, "1.0.0", "0f7cfa9bdb23fed721ec05419bcee2b2c21a77e926bce0deda029b5adc716fe2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, optional: true]}]},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], []}, "jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []}, "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
"rdf": {:hex, :rdf, "0.4.0", "10db43f71931f95e3707a5a0357d46149e6c47be8ae83f17199ff296765891db", [:mix], []}, "rdf": {:hex, :rdf, "0.4.0", "10db43f71931f95e3707a5a0357d46149e6c47be8ae83f17199ff296765891db", [:mix], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], []}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [:rebar3], []},

View file

@ -49,6 +49,6 @@ defmodule JSON.LD.TestSuite.FromRdfTest do
filename filename
|> file |> file
|> File.read! |> File.read!
|> Poison.Parser.parse! |> Jason.decode!
end end
end end

View file

@ -7,7 +7,7 @@ defmodule JSON.LD.TestSuite do
def parse_json_file!(file) do def parse_json_file!(file) do
case File.read(file(file)) do case File.read(file(file)) do
{:ok, content} -> Poison.Parser.parse!(content) {:ok, content} -> Jason.decode!(content)
{:error, reason} -> raise File.Error, path: file, action: "read", reason: reason {:error, reason} -> raise File.Error, path: file, action: "read", reason: reason
end end
end end

View file

@ -4,7 +4,7 @@ defmodule JSON.LD.CompactionTest do
alias RDF.NS.{RDFS, XSD} alias RDF.NS.{RDFS, XSD}
test "Flattened form of a JSON-LD document (EXAMPLES 57-59 of https://www.w3.org/TR/json-ld/#compacted-document-form)" do test "Flattened form of a JSON-LD document (EXAMPLES 57-59 of https://www.w3.org/TR/json-ld/#compacted-document-form)" do
input = Poison.Parser.parse! """ input = Jason.decode! """
[ [
{ {
"http://xmlns.com/foaf/0.1/name": [ "Manu Sporny" ], "http://xmlns.com/foaf/0.1/name": [ "Manu Sporny" ],
@ -16,7 +16,7 @@ defmodule JSON.LD.CompactionTest do
} }
] ]
""" """
context = Poison.Parser.parse! """ context = Jason.decode! """
{ {
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",
@ -27,7 +27,7 @@ defmodule JSON.LD.CompactionTest do
} }
} }
""" """
assert JSON.LD.compact(input, context) == Poison.Parser.parse! """ assert JSON.LD.compact(input, context) == Jason.decode! """
{ {
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",
@ -324,16 +324,16 @@ defmodule JSON.LD.CompactionTest do
} }
}, },
"compact-0007" => %{ "compact-0007" => %{
input: Poison.Parser.parse!(""" input: Jason.decode!("""
{"http://example.org/vocab#contains": "this-is-not-an-IRI"} {"http://example.org/vocab#contains": "this-is-not-an-IRI"}
"""), """),
context: Poison.Parser.parse!(""" context: Jason.decode!("""
{ {
"ex": "http://example.org/vocab#", "ex": "http://example.org/vocab#",
"ex:contains": {"@type": "@id"} "ex:contains": {"@type": "@id"}
} }
"""), """),
output: Poison.Parser.parse!(""" output: Jason.decode!("""
{ {
"@context": { "@context": {
"ex": "http://example.org/vocab#", "ex": "http://example.org/vocab#",
@ -355,7 +355,7 @@ defmodule JSON.LD.CompactionTest do
describe "@reverse" do describe "@reverse" do
%{ %{
"compact-0033" => %{ "compact-0033" => %{
input: Poison.Parser.parse!(""" input: Jason.decode!("""
[ [
{ {
"@id": "http://example.com/people/markus", "@id": "http://example.com/people/markus",
@ -371,13 +371,13 @@ defmodule JSON.LD.CompactionTest do
} }
] ]
"""), """),
context: Poison.Parser.parse!(""" context: Jason.decode!("""
{ {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",
"isKnownBy": { "@reverse": "http://xmlns.com/foaf/0.1/knows" } "isKnownBy": { "@reverse": "http://xmlns.com/foaf/0.1/knows" }
} }
"""), """),
output: Poison.Parser.parse!(""" output: Jason.decode!("""
{ {
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",

View file

@ -6,7 +6,7 @@ defmodule JSON.LD.ExpansionTest do
alias RDF.NS.{RDFS, XSD} alias RDF.NS.{RDFS, XSD}
test "Expanded form of a JSON-LD document (EXAMPLE 55 and 56 of https://www.w3.org/TR/json-ld/#expanded-document-form)" do test "Expanded form of a JSON-LD document (EXAMPLE 55 and 56 of https://www.w3.org/TR/json-ld/#expanded-document-form)" do
input = Poison.Parser.parse! """ input = Jason.decode! """
{ {
"@context": "@context":
{ {
@ -20,7 +20,7 @@ defmodule JSON.LD.ExpansionTest do
"homepage": "http://manu.sporny.org/" "homepage": "http://manu.sporny.org/"
} }
""" """
assert JSON.LD.expand(input) == Poison.Parser.parse! """ assert JSON.LD.expand(input) == Jason.decode! """
[ [
{ {
"http://xmlns.com/foaf/0.1/name": [ "http://xmlns.com/foaf/0.1/name": [
@ -540,7 +540,7 @@ defmodule JSON.LD.ExpansionTest do
}] }]
}, },
"expand-0004" => %{ "expand-0004" => %{
input: Poison.Parser.parse!(~s({ input: Jason.decode!(~s({
"@context": { "@context": {
"mylist1": {"@id": "http://example.com/mylist1", "@container": "@list"}, "mylist1": {"@id": "http://example.com/mylist1", "@container": "@list"},
"mylist2": {"@id": "http://example.com/mylist2", "@container": "@list"}, "mylist2": {"@id": "http://example.com/mylist2", "@container": "@list"},
@ -549,7 +549,7 @@ defmodule JSON.LD.ExpansionTest do
}, },
"http://example.org/property": { "@list": "one item" } "http://example.org/property": { "@list": "one item" }
})), })),
output: Poison.Parser.parse!(~s([ output: Jason.decode!(~s([
{ {
"http://example.org/property": [ "http://example.org/property": [
{ {
@ -700,7 +700,7 @@ defmodule JSON.LD.ExpansionTest do
describe "@reverse" do describe "@reverse" do
%{ %{
"expand-0037" => %{ "expand-0037" => %{
input: Poison.Parser.parse!(~s({ input: Jason.decode!(~s({
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name" "name": "http://xmlns.com/foaf/0.1/name"
}, },
@ -713,7 +713,7 @@ defmodule JSON.LD.ExpansionTest do
} }
} }
})), })),
output: Poison.Parser.parse!(~s([ output: Jason.decode!(~s([
{ {
"@id": "http://example.com/people/markus", "@id": "http://example.com/people/markus",
"@reverse": { "@reverse": {
@ -737,7 +737,7 @@ defmodule JSON.LD.ExpansionTest do
])) ]))
}, },
"expand-0043" => %{ "expand-0043" => %{
input: Poison.Parser.parse!(~s({ input: Jason.decode!(~s({
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",
"isKnownBy": { "@reverse": "http://xmlns.com/foaf/0.1/knows" } "isKnownBy": { "@reverse": "http://xmlns.com/foaf/0.1/knows" }
@ -757,7 +757,7 @@ defmodule JSON.LD.ExpansionTest do
] ]
} }
})), })),
output: Poison.Parser.parse!(~s([ output: Jason.decode!(~s([
{ {
"@id": "http://example.com/people/markus", "@id": "http://example.com/people/markus",
"http://xmlns.com/foaf/0.1/knows": [ "http://xmlns.com/foaf/0.1/knows": [
@ -870,7 +870,7 @@ defmodule JSON.LD.ExpansionTest do
}, },
"@reverse object with an @id property" => %{ "@reverse object with an @id property" => %{
input: Poison.Parser.parse!(~s({ input: Jason.decode!(~s({
"@id": "http://example/foo", "@id": "http://example/foo",
"@reverse": { "@reverse": {
"@id": "http://example/bar" "@id": "http://example/bar"
@ -879,7 +879,7 @@ defmodule JSON.LD.ExpansionTest do
exception: JSON.LD.InvalidReversePropertyMapError, exception: JSON.LD.InvalidReversePropertyMapError,
}, },
"colliding keywords" => %{ "colliding keywords" => %{
input: Poison.Parser.parse!(~s({ input: Jason.decode!(~s({
"@context": { "@context": {
"id": "@id", "id": "@id",
"ID": "@id" "ID": "@id"

View file

@ -4,7 +4,7 @@ defmodule JSON.LD.FlatteningTest do
alias RDF.NS.RDFS alias RDF.NS.RDFS
test "Flattened form of a JSON-LD document (EXAMPLE 60 and 61 of https://www.w3.org/TR/json-ld/#flattened-document-form)" do test "Flattened form of a JSON-LD document (EXAMPLE 60 and 61 of https://www.w3.org/TR/json-ld/#flattened-document-form)" do
input = Poison.Parser.parse! """ input = Jason.decode! """
{ {
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",
@ -23,7 +23,7 @@ defmodule JSON.LD.FlatteningTest do
] ]
} }
""" """
assert JSON.LD.flatten(input, input) == Poison.Parser.parse! """ assert JSON.LD.flatten(input, input) == Jason.decode! """
{ {
"@context": { "@context": {
"name": "http://xmlns.com/foaf/0.1/name", "name": "http://xmlns.com/foaf/0.1/name",
@ -107,7 +107,7 @@ defmodule JSON.LD.FlatteningTest do
] ]
}, },
"reverse properties" => %{ "reverse properties" => %{
input: Poison.Parser.parse!(""" input: Jason.decode!("""
[ [
{ {
"@id": "http://example.com/people/markus", "@id": "http://example.com/people/markus",
@ -125,7 +125,7 @@ defmodule JSON.LD.FlatteningTest do
} }
] ]
"""), """),
output: Poison.Parser.parse!(""" output: Jason.decode!("""
[ [
{ {
"@id": "http://example.com/people/dave", "@id": "http://example.com/people/dave",
@ -155,7 +155,7 @@ defmodule JSON.LD.FlatteningTest do
""") """)
}, },
"Simple named graph (Wikidata)" => %{ "Simple named graph (Wikidata)" => %{
input: Poison.Parser.parse!(""" input: Jason.decode!("""
{ {
"@context": { "@context": {
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
@ -187,7 +187,7 @@ defmodule JSON.LD.FlatteningTest do
] ]
} }
"""), """),
output: Poison.Parser.parse!(""" output: Jason.decode!("""
[{ [{
"@id": "http://example.org/ParisFact1", "@id": "http://example.org/ParisFact1",
"@type": ["http://www.w3.org/1999/02/22-rdf-syntax-ns#Graph"], "@type": ["http://www.w3.org/1999/02/22-rdf-syntax-ns#Graph"],
@ -212,7 +212,7 @@ defmodule JSON.LD.FlatteningTest do
""") """)
}, },
"Test Manifest (shortened)" => %{ "Test Manifest (shortened)" => %{
input: Poison.Parser.parse!(""" input: Jason.decode!("""
{ {
"@id": "", "@id": "",
"http://example/sequence": {"@list": [ "http://example/sequence": {"@list": [
@ -224,7 +224,7 @@ defmodule JSON.LD.FlatteningTest do
]} ]}
} }
"""), """),
output: Poison.Parser.parse!(""" output: Jason.decode!("""
[{ [{
"@id": "", "@id": "",
"http://example/sequence": [{"@list": [{"@id": "#t0001"}]}] "http://example/sequence": [{"@list": [{"@id": "#t0001"}]}]
@ -237,7 +237,7 @@ defmodule JSON.LD.FlatteningTest do
options: %{} options: %{}
}, },
"@reverse bnode issue (0045)" => %{ "@reverse bnode issue (0045)" => %{
input: Poison.Parser.parse!(""" input: Jason.decode!("""
{ {
"@context": { "@context": {
"foo": "http://example.org/foo", "foo": "http://example.org/foo",
@ -247,7 +247,7 @@ defmodule JSON.LD.FlatteningTest do
"bar": [ "http://example.org/origin", "_:b0" ] "bar": [ "http://example.org/origin", "_:b0" ]
} }
"""), """),
output: Poison.Parser.parse!(""" output: Jason.decode!("""
[ [
{ {
"@id": "_:b0", "@id": "_:b0",

View file

@ -210,7 +210,7 @@ defmodule JSON.LD.IRICompactionTest do
describe "compact-0018" do describe "compact-0018" do
setup do setup do
context = JSON.LD.context(Poison.Parser.parse! """ context = JSON.LD.context(Jason.decode! """
{ {
"id1": "http://example.com/id1", "id1": "http://example.com/id1",
"type1": "http://example.com/t1", "type1": "http://example.com/t1",
@ -324,7 +324,7 @@ defmodule JSON.LD.IRICompactionTest do
do: [values], do: [values],
else: values else: values
Enum.each(values, fn value -> Enum.each(values, fn value ->
value = Poison.Parser.parse!(value) value = Jason.decode!(value)
@tag data: {term, value} @tag data: {term, value}
test "uses #{term} for #{inspect value, limit: 3}", test "uses #{term} for #{inspect value, limit: 3}",
%{data: {term, value}, example_context: context, %{data: {term, value}, example_context: context,