-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add native Elixir
JSON
support (#394)
* Add `JSON` support This implementation defaults to `JSON` if it's available. If it's not, it tries to use `Jason` as it did before. Some exceptions are changed (only for `JSON`, `Jason` is unaffected). For example, instead of `Jason.DecodeError`, when using `JSON` we raise `Protobuf.JSON.DecodeError`, and encode errors have a more varied shape (since they can be mostly anything coming from Elixir side). * Add Elixir 1.18.1 to workflow * Add note * fix typo * Fix dialyzer * Update lib/protobuf/json.ex
- Loading branch information
Showing
7 changed files
with
91 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Protobuf.JSON.JSONLibrary do | ||
@moduledoc false | ||
# Uses `JSON` for Elixir >= 1.18, Jason if Elixir < 1.18 and Jason available, | ||
# or returns error otherwise | ||
|
||
cond do | ||
Code.ensure_loaded?(JSON) -> | ||
def encode_to_iodata(encodable) do | ||
try do | ||
{:ok, JSON.encode_to_iodata!(encodable)} | ||
rescue | ||
exception -> | ||
{:error, exception} | ||
end | ||
end | ||
|
||
def decode(data) do | ||
case JSON.decode(data) do | ||
{:ok, decoded} -> {:ok, decoded} | ||
{:error, error} -> {:error, Protobuf.JSON.DecodeError.new(error)} | ||
end | ||
end | ||
|
||
Code.ensure_loaded?(Jason) -> | ||
def encode_to_iodata(encodable), do: Jason.encode_to_iodata(encodable) | ||
def decode(data), do: Jason.decode(data) | ||
|
||
true -> | ||
def encode_to_iodata(_), do: {:error, EncodeError.new(:no_json_lib)} | ||
def decode(_), do: {:error, EncodeError.new(:no_json_lib)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters