Poison/Examples
Poison/Examples[edit]
The following are some examples of how to use Poison in Elixir:
Encoding[edit]
Encode to JSON[edit]
```elixir data = %{"name" => "John", "age" => 30} json = Poison.encode!(data) ```
Encode to JSON with options[edit]
```elixir data = %{"name" => "John", "age" => 30} options = [pretty: true] json = Poison.encode!(data, options) ```
Decoding[edit]
Decode JSON[edit]
```elixir json = ~s({"name": "John", "age": 30}) data = Poison.decode!(json) ```
Decode JSON to specific struct[edit]
```elixir defmodule User do
defstruct [:name, :age]
end
json = ~s({"name": "John", "age": 30}) data = Poison.decode!(json, as: %User{}) ```
Streaming[edit]
Stream encode to JSON[edit]
```elixir data = %{"name" => "John", "age" => 30} Poison.encode_to_iodata!(data) |> IO.write ```
Stream decode JSON[edit]
```elixir json = ~s({"name": "John", "age": 30}) Poison.stream!(json) |> Enum.each(&IO.puts(&1)) ```
For detailed information on Poison's usage and available options, please refer to the Poison documentation.