Permalink
Browse files

make poison optional

  • Loading branch information...
vasspilka committed Oct 26, 2018
1 parent e115577 commit 9ba23f13917b7b944a1d7705d6c30d23e9523563
Showing with 47 additions and 29 deletions.
  1. +10 −2 guides/Getting Started.md
  2. +10 −2 guides/Serialization.md
  3. +26 −24 lib/commanded/serialization/json_serializer.ex
  4. +1 −1 mix.exs
@@ -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
@@ -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.
@@ -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
@@ -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},

0 comments on commit 9ba23f1

Please sign in to comment.