Current section

Files

Jump to
glee src glee.erl
Raw

src/glee.erl

-module(glee).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([parse_json_string/2, parse_json_float/2, parse_json_int/2, extract_value_from_json/3, 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]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Glee\n"
" A simple way to parse JSON without\n"
" having to implement the JSON module.\n"
" +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"
" This is my first time using Gleam, so\n"
" I'm sure there are some things that\n"
" could be done better.\n"
).
-file("src/glee.gleam", 19).
?DOC(
" Parse a JSON string and return\n"
" the string value of a given field.\n"
).
-spec parse_json_string(binary(), binary()) -> {ok, binary()} |
{error, binary()}.
parse_json_string(Json, Field) ->
Json_decoder = begin
gleam@dynamic@decode:field(
Field,
{decoder, fun gleam@dynamic@decode:decode_string/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, Json_result@1} ->
{ok, Json_result@1};
{error, _} ->
{error, <<"Failed to parse string"/utf8>>}
end.
-file("src/glee.gleam", 40).
?DOC(
" Parse a JSON string and return\n"
" the float value of a given field.\n"
).
-spec parse_json_float(binary(), binary()) -> {ok, float()} | {error, binary()}.
parse_json_float(Json, Field) ->
Json_decoder = begin
gleam@dynamic@decode:field(
Field,
{decoder, fun gleam@dynamic@decode:decode_float/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 float"/utf8>>}
end.
-file("src/glee.gleam", 63).
?DOC(
" Parse a JSON string and return\n"
" the int value of a given field.\n"
).
-spec parse_json_int(binary(), binary()) -> {ok, integer()} | {error, binary()}.
parse_json_int(Json, Field) ->
Json_decoder = begin
gleam@dynamic@decode:field(
Field,
{decoder, fun gleam@dynamic@decode:decode_int/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 integer"/utf8>>}
end.
-file("src/glee.gleam", 86).
?DOC(
" Extract a value from a JSON string\n"
" by specifying the parent and key.\n"
).
-spec extract_value_from_json(binary(), binary(), binary()) -> binary().
extract_value_from_json(Json, Parent, Key) ->
Parent_pattern = <<<<"\""/utf8, Parent/binary>>/binary, "\":{"/utf8>>,
Parent_parts = gleam@string:split(Json, Parent_pattern),
case Parent_parts of
[_, Parent_content | _] ->
Key_pattern = <<<<"\""/utf8, Key/binary>>/binary, "\":"/utf8>>,
Key_parts = gleam@string:split(Parent_content, Key_pattern),
case Key_parts of
[_, Value_part | _] ->
By_comma = gleam@string:split(Value_part, <<","/utf8>>),
By_brace = gleam@string:split(Value_part, <<"}"/utf8>>),
Value_by_comma = case By_comma of
[First | _] ->
First;
_ ->
Value_part
end,
Value_by_brace = case By_brace of
[First@1 | _] ->
First@1;
_ ->
Value_part
end,
Value = case string:length(Value_by_comma) < string:length(
Value_by_brace
) of
true ->
Value_by_comma;
false ->
Value_by_brace
end,
Value@1 = gleam@string:trim(Value),
Value@2 = case gleam_stdlib:string_starts_with(
Value@1,
<<"\""/utf8>>
)
andalso gleam_stdlib:string_ends_with(
Value@1,
<<"\""/utf8>>
) of
true ->
gleam@string:slice(
Value@1,
1,
string:length(Value@1) - 2
);
false ->
Value@1
end,
Value@2;
_ ->
<<"Unknown"/utf8>>
end;
_ ->
<<"Unknown"/utf8>>
end.
-file("src/glee.gleam", 140).
?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/glee.gleam", 146).
?DOC(" Encode an int value into a JSON string\n").
-spec encode_int(integer()) -> binary().
encode_int(Value) ->
erlang:integer_to_binary(Value).
-file("src/glee.gleam", 151).
?DOC(" Encode a float value into a JSON string\n").
-spec encode_float(float()) -> binary().
encode_float(Value) ->
gleam_stdlib:float_to_string(Value).
-file("src/glee.gleam", 156).
?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/glee.gleam", 164).
?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/glee.gleam", 177).
?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/glee.gleam", 195).
?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/glee.gleam", 202).
?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).