Skip to content

Commit

Permalink
Use new json module included in Erlang/OTP 27.0-rc2 when available
Browse files Browse the repository at this point in the history
  • Loading branch information
badlop committed Apr 29, 2024
1 parent b4d0900 commit 24d2361
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 4 additions & 1 deletion rebar.config
Expand Up @@ -19,9 +19,12 @@
{erl_opts, [debug_info,
{src_dirs, ["src"]},
{i, "include"},
{if_version_below, "27", {d, 'OTP_BELOW_27'}},
{platform_define, "^(R|1|20)", deprecated_stacktrace}]}.

{deps, [{jiffy, "~> 1.1.1", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.1"}}},
{deps, [{if_version_below, "27",
{jiffy, "~> 1.1.1", {git, "https://github.com/davisp/jiffy.git", {tag, "1.1.1"}}}
},
{yconf, "~> 1.0.15", {git, "https://github.com/processone/yconf.git", {tag, "1.0.15"}}},
{idna, "~> 6.0", {git, "https://github.com/benoitc/erlang-idna", {tag, "6.0.0"}}},
{base64url, "~> 1.0", {git, "https://github.com/dvv/base64url", {tag, "1.0.1"}}},
Expand Down
2 changes: 1 addition & 1 deletion src/p1_acme.app.src
Expand Up @@ -22,7 +22,7 @@
{registered, []},
{applications, [kernel, stdlib,
crypto, inets, public_key, ssl,
base64url, idna, jiffy, jose, yconf]},
base64url, idna, jose, yconf]},
{env, []},
{mod, {p1_acme, []}},

Expand Down
18 changes: 15 additions & 3 deletions src/p1_acme.erl
Expand Up @@ -681,7 +681,7 @@ handle_http_response(ReqFun, {{_, Code, Slogan}, Hdrs, Body}, State, RetryTimeou
{_, Type} when Type == "application/problem+json";
Type == "application/json" ->
State1 = update_nonce(Hdrs, State),
try jiffy:decode(Body, [return_maps]) of
try json_decode(Body) of
JSON when Type == "application/json" ->
{ok, {Code, Hdrs, JSON}, State1};
JSON when Type == "application/problem+json" ->
Expand Down Expand Up @@ -913,7 +913,7 @@ jose_json(#state{account = {Key, AccURL}, nonce = Nonce} = State, Data, URL) ->
JwsMap = case AccURL of
undefined ->
{_, BinaryPubKey} = jose_jwk:to_binary(PubKey),
PubKeyJson = jiffy:decode(BinaryPubKey),
PubKeyJson = json_decode(BinaryPubKey),
JwsMap0#{<<"jwk">> => PubKeyJson};
_ ->
JwsMap0#{<<"kid">> => iolist_to_binary(AccURL)}
Expand All @@ -930,7 +930,19 @@ auth_key(#state{account = {PrivKey, _}}, Token) ->

-spec encode_json(map()) -> binary().
encode_json(JSON) ->
iolist_to_binary(jiffy:encode(JSON)).
json_encode(JSON).

-ifdef(OTP_BELOW_27).
json_encode(Term) ->
iolist_to_binary(jiffy:encode(Term)).
json_decode(Bin) ->
jiffy:decode(Bin).
-else.
json_encode(Term) ->
iolist_to_binary(json:encode(Term)).
json_decode(Bin) ->
json:decode(Bin).
-endif.

%%%===================================================================
%%% Misc
Expand Down

0 comments on commit 24d2361

Please sign in to comment.