Current section
Files
Jump to
Current section
Files
src/simplejson@internal@pointer.erl
-module(simplejson@internal@pointer).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/simplejson/internal/pointer.gleam").
-export([jsonpath/2, jsonpointer/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.
?MODULEDOC(false).
-file("src/simplejson/internal/pointer.gleam", 14).
?DOC(false).
-spec jsonpath(simplejson@jsonvalue:json_value(), binary()) -> {ok,
simplejson@jsonvalue:json_value()} |
{error, simplejson@jsonvalue:json_path_error()}.
jsonpath(Json, Jsonpath) ->
gleam@list:try_fold(
gleam@string:split(Jsonpath, <<"."/utf8>>),
Json,
fun(Current_json, Path_segment) -> case Path_segment of
<<""/utf8>> ->
{ok, Current_json};
<<"#"/utf8, Array_index/binary>> ->
case gleam_stdlib:parse_int(Array_index) of
{ok, Index} ->
case Current_json of
{json_array, Json_list} ->
case gleam_stdlib:map_get(Json_list, Index) of
{ok, Found_json} ->
{ok, Found_json};
{error, _} ->
{error, path_not_found}
end;
_ ->
{error, path_not_found}
end;
{error, _} ->
{error, invalid_json_path}
end;
_ ->
case Current_json of
{json_object, Found_dict} ->
case gleam_stdlib:map_get(Found_dict, Path_segment) of
{ok, Json_found_at_path} ->
{ok, Json_found_at_path};
{error, _} ->
{error, path_not_found}
end;
_ ->
{error, path_not_found}
end
end end
).
-file("src/simplejson/internal/pointer.gleam", 52).
?DOC(false).
-spec jsonpointer(simplejson@jsonvalue:json_value(), binary()) -> {ok,
simplejson@jsonvalue:json_value()} |
{error, simplejson@jsonvalue:json_path_error()}.
jsonpointer(Json, Jsonpointer) ->
{Split, Fragment} = case gleam@string:split(Jsonpointer, <<"/"/utf8>>) of
[] ->
{[], false};
[<<""/utf8>>] ->
{[], false};
[<<"#"/utf8>>] ->
{[], false};
[<<"#"/utf8>> | Rest] ->
{Rest, true};
[_ | Rest@1] ->
{Rest@1, false}
end,
gleam@bool:guard(
Split =:= [],
{ok, Json},
fun() ->
gleam@list:try_fold(
Split,
Json,
fun(Current_json, Path_segment) ->
Path_segment@1 = begin
_pipe = Path_segment,
_pipe@1 = gleam@string:replace(
_pipe,
<<"~1"/utf8>>,
<<"/"/utf8>>
),
_pipe@2 = gleam@string:replace(
_pipe@1,
<<"~0"/utf8>>,
<<"~"/utf8>>
),
_pipe@3 = gleam@string:replace(
_pipe@2,
<<"\\\\"/utf8>>,
<<"\\"/utf8>>
),
gleam@string:replace(
_pipe@3,
<<"\\\""/utf8>>,
<<"\""/utf8>>
)
end,
gleam@result:'try'(case Fragment of
true ->
_pipe@4 = gleam_stdlib:percent_decode(
Path_segment@1
),
gleam@result:replace_error(
_pipe@4,
{parse_error,
<<"Invalid path "/utf8,
Path_segment@1/binary>>}
);
false ->
{ok, Path_segment@1}
end, fun(Path_segment@2) -> case Current_json of
{json_object, Found_dict} ->
case gleam_stdlib:map_get(
Found_dict,
Path_segment@2
) of
{ok, Json_found_at_path} ->
{ok, Json_found_at_path};
{error, _} ->
{error, path_not_found}
end;
{json_array, Found_dict@1} ->
gleam@result:'try'(
begin
_pipe@5 = gleam_stdlib:parse_int(
Path_segment@2
),
gleam@result:replace_error(
_pipe@5,
{parse_error,
<<Path_segment@2/binary,
" is not a number"/utf8>>}
)
end,
fun(I) ->
case gleam_stdlib:map_get(
Found_dict@1,
I
) of
{ok, Json_found_at_path@1} ->
{ok, Json_found_at_path@1};
{error, _} ->
{error, path_not_found}
end
end
);
_ ->
{error, path_not_found}
end end)
end
)
end
).