Skip to content

Commit

Permalink
feat: Jason.encode_to_iodata! rather than Jason.encode!
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Nov 26, 2024
1 parent 486a20d commit 2f7435f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- BREAKING CHANGE: The JSON library is now configurable via the `:json_library` option at compile time
- Drop support for Elixir < 1.15
- Use `Jason.encode_to_iodata!` rather than `Jason.encode!` for default JSON encoding

### Removed
- BREAKING CHANGE: Remove generated `defs/0` function
Expand Down
4 changes: 2 additions & 2 deletions lib/protox.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,12 @@ defmodule Protox do
iex> msg = %Namespace.Fiz.Foo{a: :BAR}
iex> {:ok, iodata} = Protox.json_encode(msg)
iex> iodata
["{", ["\\"a\\"", ":", "\\"BAR\\""], "}"]
["{", [[34, [[] | "a"], 34], ":", [34, [[] | "BAR"], 34]], "}"]
iex> msg = %Sub{a: 42}
iex> {:ok, iodata} = Protox.json_encode(msg)
iex> iodata
["{", ["\\"a\\"", ":", "42"], "}"]
["{", [[34, [[] | "a"], 34], ":", "42"], "}"]
iex> msg = %Msg{msg_k: %{1 => "foo", 2 => "bar"}}
iex> {:ok, iodata} = msg |> Protox.json_encode()
Expand Down
2 changes: 1 addition & 1 deletion lib/protox/jason.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Protox.Jason do
@impl true
def encode!(term) do
try do
Jason.encode!(term)
Jason.encode_to_iodata!(term)
rescue
e in Jason.EncodeError ->
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
Expand Down
6 changes: 1 addition & 5 deletions test/protox/json_encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,7 @@ defmodule Protox.JsonEncodeTest do
msg = %WithJason.Msg{msg_k: %{1 => "a", 2 => "b"}}
json = Protox.json_encode!(msg)

assert json == [
"{",
["\"msgK\"", ":", ["{", "\"2\"", ":", "\"b\"", ",", "\"1\"", ":", "\"a\"", "}"]],
"}"
]
assert IO.iodata_to_binary(json) == "{\"msgK\":{\"2\":\"b\",\"1\":\"a\"}}"
end

test "Success: Poison" do
Expand Down

0 comments on commit 2f7435f

Please sign in to comment.