Skip to content

Commit

Permalink
Update JSON code to support both jiffy and json libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
badlop committed Apr 30, 2024
1 parent e9097ca commit aaf491b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/ejabberd_oauth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,10 @@ process(_Handlers,
ExpiresIn
end,
{ok, VerifiedScope} = oauth2_response:scope(Response),
json_response(200, {[
{<<"access_token">>, AccessToken},
{<<"token_type">>, Type},
{<<"scope">>, str:join(VerifiedScope, <<" ">>)},
{<<"expires_in">>, Expires}]});
json_response(200, #{<<"access_token">> => AccessToken,
<<"token_type">> => Type,
<<"scope">> => str:join(VerifiedScope, <<" ">>),
<<"expires_in">> => Expires});
{error, Error} when is_atom(Error) ->
json_error(400, <<"invalid_grant">>, Error)
end;
Expand Down Expand Up @@ -762,8 +761,8 @@ json_response(Code, Body) ->
%% https://tools.ietf.org/html/draft-ietf-oauth-v2-25#section-5.2
json_error(Code, Error, Reason) ->
Desc = json_error_desc(Reason),
Body = {[{<<"error">>, Error},
{<<"error_description">>, Desc}]},
Body = #{<<"error">> => Error,
<<"error_description">> => Desc},
json_response(Code, Body).

json_error_desc(access_denied) -> <<"Access denied">>;
Expand Down
5 changes: 4 additions & 1 deletion src/ext_mod.erl
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,10 @@ get_commit_details2(Path) ->
end.

parse_details(Body) ->
{Contents} = misc:json_decode(Body),
Contents = case misc:json_decode(Body) of
C when is_list(C) -> C;
C when is_map(C) -> maps:to_list(C)
end,

{_, {Commit}} = lists:keyfind(<<"commit">>, 1, Contents),
{_, Sha} = lists:keyfind(<<"sha">>, 1, Commit),
Expand Down
5 changes: 4 additions & 1 deletion src/misc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ crypto_hmac(Type, Key, Data, MacL) -> crypto:macN(hmac, Type, Key, Data, MacL).
json_encode(Term) ->
jiffy:encode(Term).
json_decode(Bin) ->
jiffy:decode(Bin).
case jiffy:decode(Bin) of
{List} when is_list(List) -> List;
Other -> Other
end.
-else.
json_encode(Term) ->
iolist_to_binary(json:encode(Term)).
Expand Down
3 changes: 2 additions & 1 deletion src/mod_conversejs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) ->
undefined -> Init2;
BoshURL -> [{<<"bosh_service_url">>, BoshURL} | Init2]
end,
InitMap = maps:from_list(Init3),
{200, [html],
[<<"<!DOCTYPE html>">>,
<<"<html>">>,
Expand All @@ -89,7 +90,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) ->
<<"</head>">>,
<<"<body>">>,
<<"<script>">>,
<<"converse.initialize(">>, misc:json_encode({Init3}), <<");">>,
<<"converse.initialize(">>, misc:json_encode(InitMap), <<");">>,
<<"</script>">>,
<<"</body>">>,
<<"</html>">>]};
Expand Down
7 changes: 3 additions & 4 deletions src/mod_http_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ extract_args(<<"\n">>) -> [];
extract_args(Data) ->
case misc:json_decode(Data) of
List when is_list(List) -> List;
{List} when is_list(List) -> List;
Other -> [Other]
end.

Expand Down Expand Up @@ -509,9 +508,9 @@ json_response(Code, Body) when is_integer(Code) ->
%% message is binary
json_error(HTTPCode, JSONCode, Message) ->
{HTTPCode, ?HEADER(?CT_JSON),
misc:json_encode({[{<<"status">>, <<"error">>},
{<<"code">>, JSONCode},
{<<"message">>, Message}]})
misc:json_encode(#{<<"status">> => <<"error">>,
<<"code">> => JSONCode,
<<"message">> => Message})
}.

log(Call, Args, {Addr, Port}) ->
Expand Down

0 comments on commit aaf491b

Please sign in to comment.