From aaf491b19999292474c1bb59c0336e96462cf50a Mon Sep 17 00:00:00 2001 From: Badlop Date: Tue, 30 Apr 2024 14:10:20 +0200 Subject: [PATCH] Update JSON code to support both jiffy and json libraries --- src/ejabberd_oauth.erl | 13 ++++++------- src/ext_mod.erl | 5 ++++- src/misc.erl | 5 ++++- src/mod_conversejs.erl | 3 ++- src/mod_http_api.erl | 7 +++---- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/ejabberd_oauth.erl b/src/ejabberd_oauth.erl index 59357245da3..678d4318e2a 100644 --- a/src/ejabberd_oauth.erl +++ b/src/ejabberd_oauth.erl @@ -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; @@ -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">>; diff --git a/src/ext_mod.erl b/src/ext_mod.erl index b3cbc8abbf6..219a377dd43 100644 --- a/src/ext_mod.erl +++ b/src/ext_mod.erl @@ -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), diff --git a/src/misc.erl b/src/misc.erl index 4144e95f9d1..1903c7e4c6a 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -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)). diff --git a/src/mod_conversejs.erl b/src/mod_conversejs.erl index 59d6122c0a2..9b532510f12 100644 --- a/src/mod_conversejs.erl +++ b/src/mod_conversejs.erl @@ -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], [<<"">>, <<"">>, @@ -89,7 +90,7 @@ process([], #request{method = 'GET', host = Host, raw_path = RawPath}) -> <<"">>, <<"">>, <<"">>, <<"">>, <<"">>]}; diff --git a/src/mod_http_api.erl b/src/mod_http_api.erl index 784f26cf916..a04490c9f30 100644 --- a/src/mod_http_api.erl +++ b/src/mod_http_api.erl @@ -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. @@ -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}) ->