Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OTP json module #170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/jose_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ check_json(_Fallback, Entries) ->
undefined ->
case code:ensure_loaded(elixir) of
{module, elixir} ->
check_json_modules([ojson, 'Elixir.Jason', 'Elixir.Poison', jiffy, jsone, jsx, thoas]);
check_json_modules([json, ojson, 'Elixir.Jason', 'Elixir.Poison', jiffy, jsone, jsx, thoas]);
_ ->
check_json_modules([ojson, jiffy, jsone, jsx, thoas])
check_json_modules([json, ojson, jiffy, jsone, jsx, thoas])
end;
M when is_atom(M) ->
check_json_module(M)
Expand All @@ -469,6 +469,8 @@ check_json(_Fallback, Entries) ->
[{json_module, JSONModule} | Entries].

%% @private
check_json_module(json) ->
jose_json_otp;
check_json_module(jiffy) ->
jose_json_jiffy;
check_json_module(jsx) ->
Expand Down
20 changes: 20 additions & 0 deletions src/json/jose_json_otp.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%% -*- mode: erlang; tab-width: 4; indent-tabs-mode: 1; st-rulers: [70] -*-
%% vim: ts=4 sw=4 ft=erlang noet
-module(jose_json_otp).
-behaviour(jose_json).

%% jose_json callbacks
-export([decode/1]).
-export([encode/1]).

%%====================================================================
%% jose_json callbacks
%%====================================================================

decode(Binary) -> json:decode(Binary).

Check warning on line 14 in src/json/jose_json_otp.erl

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function :json.decode/1 does not exist.

Check warning on line 14 in src/json/jose_json_otp.erl

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function :json.decode/1 does not exist.

encode(Term) -> iolist_to_binary(json:encode(Term)).

Check warning on line 16 in src/json/jose_json_otp.erl

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function :json.encode/1 does not exist.

Check warning on line 16 in src/json/jose_json_otp.erl

View workflow job for this annotation

GitHub Actions / Dialyzer

unknown_function

Function :json.encode/1 does not exist.

%%%-------------------------------------------------------------------
%%% Internal functions
%%%-------------------------------------------------------------------
12 changes: 12 additions & 0 deletions test/jose_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ defmodule JOSETest do
assert binary == :erlang.element(2, JOSE.JWE.to_binary(jwe))
assert jwe == JOSE.JWE.from_binary(binary)
assert jwe == JOSE.JWE.from(jwe)

if System.otp_release() >= "27" do
# OTP json
JOSE.json_module(:json)
assert :jose_json_otp == JOSE.json_module()
assert map == :erlang.element(2, JOSE.JWE.to_map(jwe))
assert binary == :erlang.element(2, JOSE.JWE.to_binary(jwe))
assert jwe == JOSE.JWE.from_binary(binary)
assert jwe == JOSE.JWE.from(jwe)
end

# jsx
# # jiffy
# JOSE.json_module(:jiffy)
# assert :jose_json_jiffy == JOSE.json_module()
Expand Down
Loading