@ -4,6 +4,8 @@ defmodule BlockScoutWeb.API.EthRPC.View do
"""
use BlockScoutWeb , :view
@jsonrpc_2_0 ~s( "jsonrpc":"2.0" )
defstruct [ :result , :id , :error ]
def render ( " show.json " , %{ result : result , id : id } ) do
@ -50,50 +52,64 @@ defmodule BlockScoutWeb.API.EthRPC.View do
end )
end
defimpl Poison.Encoder , for : BlockScoutWeb.API.EthRPC.View do
def encode ( % BlockScoutWeb.API.EthRPC.View { result : result , id : id , error : error } , _options ) when is_nil ( error ) do
result = Poison . encode! ( result )
@doc """
Encodes id into JSON string
"""
@spec sanitize_id ( any ( ) ) :: non_neg_integer ( ) | String . t ( )
def sanitize_id ( id ) do
if is_integer ( id ) , do : id , else : " \" #{ id } \" "
end
"""
{ " jsonrpc " :" 2.0 " , " result " : #{result},"id":#{id}}
"""
@doc """
Encodes error into JSON string
"""
@spec sanitize_error ( any ( ) , :jason | :poison ) :: String . t ( )
def sanitize_error ( error , json_encoder ) do
case json_encoder do
:jason -> if is_map ( error ) , do : Jason . encode! ( error ) , else : " \" #{ error } \" "
:poison -> if is_map ( error ) , do : Poison . encode! ( error ) , else : " \" #{ error } \" "
end
end
@doc """
Pass " jsonrpc " :" 2.0 " to use in Poison.Encoder and Jason.Encoder below
"""
@spec jsonrpc_2_0 ( ) :: String . t ( )
def jsonrpc_2_0 , do : @jsonrpc_2_0
def encode ( % BlockScoutWeb.API.EthRPC.View { id : id , error : error } , _options ) when is_map ( error ) do
error = Poison . encode! ( error )
defimpl Poison.Encoder , for : BlockScoutWeb.API.EthRPC.View do
alias BlockScoutWeb.API.EthRPC.View
def encode ( % View { result : result , id : id , error : error } , _options ) when is_nil ( error ) do
result = Poison . encode! ( result )
"""
{ " jsonrpc " :" 2.0 " , " error " : #{error},"id": #{id}}
{ #{View.jsonrpc_2_0()},"result": #{result},"id": #{View.sanitize_id(id) }}
"""
end
def encode ( % BlockScoutWeb.API.EthRPC. View{ id : id , error : error } , _options ) do
def encode ( % View { id : id , error : error } , _options ) do
"""
{ " jsonrpc " :" 2.0 " , " error " : " #{ error } " , " id " : #{id }}
{ #{View.jsonrpc_2_0()},"error": #{View.sanitize_error(error, :poison)},"id": #{View.sanitize_id(id) }}
"""
end
end
defimpl Jason.Encoder , for : BlockScoutWeb.API.EthRPC.View do
def encode ( % BlockScoutWeb.API.EthRPC.View { result : result , id : id , error : error } , _options ) when is_nil ( error ) do
result = Jason . encode! ( result )
# credo:disable-for-next-line
alias BlockScoutWeb.API.EthRPC.View
"""
{ " jsonrpc " :" 2.0 " , " result " : #{result},"id":#{id}}
"""
end
def encode ( % BlockScoutWeb.API.EthRPC.View { id : id , error : error } , _options ) when is_map ( error ) do
error = Jason . encode! ( error )
def encode ( % View { result : result , id : id , error : error } , _options ) when is_nil ( error ) do
result = Jason . encode! ( result )
"""
{ " jsonrpc " :" 2.0 " , " error " : #{error},"id": #{id }}
{ #{View.jsonrpc_2_0()},"result": #{result},"id": #{View.sanitize_id(id)}}
"""
end
def encode ( % BlockScoutWeb.API.EthRPC. View{ id : id , error : error } , _options ) do
def encode ( % View { id : id , error : error } , _options ) do
"""
{ " jsonrpc " :" 2.0 " , " error " : " #{ error } " , " id " : #{id }}
{ #{View.jsonrpc_2_0()},"error": #{View.sanitize_error(error, :jason)},"id": #{View.sanitize_id(id) }}
"""
end
end