Skip to content
Open
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
7 changes: 6 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{erl_opts, [debug_info]}.
{minimum_otp_vsn, "22.0"}.

{erl_opts, [warnings_as_errors]}.

{deps, []}.

{xref_checks, [undefined_function_calls,undefined_functions,locals_not_used]}.

{project_plugins, [rebar3_ex_doc, rebar3_hex]}.

{hex, [{doc, #{provider => ex_doc}}]}.
Expand Down
20 changes: 18 additions & 2 deletions src/thoas_decode.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{throw_error, 2}, {continue, 7}
]}]).

-dialyzer(no_improper_lists).

-export([decode/2]).

% We use integers instead of atoms to take advantage of the jump table optimization
Expand All @@ -13,6 +15,20 @@
-define(key, 2).
-define(object, 3).

-if(?OTP_RELEASE =:= 22).
convert_to_atom(Binary) ->
binary_to_atom(Binary, utf8).

convert_to_existing_atom(Binary) ->
binary_to_existing_atom(Binary, utf8).
-else.
convert_to_atom(Binary) ->
binary_to_atom(Binary).

convert_to_existing_atom(Binary) ->
binary_to_existing_atom(Binary).
-endif.

array(Rest, Input, Skip, Stack, StringDecode, KeyDecode) ->
value(Rest, Input, Skip, [?array, [] | Stack], StringDecode, KeyDecode).

Expand Down Expand Up @@ -1089,12 +1105,12 @@ key_decode_function(Options) ->

identity(Key) -> Key.

to_atom(Key) when is_binary(Key) -> binary_to_atom(Key);
to_atom(Key) when is_binary(Key) -> convert_to_atom(Key);
to_atom(Key) -> Key.

to_existing_atom(Key) when is_binary(Key) ->
try
binary_to_existing_atom(Key)
convert_to_existing_atom(Key)
catch
error:badarg -> Key
end;
Expand Down
15 changes: 13 additions & 2 deletions src/thoas_encode.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{inline, [{float, 1}, {integer, 1}, {error_invalid_byte_error, 2}]}
]).

-dialyzer(no_improper_lists).

%%% Normal reflection based API that turns Erlang terms into JSON.
-export([encode/2]).

Expand All @@ -15,6 +17,15 @@
non_recursive_array/1, non_recursive_object/1
]).

-if(?OTP_RELEASE >= 25).
convert_float(Float) ->
erlang:float_to_binary(Float, [short]).
-else.
convert_float(Float) ->
erlang:float_to_binary(Float).
-endif.


%%% A boolean value as JSON.
-spec boolean(boolean()) -> iodata().
boolean('true') -> <<"true">>;
Expand All @@ -35,7 +46,7 @@ null() -> <<"null">>.
%%% A float in JSON format.
-spec float(float()) -> iodata().
float(Float) ->
erlang:float_to_binary(Float, [short]).
convert_float(Float).

%%% An integer in JSON format.
-spec integer(integer()) -> iodata().
Expand Down Expand Up @@ -1751,7 +1762,7 @@ value({{Y, M, D}, {H, I, S}}, Escape)
is_integer(I) andalso I >= 0 andalso I =< 59 andalso
is_float(S) andalso S >= 0 andalso S < 60 ->
Dt = io_lib:format("~4..0b-~2..0b-~2..0bT~2..0b:~2..0b:~sZ",
[Y, M, D, H, I, float_to_binary(S, [short])]),
[Y, M, D, H, I, convert_float(S)]),
encode_string(iolist_to_binary(Dt), Escape);
value(Value, Escape) when is_list(Value) ->
list(Value, Escape);
Expand Down
16 changes: 15 additions & 1 deletion test/thoas_encode_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ integer_test_() ->
|| {Input, Expected} <- Cases
].

-if(?OTP_RELEASE >= 25).
float_test_() ->
Cases = [
{0.0, <<"0.0">>},
Expand All @@ -49,6 +50,8 @@ float_test_() ->
?_assertEqual(Expected, float(Input))
|| {Input, Expected} <- Cases
].
-endif.


string_test_() ->
Cases = [
Expand Down Expand Up @@ -122,10 +125,21 @@ encode_test_() ->
types_test_() ->
Cases =[
{#{<<"date">> => {2023,8,7}}, <<"{\"date\":\"2023-08-07\"}">>},
{#{<<"datetime">> => {{2023,8,7},{19,2,24}}}, <<"{\"datetime\":\"2023-08-07T19:02:24Z\"}">>},
{#{<<"datetime">> => {{2023,8,7},{19,2,24}}}, <<"{\"datetime\":\"2023-08-07T19:02:24Z\"}">>}
],
[
?_assertEqual(Expected, iolist_to_binary(encode(Input, #{})))
|| {Input, Expected} <- Cases
].

-if(?OTP_RELEASE >= 25).
types_withfloat_test_() ->
Cases =[
{#{<<"datetime">> => {{2023,8,7},{19,2,24.65432}}}, <<"{\"datetime\":\"2023-08-07T19:02:24.65432Z\"}">>}
],
[
?_assertEqual(Expected, iolist_to_binary(encode(Input, #{})))
|| {Input, Expected} <- Cases
].
-endif.