Skip to content

Commit

Permalink
Jason is now a common default, remove usage instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
michalmuskala committed Jun 21, 2019
1 parent e44decf commit 8c0701f
Showing 1 changed file with 6 additions and 44 deletions.
50 changes: 6 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,56 +39,18 @@ Full documentation can be found at [https://hexdocs.pm/jason](https://hexdocs.pm

### Postgrex

You need to define a custom "types" module in an `.ex` file, somewhere in `lib`:
Versions starting at 0.14.0 use `Jason` by default. For earlier versions, please refer to
[previous versions of this document](https://github.com/michalmuskala/jason/tree/v1.1.2#postgrex).

```elixir
Postgrex.Types.define(MyApp.PostgresTypes, [], json: Jason)

## If using with ecto, you also need to pass ecto default extensions:

Postgrex.Types.define(MyApp.PostgresTypes, [] ++ Ecto.Adapters.Postgres.extensions(), json: Jason)
```

Then you can use the module, by passing it to `Postgrex.start_link`.
### Ecto

To replicate fully the current behaviour of `Poison` when used in Ecto applications,
you need to configure `Jason` to be the default encoder of the `postgrex` library, in `config/config.exs`:

```elixir
config :postgrex, :json_library, Jason
```

Additionally, when using PostgreSQL, you need to define a custom types module as described
above, and configure your repo to use it (in either `config/config.exs` or `config/<env>.exs`):

```elixir
config :my_app, MyApp.Repo, types: MyApp.PostgresTypes
```
Versions starting at 3.0.0 use `Jason` by default. For earlier versions, please refer to
[previous versions of this document](https://github.com/michalmuskala/jason/tree/v1.1.2#ecto).

### Plug (and Phoenix)

First, you need to configure `Plug.Parsers` to use `Jason` for parsing JSON. You need to find,
where you're plugging the `Plug.Parsers` plug (in case of Phoenix, it will be in the
Endpoint module) and configure it in your endpoint module (`lib/app_web/endpoint.ex`),
for example:

```elixir
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Jason
```

Additionally, for Phoenix, you need to configure the "encoder" in `config/config.exs`:

```elixir
config :phoenix, :format_encoders,
json: Jason
```

A custom JSON encoder for Phoenix channels is unfortunately a bit more involved,
you can find code for a custom serializer and how to use it [in here](https://gist.github.com/michalmuskala/d5fabcd26be2befdfb72b72e0b0f2797).
Phoenix starting at 1.4.0 uses `Jason` by default. For earlier versions, please refer to
[previous versions of this document](https://github.com/michalmuskala/jason/tree/v1.1.2#plug-and-phoenix).

### Absinthe

Expand Down

0 comments on commit 8c0701f

Please sign in to comment.