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

Add support for Thoas (a yet another JSON library) #126

Merged
merged 3 commits into from
Sep 1, 2022
Merged
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
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PROJECT = jose
PROJECT_DESCRIPTION = JSON Object Signing and Encryption (JOSE) for Erlang and Elixir.
PROJECT_VERSION = 1.11.3

TEST_DEPS = jiffy jsone jsx libdecaf ojson proper
TEST_DEPS = jiffy jsone jsx libdecaf ojson proper thoas

dep_jiffy = hex 1.1.1
dep_jsone = hex 1.7.0
Expand All @@ -11,8 +11,10 @@ dep_jsx = hex 3.1.0
dep_libdecaf = hex 2.1.0
# dep_libsodium = git https://github.com/talklittle/erlang-libsodium.git otp-23-remove-erl-interface
dep_ojson = hex 1.0.0
dep_thoas = hex 0.4.0
dep_proper = git https://github.com/proper-testing/proper.git bfd7d862dd5082eeca65c192a7021d0e4de5973e


include erlang.mk

.PHONY: docker-build docker-load docker-setup docker-save docker-shell docker-test
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defmodule JOSE.Mixfile do
# {:libsodium, "~> 0.0.10", only: [:dev, :test]},
{:ojson, "~> 1.0", only: [:dev, :test]},
{:poison, "~> 5.0", only: [:dev, :test]},
{:thoas, "~> 0.4", only: [:dev, :test]},
{:ex_doc, "~> 0.28", only: :dev},
{:earmark, "~> 1.4", only: :dev}
]
Expand Down
6 changes: 4 additions & 2 deletions src/jose_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,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]);
check_json_modules([ojson, 'Elixir.Jason', 'Elixir.Poison', jiffy, jsone, jsx, thoas]);
_ ->
check_json_modules([ojson, jiffy, jsone, jsx])
check_json_modules([ojson, jiffy, jsone, jsx, thoas])
end;
M when is_atom(M) ->
check_json_module(M)
Expand All @@ -372,6 +372,8 @@ check_json_module(jsone) ->
jose_json_jsone;
check_json_module(ojson) ->
jose_json_ojson;
check_json_module(thoas) ->
jose_json_thoas;
check_json_module('Elixir.Jason') ->
jose_json_jason;
check_json_module('Elixir.Poison') ->
Expand Down
30 changes: 30 additions & 0 deletions src/json/jose_json_thoas.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%% -*- mode: erlang; tab-width: 4; indent-tabs-mode: 1; st-rulers: [70] -*-
%% vim: ts=4 sw=4 ft=erlang noet
%%%-------------------------------------------------------------------
%%% @author Michael Klishin <michael@clojurewerkz.org>
%%% @copyright 2022, Michael Klishin
%%% @doc
%%%
%%% @end
%%% Created : 30 Jul 2022 by Michael Klishin <michael@clojurewerkz.org>
%%%-------------------------------------------------------------------
-module(jose_json_thoas).
-behaviour(jose_json).

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

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

decode(Binary) ->
case thoas:decode(Binary) of
{ok, Value} -> Value;
{error, _} = Error ->
error(Error)
end.

encode(Term) ->
thoas:encode(Term).
34 changes: 34 additions & 0 deletions test/jose_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ 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)
# thoas
JOSE.json_module(:thoas)
assert :jose_json_thoas == 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)
# ojson
JOSE.json_module(:ojson)
assert :jose_json_ojson == JOSE.json_module()
Expand Down Expand Up @@ -219,6 +226,17 @@ defmodule JOSETest do
assert jwk == :erlang.element(2, JOSE.JWK.from_binary(password, JOSE.JWK.to_binary(password, jwk)))
assert jwk == :erlang.element(2, JOSE.JWK.from_map(password, JOSE.JWK.to_map(password, jwk)))
assert jwk == JOSE.JWK.from_pem(password, JOSE.JWK.to_pem(password, jwk))
# thoas
JOSE.json_module(:thoas)
assert :jose_json_thoas == JOSE.json_module()
assert map == :erlang.element(2, JOSE.JWK.to_map(jwk))
assert binary == :erlang.element(2, JOSE.JWK.to_binary(jwk))
assert jwk == JOSE.JWK.from_binary(binary)
assert jwk == JOSE.JWK.from(jwk)
assert jwk == JOSE.JWK.from_pem(JOSE.JWK.to_pem(jwk))
assert jwk == :erlang.element(2, JOSE.JWK.from_binary(password, JOSE.JWK.to_binary(password, jwk)))
assert jwk == :erlang.element(2, JOSE.JWK.from_map(password, JOSE.JWK.to_map(password, jwk)))
assert jwk == JOSE.JWK.from_pem(password, JOSE.JWK.to_pem(password, jwk))
# ojson
JOSE.json_module(:ojson)
assert :jose_json_ojson == JOSE.json_module()
Expand Down Expand Up @@ -289,6 +307,13 @@ defmodule JOSETest do
assert binary == :erlang.element(2, JOSE.JWS.to_binary(jws))
assert jws == JOSE.JWS.from_binary(binary)
assert jws == JOSE.JWS.from(jws)
# thoas
JOSE.json_module(:thoas)
assert :jose_json_thoas == JOSE.json_module()
assert map == :erlang.element(2, JOSE.JWS.to_map(jws))
assert binary == :erlang.element(2, JOSE.JWS.to_binary(jws))
assert jws == JOSE.JWS.from_binary(binary)
assert jws == JOSE.JWS.from(jws)
# ojson
JOSE.json_module(:ojson)
assert :jose_json_ojson == JOSE.json_module()
Expand Down Expand Up @@ -347,6 +372,13 @@ defmodule JOSETest do
assert binary == :erlang.element(2, JOSE.JWT.to_binary(jwt))
assert jwt == JOSE.JWT.from_binary(binary)
assert jwt == JOSE.JWT.from(jwt)
# thoas
JOSE.json_module(:thoas)
assert :jose_json_thoas == JOSE.json_module()
assert map == :erlang.element(2, JOSE.JWT.to_map(jwt))
assert binary == :erlang.element(2, JOSE.JWT.to_binary(jwt))
assert jwt == JOSE.JWT.from_binary(binary)
assert jwt == JOSE.JWT.from(jwt)
# ojson
JOSE.json_module(:ojson)
assert :jose_json_ojson == JOSE.json_module()
Expand Down Expand Up @@ -426,6 +458,8 @@ defmodule JOSETest do
assert :jose_json_jsone.decode(json) == term
assert :jose_json_jsx.encode(term) == json
assert :jose_json_jsx.decode(json) == term
assert :jose_json_thoas.encode(term) == json
assert :jose_json_thoas.decode(json) == term
assert :jose_json_poison_compat_encoder.encode(term) == json
assert :jose_json_poison_compat_encoder.decode(json) == term
assert :jose_json_poison_lexical_encoder.encode(term) == json
Expand Down