Support for pretty printing by passing options through to Jason 1.1

This commit is contained in:
Marcel Otto 2018-07-11 00:17:36 +02:00
parent b5c19ead51
commit a40e2a5026
6 changed files with 47 additions and 7 deletions

View file

@ -5,6 +5,16 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
[Keep a CHANGELOG](http://keepachangelog.com).
## Unreleased
- Upgrade to Jason 1.1
- Pass options to `JSON.LD.Encoder.encode/2` and `JSON.LD.Encoder.encode!/2`
through to Jason; this allows to use the new Jason pretty printing options
[Compare v0.2.2...HEAD](https://github.com/marcelotto/jsonld-ex/compare/v0.2.2...HEAD)
## 0.2.2 - 2018-03-17
### Added

View file

@ -119,6 +119,16 @@ JSON.LD.write_file!(dataset, "file.jsonld")
```
## 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.
```elixir
JSON.LD.write_file!(dataset, "file.jsonld", pretty: true)
JSON.LD.write_string(dataset, "file.jsonld", pretty: [indent: "\t"])
```
## Getting help
- [Documentation](http://hexdocs.pm/json_ld)

View file

@ -15,14 +15,14 @@ defmodule JSON.LD.Encoder do
def encode(data, opts \\ []) do
with {:ok, json_ld_object} <- from_rdf(data, opts) do
encode_json(json_ld_object)
encode_json(json_ld_object, opts)
end
end
def encode!(data, opts \\ []) do
data
|> from_rdf!(opts)
|> encode_json!
|> encode_json!(opts)
end
def from_rdf(dataset, options \\ %JSON.LD.Options{}) do
@ -320,11 +320,11 @@ defmodule JSON.LD.Encoder do
defp encode_json(value, opts \\ []) do
Jason.encode(value)
Jason.encode(value, opts)
end
defp encode_json!(value, opts \\ []) do
Jason.encode!(value)
Jason.encode!(value, opts)
end
end

View file

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

View file

@ -9,11 +9,11 @@
"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]}]},
"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]}]},
"jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"rdf": {:hex, :rdf, "0.4.0", "10db43f71931f95e3707a5a0357d46149e6c47be8ae83f17199ff296765891db", [:mix], []},
"rdf": {:hex, :rdf, "0.4.1", "8c879a091cc2a6035cc6e955186948a15477c9cce4a7ca61a54f38f7259ff396", [:mix], []},
"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], []},
}

View file

@ -25,6 +25,26 @@ defmodule JSON.LD.EncoderTest do
end
end
test "pretty printing" do
dataset = Dataset.new {~I<http://a/b>, ~I<http://a/c>, ~I<http://a/d>}
assert JSON.LD.Encoder.encode!(dataset) ==
"[{\"@id\":\"http://a/b\",\"http://a/c\":[{\"@id\":\"http://a/d\"}]}]"
assert JSON.LD.Encoder.encode!(dataset, pretty: true) ==
"""
[
{
"@id": "http://a/b",
"http://a/c": [
{
"@id": "http://a/d"
}
]
}
]
""" |> String.trim()
end
test "an empty RDF.Dataset is serialized to an JSON array string" do
assert JSON.LD.Encoder.encode!(Dataset.new) == "[]"