-module(glee). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([parse_json_string/2, parse_json_number/2, float_to_string/1, 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", 16). ?DOC( " Parse a JSON string and return\n" " the string value of a given field.\n" ). -spec parse_json_string(binary(), binary()) -> 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} -> Json_result@1; {error, _} -> <<"Error"/utf8>> end. -file("src/glee.gleam", 39). ?DOC( " Parse the JSON string and return\n" " the value of the given field.\n" " Parse a JSON string and return\n" " the float value of a given field.\n" ). -spec parse_json_number(binary(), binary()) -> {ok, float()} | {error, binary()}. parse_json_number(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 number"/utf8>>} end. -file("src/glee.gleam", 61). ?DOC(" Convert a float to a string with 1 decimal place\n"). -spec float_to_string(float()) -> binary(). float_to_string(Value) -> _pipe = gleam_stdlib:float_to_string(Value), _pipe@1 = gleam@string:split(_pipe, <<"."/utf8>>), (fun(Parts) -> case Parts of [Whole, Decimal] -> Short_decimal = case string:length(Decimal) of 0 -> Decimal; _ -> gleam@string:slice(Decimal, 0, 1) end, <<<>/binary, Short_decimal/binary>>; _ -> gleam_stdlib:float_to_string(Value) end end)(_pipe@1). -file("src/glee.gleam", 87). ?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.