Skip to content

Commit

Permalink
make poison optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vasspilka committed Oct 26, 2018
1 parent e115577 commit 9ba23f1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
12 changes: 10 additions & 2 deletions guides/Getting Started.md
Expand Up @@ -10,13 +10,21 @@ Commanded can be installed from hex as follows.
end
```

2. Fetch mix dependencies:
2. Optionally add `poison` to make `Commanded.Serialization.JsonSerializer` available:

```elixir
def deps do
[{:poison, "~> 3.1 or ~> 4.0"}]
end
```

3. Fetch mix dependencies:

```console
$ mix deps.get
```

3. Configure one of the supported event stores by following the "Choosing an Event Store" guide.
4. Configure one of the supported event stores by following the "Choosing an Event Store" guide.

## Starting the Commanded application

Expand Down
12 changes: 10 additions & 2 deletions guides/Serialization.md
@@ -1,8 +1,16 @@
# Serialization

JSON serialization is used by default for event and snapshot data.
JSON serialization can be used for event and snapshot data.

The included `Commanded.Serialization.JsonSerializer` module provides an extension point to allow additional decoding of the deserialized value. This can be used for parsing data into valid structures, such as date/time parsing from a string.
To enable JSON serialization with the included `Commanded.Serialization.JsonSerializer` module add `poison` to your deps

```elixir
def deps do
[{:poison, "~> 3.1 or ~> 4.0"}]
end
```

The `Commanded.Serialization.JsonSerializer` module provides an extension point to allow additional decoding of the deserialized value. This can be used for parsing data into valid structures, such as date/time parsing from a string.

The example event below has an implementation of the `Commanded.Serialization.JsonDecoder` protocol to parse the date into a `NaiveDateTime` struct.

Expand Down
50 changes: 26 additions & 24 deletions lib/commanded/serialization/json_serializer.ex
@@ -1,30 +1,32 @@
defmodule Commanded.Serialization.JsonSerializer do
@moduledoc """
A serializer that uses the JSON format.
"""
if Code.ensure_loaded?(Poison) do
defmodule Commanded.Serialization.JsonSerializer do
@moduledoc """
A serializer that uses the JSON format.
"""

alias Commanded.EventStore.TypeProvider
alias Commanded.Serialization.JsonDecoder
alias Commanded.EventStore.TypeProvider
alias Commanded.Serialization.JsonDecoder

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

@doc """
Deserialize given JSON binary data to the expected type.
"""
def deserialize(binary, config \\ [])
def deserialize(binary, config) do
type = case Keyword.get(config, :type) do
nil -> nil
type -> TypeProvider.to_struct(type)
@doc """
Serialize given term to JSON binary data.
"""
def serialize(term) do
Poison.encode!(term)
end

binary
|> Poison.decode!(as: type)
|> JsonDecoder.decode()
@doc """
Deserialize given JSON binary data to the expected type.
"""
def deserialize(binary, config \\ [])
def deserialize(binary, config) do
type = case Keyword.get(config, :type) do
nil -> nil
type -> TypeProvider.to_struct(type)
end

binary
|> Poison.decode!(as: type)
|> JsonDecoder.decode()
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -50,7 +50,7 @@ defmodule Commanded.Mixfile do
defp deps do
[
{:elixir_uuid, "~> 1.2"},
{:poison, "~> 3.1 or ~> 4.0"},
{:poison, "~> 3.1 or ~> 4.0", optional: true},

# Build & test tools
{:dialyxir, "~> 0.5", only: :dev, runtime: false},
Expand Down

0 comments on commit 9ba23f1

Please sign in to comment.