Skip to content

Commit

Permalink
doc: how to configure the JSON library
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Nov 29, 2024
1 parent fb06e5f commit 5ea9232
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,19 @@ iex> Fiz.Foo.json_decode("{\"a\":42}")
iex> Fiz.Foo.json_decode!("{\"a\":42}")
```


### JSON library configuration

By default, `protox` uses [`Jason`](https://github.com/michalmuskala/jason) to encode values to JSON (mostly to escape strings). You can also use [`Poison`](https://github.com/devinus/poison):
By default, `protox` uses [`Jason`](https://github.com/michalmuskala/jason) to encode values to JSON. You can also use [`Poison`](https://github.com/devinus/poison) by configuring the `json_library` option:

```elixir
iex> Protox.json_decode!(iodata, Fiz.Foo, json_library: Protox.Poison)
iex> Protox.json_encode!(msg, json_library: Protox.Poison)
defmodule MyModule do
use Protox, schema: """
syntax = "proto3";
message Foo {}
""",
json_library: Poison # or Jason
end
```

ℹ️ You can use any other library by implementing the `Protox.JsonLibrary` behaviour.
Expand Down
10 changes: 0 additions & 10 deletions lib/protox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ defmodule Protox do
## Errors
This function returns a tuple `{:error, reason}` if:
- `input` could not be decoded to JSON; `reason` is a `Protox.JsonDecodingError` error
## JSON library configuration
TODO: document json library configuration
"""
@doc since: "2.0.0"
@spec json_decode(iodata(), atom()) :: {:ok, struct()} | {:error, any()}
Expand Down Expand Up @@ -198,13 +195,6 @@ defmodule Protox do
iex> {:ok, iodata} = msg |> Protox.json_encode()
iex> :binary.list_to_bin(iodata)
"{\\"msgK\\":{\\"2\\":\\"bar\\",\\"1\\":\\"foo\\"}}"
## JSON library configuration
TODO: document the json library configuration
## Encoding specifications
See https://developers.google.com/protocol-buffers/docs/proto3#json for the specifications
of the encoding.
"""
@doc since: "2.0.0"
@spec json_encode(struct()) :: {:ok, iodata()} | {:error, any()}
Expand Down

0 comments on commit 5ea9232

Please sign in to comment.