Skip to content

Commit

Permalink
fix: no more warnings when Poison or Jason are not in the dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Nov 15, 2024
1 parent db3822a commit f45062e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 40 deletions.
57 changes: 37 additions & 20 deletions lib/protox/jason.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@ defmodule Protox.Jason do
@moduledoc false
@behaviour Protox.JsonLibrary

@impl true
def load() do
if Code.ensure_loaded?(Jason) do
if Code.ensure_loaded?(Jason) do
@impl true
def load() do
{:ok, Jason}
else
end

@impl true
def decode!(_jason_module, iodata) do
try do
Jason.decode!(iodata)
rescue
e in Jason.DecodeError ->
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
end
end

@impl true
def encode!(_jason_module, term) do
try do
Jason.encode!(term)
rescue
e in Jason.EncodeError ->
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
end
end
else
@impl true
def load() do
:error
end
end

@impl true
def decode!(jason_module, iodata) do
try do
jason_module.decode!(iodata)
rescue
e in Jason.DecodeError ->
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
@impl true
def decode!(_jason_module, _iodata) do
raise Protox.JsonDecodingError.new(
"Jason library not loaded. Please check your project dependencies."
)
end
end

@impl true
def encode!(jason_module, term) do
try do
jason_module.encode!(term)
rescue
e in Jason.EncodeError ->
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
@impl true
def encode!(_jason_module, _term) do
raise Protox.JsonEncodingError.new(
"Jason library not loaded. Please check your project dependencies."
)
end
end
end
57 changes: 37 additions & 20 deletions lib/protox/poison.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@ defmodule Protox.Poison do
@moduledoc false
@behaviour Protox.JsonLibrary

@impl true
def load() do
if Code.ensure_loaded?(Poison) do
if Code.ensure_loaded?(Poison) do
@impl true
def load() do
{:ok, Poison}
else
end

@impl true
def decode!(poison_module, iodata) do
try do
poison_module.decode!(iodata)
rescue
e in [Poison.DecodeError, Poison.ParseError] ->
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
end
end

@impl true
def encode!(poison_module, term) do
try do
poison_module.encode!(term)
rescue
e in Poison.EncodeError ->
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
end
end
else
@impl true
def load() do
:error
end
end

@impl true
def decode!(poison_module, iodata) do
try do
poison_module.decode!(iodata)
rescue
e in [Poison.DecodeError, Poison.ParseError] ->
reraise Protox.JsonDecodingError.new(Exception.message(e)), __STACKTRACE__
@impl true
def decode!(_poison_module, _iodata) do
raise Protox.JsonDecodingError.new(
"Poison library not loaded. Please check your project dependencies."
)
end
end

@impl true
def encode!(poison_module, term) do
try do
poison_module.encode!(term)
rescue
e in Poison.EncodeError ->
reraise Protox.JsonEncodingError.new(Exception.message(e)), __STACKTRACE__
@impl true
def encode!(_poison_module, _term) do
raise Protox.JsonEncodingError.new(
"Poison library not loaded. Please check your project dependencies."
)
end
end
end

0 comments on commit f45062e

Please sign in to comment.