Current section

Files

Jump to
glee src utilities.erl
Raw

src/utilities.erl

-module(utilities).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([encode_string/1, encode_int/1, encode_float/1, encode_bool/1, encode_list/1, encode_object/1, create_json_object/2, create_json_object_from_pairs/1, parse_json_bool/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/utilities.gleam", 10).
?DOC(" Encode a string value into a JSON string\n").
-spec encode_string(binary()) -> binary().
encode_string(Value) ->
Escaped = gleam@string:replace(Value, <<"\""/utf8>>, <<"\\\""/utf8>>),
<<<<"\""/utf8, Escaped/binary>>/binary, "\""/utf8>>.
-file("src/utilities.gleam", 16).
?DOC(" Encode an int (interger) value into a JSON string\n").
-spec encode_int(integer()) -> binary().
encode_int(Value) ->
erlang:integer_to_binary(Value).
-file("src/utilities.gleam", 21).
?DOC(" Encode a float (floating point) value into a JSON string\n").
-spec encode_float(float()) -> binary().
encode_float(Value) ->
gleam_stdlib:float_to_string(Value).
-file("src/utilities.gleam", 26).
?DOC(" Encode a boolean value into a JSON string\n").
-spec encode_bool(boolean()) -> binary().
encode_bool(Value) ->
case Value of
true ->
<<"true"/utf8>>;
false ->
<<"false"/utf8>>
end.
-file("src/utilities.gleam", 34).
?DOC(" Encode a list of values into a JSON array string\n").
-spec encode_list(list(binary())) -> binary().
encode_list(Values) ->
Joined_values = gleam@list:fold(
Values,
<<""/utf8>>,
fun(Acc, Value) -> case Acc of
<<""/utf8>> ->
Value;
_ ->
<<<<Acc/binary, ","/utf8>>/binary, Value/binary>>
end end
),
<<<<"["/utf8, Joined_values/binary>>/binary, "]"/utf8>>.
-file("src/utilities.gleam", 47).
?DOC(" Encode a map of string keys and string values into a JSON object string\n").
-spec encode_object(gleam@dict:dict(binary(), binary())) -> binary().
encode_object(Values) ->
Entries = maps:to_list(Values),
Joined_entries = gleam@list:fold(
Entries,
<<""/utf8>>,
fun(Acc, Entry) ->
{Key, Value} = Entry,
Formatted_entry = <<<<(encode_string(Key))/binary, ":"/utf8>>/binary,
Value/binary>>,
case Acc of
<<""/utf8>> ->
Formatted_entry;
_ ->
<<<<Acc/binary, ","/utf8>>/binary, Formatted_entry/binary>>
end
end
),
<<<<"{"/utf8, Joined_entries/binary>>/binary, "}"/utf8>>.
-file("src/utilities.gleam", 65).
?DOC(" Create a simple JSON object with a single key-value pair\n").
-spec create_json_object(binary(), binary()) -> binary().
create_json_object(Key, Value) ->
Values = maps:new(),
Values@1 = gleam@dict:insert(Values, Key, Value),
encode_object(Values@1).
-file("src/utilities.gleam", 72).
?DOC(" Create a JSON object from multiple key-value pairs\n").
-spec create_json_object_from_pairs(list({binary(), binary()})) -> binary().
create_json_object_from_pairs(Pairs) ->
Values = maps:from_list(Pairs),
encode_object(Values).
-file("src/utilities.gleam", 79).
?DOC(
" Parse a JSON string and return\n"
" the boolean value of a given field.\n"
).
-spec parse_json_bool(binary(), binary()) -> {ok, boolean()} | {error, binary()}.
parse_json_bool(Json, Field) ->
Json_decoder = begin
gleam@dynamic@decode:field(
Field,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
fun(Json@1) -> gleam@dynamic@decode:success(Json@1) end
)
end,
Json_result = begin
_pipe = Json,
gleam@json:parse(_pipe, Json_decoder)
end,
case Json_result of
{ok, Value} ->
{ok, Value};
{error, _} ->
{error, <<"Failed to parse boolean"/utf8>>}
end.