Skip to content

Commit

Permalink
replace poison with jason
Browse files Browse the repository at this point in the history
  • Loading branch information
vasspilka committed Jan 18, 2019
1 parent 0a2e990 commit a95a631
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 33 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

### Enhancements

- BREAKING CHANGE: Replace `:poison` with `:jason` ([#144](https://github.com/commanded/eventstore/pull/144)).
To support this change the users will need to @derive the Jason.Encoder protocol to all their Events.
This can be done by either using `@derive Jason.Encoder` before defining the struct. Or using `Protocol.derive/2` for each event.
Examples below.
```
defmodule Event1 do
@derive Jason.Encoder
defstruct [:id, :data]
end
```
```
require Protocol
for event <- [Event1, Event2, Event3] do
Protocol.derive(Jason.Encoder, event)
end
```
- Add `:socket` and `:socket_dir` config options ([#132](https://github.com/commanded/eventstore/pull/132)).
- Rename `uuid` dependency to `elixir_uuid` ([#135](https://github.com/commanded/eventstore/pull/135)).
- Subscription concurrency ([#134](https://github.com/commanded/eventstore/pull/134)).
Expand Down
6 changes: 5 additions & 1 deletion config/distributed.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use Mix.Config

config :logger, backends: []
config :logger,
backends: [],
compile_time_purge_matching: [
[application: :eventstore]
]

config :ex_unit,
capture_log: true,
Expand Down
11 changes: 10 additions & 1 deletion guides/Event Serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ The default serialization of event data and metadata uses Erlang's [external ter

You must implement the `EventStore.Serializer` behaviour to provide your preferred serialization format.

## Example JSON serializer
## Jason Serializer
EventStore includes a JSON serializer using Jason under the `EventStore.JsonSerializer` module. To include it add `{:jason, "~> 1.1"}` to your dependencies. Then configure EventStore to use it.

```elixir
config :eventstore, EventStore.Storage,
serializer: EventStore.JsonSerializer,
# ...
```

## Example JSON (Poison) serializer

The example serializer below serializes event data and metadata to JSON using the [Poison](https://github.com/devinus/poison) library.

Expand Down
31 changes: 31 additions & 0 deletions lib/event_store/json_serializer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
if Code.ensure_loaded?(Jason) do
defmodule EventStore.JsonSerializer do
@moduledoc """
A serializer that uses the JSON format.
"""

@behaviour EventStore.Serializer

@doc """
Serialize given term to JSON binary data.
"""
def serialize(term) do
Jason.encode!(term)
end

@doc """
Deserialize given JSON binary data to the expected type.
"""
def deserialize(binary, config) do
case Keyword.get(config, :type, nil) do
nil ->
Jason.decode!(binary)

type ->
type
|> String.to_existing_atom()
|> struct(Jason.decode!(binary, keys: :atoms!))
end
end
end
end
2 changes: 1 addition & 1 deletion lib/register_postgres_types.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if Code.ensure_loaded?(Postgrex) do
Postgrex.Types.define(EventStore.PostgresTypes, [], json: Poison)
Postgrex.Types.define(EventStore.PostgresTypes, [], json: Jason)
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule EventStore.Mixfile do
{:ex_doc, "~> 0.19", only: :dev},
{:markdown, github: "devinus/markdown", only: :dev},
{:mix_test_watch, "~> 0.9", only: :dev},
{:poison, "~> 2.2 or ~> 3.0", optional: true}
{:jason, "~> 1.1", optional: true}
]
end

Expand Down
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"markdown": {:git, "https://github.com/devinus/markdown.git", "d065dbcc4e242a85ca2516fdadd0082712871fd8", []},
"mix_test_watch": {:hex, :mix_test_watch, "0.9.0", "c72132a6071261893518fa08e121e911c9358713f62794a90c95db59042af375", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.14.1", "63247d4a5ad6b9de57a0bac5d807e1c32d41e39c04b8a4156a26c63bcd8a2e49", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
}
5 changes: 4 additions & 1 deletion test/event_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ defmodule EventStoreTest do
end
end

defmodule(ExampleData, do: defstruct([:data]))
defmodule ExampleData do
@derive Jason.Encoder
defstruct([:data])
end

test "record snapshot" do
assert record_snapshot() != nil
Expand Down
1 change: 1 addition & 0 deletions test/support/event_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule EventStore.EventFactory do
@serializer Application.get_env(:eventstore, EventStore.Storage)[:serializer] || EventStore.JsonSerializer

defmodule Event do
@derive Jason.Encoder
defstruct event: nil
end

Expand Down
27 changes: 0 additions & 27 deletions test/support/json_serializer.ex

This file was deleted.

0 comments on commit a95a631

Please sign in to comment.