Current section
Files
Jump to
Current section
Files
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]).
-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", 15).
?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", 36).
?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", 59).
?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", 82).
?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.