-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
432 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,38 +17,283 @@ | |
%% ============================================================================= | ||
|
||
%% ----------------------------------------------------------------------------- | ||
%% @doc A utiliy module use to customised the JSON encoding for 3rd-party libs | ||
%% @doc A utility module use to customised the JSON encoding for 3rd-party libs | ||
%% e.g. erlang_jose | ||
%% @end | ||
%% ----------------------------------------------------------------------------- | ||
-module(bondy_json). | ||
|
||
-export([setup/0]). | ||
%% For backwards compat with jsx lib default | ||
-define(DEFAULT_FLOAT_FORMAT, [{decimals, 16}]). | ||
-define(DEFAULT_ENCODE_OPTS, [{float_format, ?DEFAULT_FLOAT_FORMAT}]). | ||
|
||
-if(?OTP_RELEASE >= 27). | ||
|
||
-define(IS_UINT(X), (is_integer(X) andalso X >= 0)). | ||
-define(IS_PNUM(X), (is_number(X) andalso X >= 0)). | ||
-define(IS_DATETIME(Y, M, D, H, Mi, S), | ||
( | ||
?IS_UINT(Y) andalso | ||
?IS_UINT(M) andalso | ||
?IS_UINT(D) andalso | ||
?IS_UINT(H) andalso | ||
?IS_UINT(Mi) andalso | ||
?IS_PNUM(S) | ||
) | ||
). | ||
|
||
-define(SECONDS_PER_MINUTE, 60). | ||
-define(SECONDS_PER_HOUR, 3600). | ||
|
||
-endif. | ||
|
||
-type encode_opt() :: {float_format, [float_format()]}. | ||
|
||
%% idem erlang:float_to_binary/2 options | ||
-type float_format() :: {scientific, Decimals :: 0..249} | ||
| {decimals, Decimals :: 0..253} | ||
| compact | ||
| short. | ||
|
||
-export([decode/1]). | ||
-export([decode/2]). | ||
-export([encode/1]). | ||
-export([encode/2]). | ||
-export([try_decode/1]). | ||
-export([try_decode/2]). | ||
-export([validate_opts/1]). | ||
|
||
|
||
%% ============================================================================= | ||
%% BONDY_CONFIG_MANAGER CALLBACKS | ||
%% API | ||
%% ============================================================================= | ||
-spec encode(any()) -> binary(). | ||
|
||
encode(Term) -> | ||
encode(Term, bondy_config:get(json, ?DEFAULT_ENCODE_OPTS)). | ||
|
||
|
||
-spec encode(any(), [encode_opt()]) -> iodata() | binary(). | ||
|
||
encode(Term, Opts) -> | ||
FloatOpts = float_opts(validate_opts(Opts)), | ||
do_encode(Term, FloatOpts). | ||
|
||
|
||
decode(Term) -> | ||
decode(Term, []). | ||
|
||
|
||
decode(Term, Opts) -> | ||
do_decode(Term, Opts). | ||
|
||
|
||
try_decode(Term) -> | ||
try_decode(Term, []). | ||
|
||
|
||
try_decode(Term, Opts) -> | ||
try | ||
{ok, decode(Term, Opts)} | ||
catch | ||
_:Reason -> | ||
{error, Reason} | ||
end. | ||
|
||
|
||
setup() -> | ||
jose:json_module(?MODULE). | ||
validate_opts(List) when is_list(List) -> | ||
lists:map(fun validate_opt/1, List). | ||
|
||
|
||
|
||
%% ============================================================================= | ||
%% JOSE CALLBACKS | ||
%% PRIVATE | ||
%% ============================================================================= | ||
|
||
|
||
encode(Bin) -> | ||
jsone:encode(Bin, [ | ||
%% @private | ||
float_opts(Opts) -> | ||
case lists:keyfind(float_format, 1, Opts) of | ||
{float_format, FloatOpts} when is_list(FloatOpts) -> | ||
FloatOpts; | ||
false -> | ||
?DEFAULT_FLOAT_FORMAT | ||
end. | ||
|
||
|
||
validate_opt({float_format, Opts}) -> | ||
{float_format, validate_float_opts(Opts)}; | ||
|
||
validate_opt({datetime_format, _Opts} = Term) -> | ||
%% TODO | ||
Term. | ||
|
||
|
||
validate_float_opts(Opts) -> | ||
lists:map(fun validate_float_opt/1, Opts). | ||
|
||
validate_float_opt({scientific, Decimals} = Term) | ||
when is_integer(Decimals), Decimals >= 0, Decimals =< 249 -> | ||
Term; | ||
|
||
validate_float_opt({scientific, Decimals}) | ||
when is_integer(Decimals), Decimals >= 0 -> | ||
%% Coerce to max | ||
{scientific, 249}; | ||
|
||
validate_float_opt({decimals, Decimals} = Term) | ||
when is_integer(Decimals), Decimals >= 0, Decimals =< 253 -> | ||
Term; | ||
|
||
validate_float_opt(compact = Term) -> | ||
Term; | ||
|
||
validate_float_opt(short = Term) -> | ||
Term; | ||
|
||
validate_float_opt(Arg) -> | ||
error(badarg, {float_format, Arg}). | ||
|
||
|
||
-if(?OTP_RELEASE >= 27). | ||
|
||
do_encode(Term, FloatOpts) -> | ||
json:encode(Term, fun | ||
(undefined, _Encode) -> | ||
<<"null">>; | ||
|
||
(Value, _Encode) when is_float(Value) -> | ||
float_to_binary(Value, FloatOpts); | ||
|
||
({{Y, M, D}, {H, Mi, S}}, _Encode) | ||
when ?IS_DATETIME(Y, M, D, H, Mi, S) -> | ||
encode_datetime({{Y, M, D}, {H, Mi, S}}); | ||
|
||
(Value, Encode) -> | ||
json:encode_value(Value, Encode) | ||
end). | ||
|
||
do_decode(Term, _Opts) -> | ||
json:decode(Term). | ||
|
||
-else. | ||
|
||
do_encode(Term, FloatOpts) -> | ||
jsone:encode(Term, [ | ||
undefined_as_null, | ||
{float_format, FloatOpts}, | ||
{datetime_format, iso8601}, | ||
{object_key_type, string} | ||
]). | ||
|
||
do_decode(Term, _Opts) -> | ||
jsone:decode(Term, [ | ||
undefined_as_null, | ||
{object_format, map} | ||
]). | ||
|
||
-endif. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
-if(?OTP_RELEASE >= 27). | ||
|
||
%% ============================================================================= | ||
%% PRIVATE - BORROWED FROM JSONE LIBRARY | ||
%% | ||
%% Copyright (c) 2013-2016, Takeru Ohta <[email protected]> | ||
%% | ||
%% The MIT License | ||
%% | ||
%% Permission is hereby granted, free of charge, to any person obtaining a copy | ||
%% of this software and associated documentation files (the "Software"), to deal | ||
%% in the Software without restriction, including without limitation the rights | ||
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
%% copies of the Software, and to permit persons to whom the Software is | ||
%% furnished to do so, subject to the following conditions: | ||
%% | ||
%% The above copyright notice and this permission notice shall be included in | ||
%% all copies or substantial portions of the Software. | ||
%% | ||
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
%% THE SOFTWARE. | ||
%% | ||
%% ============================================================================= | ||
|
||
-spec encode_datetime(calendar:datetime()) -> iodata(). | ||
|
||
encode_datetime({{Y, M, D}, {H, Mi, S}}) -> | ||
[ | ||
format_year(Y), $-, | ||
format2digit(M), $-, | ||
format2digit(D), $T, | ||
format2digit(H), $:, | ||
format2digit(Mi), $:, | ||
format_seconds(S), $Z | ||
]. | ||
|
||
|
||
-spec format_year(non_neg_integer()) -> iodata(). | ||
|
||
format_year(Y) when Y > 999 -> | ||
integer_to_binary(Y); | ||
|
||
format_year(Y) -> | ||
B = integer_to_binary(Y), | ||
[lists:duplicate(4 - byte_size(B), $0) | B]. | ||
|
||
|
||
-spec format2digit(non_neg_integer()) -> iolist(). | ||
|
||
format2digit(0) -> | ||
"00"; | ||
format2digit(1) -> | ||
"01"; | ||
format2digit(2) -> | ||
"02"; | ||
format2digit(3) -> | ||
"03"; | ||
format2digit(4) -> | ||
"04"; | ||
format2digit(5) -> | ||
"05"; | ||
format2digit(6) -> | ||
"06"; | ||
format2digit(7) -> | ||
"07"; | ||
format2digit(8) -> | ||
"08"; | ||
format2digit(9) -> | ||
"09"; | ||
format2digit(X) -> | ||
integer_to_list(X). | ||
|
||
|
||
-spec format_seconds(non_neg_integer() | float()) -> iolist(). | ||
|
||
format_seconds(S) when is_integer(S) -> | ||
format2digit(S); | ||
|
||
format_seconds(S) when is_float(S) -> | ||
io_lib:format("~6.3.0f", [S]). | ||
|
||
|
||
%% -spec format_tz_(integer()) -> iolist(). | ||
%% format_tz_(S) -> | ||
%% H = S div ?SECONDS_PER_HOUR, | ||
%% S1 = S rem ?SECONDS_PER_HOUR, | ||
%% M = S1 div ?SECONDS_PER_MINUTE, | ||
%% [format2digit(H), $:, format2digit(M)]. | ||
|
||
decode(Bin) -> | ||
jsone:decode(Bin, [undefined_as_null]). | ||
-endif. |
Oops, something went wrong.