Packages

Native gleam json parser/generator with jsonpath querying

Current section

Files

Jump to
simplejson src simplejson@internal@stringify.erl
Raw

src/simplejson@internal@stringify.erl

-module(simplejson@internal@stringify).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/simplejson/internal/stringify.gleam").
-export([dict_to_ordered_list/1, to_string/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(false).
-file("src/simplejson/internal/stringify.gleam", 41).
?DOC(false).
-spec dict_to_ordered_list(
gleam@dict:dict(integer(), simplejson@jsonvalue:json_value())
) -> list(simplejson@jsonvalue:json_value()).
dict_to_ordered_list(D) ->
_pipe = maps:to_list(D),
_pipe@1 = gleam@list:sort(
_pipe,
fun(E1, E2) ->
gleam@int:compare(erlang:element(1, E1), erlang:element(1, E2))
end
),
gleam@list:map(_pipe@1, fun(E) -> erlang:element(2, E) end).
-file("src/simplejson/internal/stringify.gleam", 47).
?DOC(false).
-spec encode_int(integer()) -> binary().
encode_int(N) ->
erlang:integer_to_binary(N).
-file("src/simplejson/internal/stringify.gleam", 51).
?DOC(false).
-spec encode_float(float()) -> binary().
encode_float(N) ->
gleam_stdlib:float_to_string(N).
-file("src/simplejson/internal/stringify.gleam", 80).
?DOC(false).
-spec encode_char(binary()) -> binary().
encode_char(Char) ->
case Char of
<<"\r\n"/utf8>> ->
<<"\\r\\n"/utf8>>;
<<"\x{00}"/utf8>> ->
<<"\\u0000"/utf8>>;
<<"\x{01}"/utf8>> ->
<<"\\u0001"/utf8>>;
<<"\x{02}"/utf8>> ->
<<"\\u0002"/utf8>>;
<<"\x{03}"/utf8>> ->
<<"\\u0003"/utf8>>;
<<"\x{04}"/utf8>> ->
<<"\\u0004"/utf8>>;
<<"\x{05}"/utf8>> ->
<<"\\u0005"/utf8>>;
<<"\x{06}"/utf8>> ->
<<"\\u0006"/utf8>>;
<<"\x{07}"/utf8>> ->
<<"\\u0007"/utf8>>;
<<"\x{08}"/utf8>> ->
<<"\\b"/utf8>>;
<<"\x{09}"/utf8>> ->
<<"\\t"/utf8>>;
<<"\x{0A}"/utf8>> ->
<<"\\n"/utf8>>;
<<"\x{0B}"/utf8>> ->
<<"\\u000B"/utf8>>;
<<"\x{0C}"/utf8>> ->
<<"\\f"/utf8>>;
<<"\x{0D}"/utf8>> ->
<<"\\r"/utf8>>;
<<"\x{0E}"/utf8>> ->
<<"\\u000E"/utf8>>;
<<"\x{0F}"/utf8>> ->
<<"\\u000F"/utf8>>;
<<"\x{10}"/utf8>> ->
<<"\\u0010"/utf8>>;
<<"\x{11}"/utf8>> ->
<<"\\u0011"/utf8>>;
<<"\x{12}"/utf8>> ->
<<"\\u0012"/utf8>>;
<<"\x{13}"/utf8>> ->
<<"\\u0013"/utf8>>;
<<"\x{14}"/utf8>> ->
<<"\\u0014"/utf8>>;
<<"\x{15}"/utf8>> ->
<<"\\u0015"/utf8>>;
<<"\x{16}"/utf8>> ->
<<"\\u0016"/utf8>>;
<<"\x{17}"/utf8>> ->
<<"\\u0017"/utf8>>;
<<"\x{18}"/utf8>> ->
<<"\\u0018"/utf8>>;
<<"\x{19}"/utf8>> ->
<<"\\u0019"/utf8>>;
<<"\x{1A}"/utf8>> ->
<<"\\u001A"/utf8>>;
<<"\x{1B}"/utf8>> ->
<<"\\u001B"/utf8>>;
<<"\x{1C}"/utf8>> ->
<<"\\u001C"/utf8>>;
<<"\x{1D}"/utf8>> ->
<<"\\u001D"/utf8>>;
<<"\x{1E}"/utf8>> ->
<<"\\u001E"/utf8>>;
<<"\x{1F}"/utf8>> ->
<<"\\u001F"/utf8>>;
<<"\""/utf8>> ->
<<"\\\""/utf8>>;
<<"\\"/utf8>> ->
<<"\\\\"/utf8>>;
_ ->
Char
end.
-file("src/simplejson/internal/stringify.gleam", 73).
?DOC(false).
-spec encode_string(binary(), binary()) -> binary().
encode_string(Str, Acc) ->
case gleam_stdlib:string_pop_grapheme(Str) of
{ok, {Char, Rest}} ->
encode_string(Rest, <<Acc/binary, (encode_char(Char))/binary>>);
{error, _} ->
Acc
end.
-file("src/simplejson/internal/stringify.gleam", 55).
?DOC(false).
-spec encode_object(
gleam@dict:dict(binary(), simplejson@jsonvalue:json_value())
) -> binary().
encode_object(O) ->
_pipe = maps:to_list(O),
gleam@list:fold(
_pipe,
<<""/utf8>>,
fun(Acc, Entry) ->
{Key, Value} = Entry,
<<<<<<<<<<Acc/binary, ((case Acc of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<","/utf8>>
end))/binary>>/binary, "\""/utf8>>/binary, (encode_string(
Key,
<<""/utf8>>
))/binary>>/binary, "\":"/utf8>>/binary, (create_string(
Value,
<<""/utf8>>
))/binary>>
end
).
-file("src/simplejson/internal/stringify.gleam", 16).
?DOC(false).
-spec create_string(simplejson@jsonvalue:json_value(), binary()) -> binary().
create_string(Json, Acc) ->
case Json of
{json_bool, B} ->
case B of
true ->
<<Acc/binary, "true"/utf8>>;
false ->
<<Acc/binary, "false"/utf8>>
end;
{json_string, S} ->
<<<<<<Acc/binary, "\""/utf8>>/binary,
(encode_string(S, <<""/utf8>>))/binary>>/binary,
"\""/utf8>>;
{json_array, L} ->
<<<<<<Acc/binary, "["/utf8>>/binary,
(encode_list(dict_to_ordered_list(L), <<""/utf8>>))/binary>>/binary,
"]"/utf8>>;
json_null ->
<<Acc/binary, "null"/utf8>>;
{json_number, _, _, {some, S@1}} ->
<<Acc/binary, S@1/binary>>;
{json_number, {some, I}, _, _} ->
<<Acc/binary, (encode_int(I))/binary>>;
{json_number, _, {some, F}, _} ->
<<Acc/binary, (encode_float(F))/binary>>;
{json_number, none, none, _} ->
<<Acc/binary, "0"/utf8>>;
{json_object, O} ->
<<<<<<Acc/binary, "{"/utf8>>/binary, (encode_object(O))/binary>>/binary,
"}"/utf8>>
end.
-file("src/simplejson/internal/stringify.gleam", 12).
?DOC(false).
-spec to_string(simplejson@jsonvalue:json_value()) -> binary().
to_string(Json) ->
create_string(Json, <<""/utf8>>).
-file("src/simplejson/internal/stringify.gleam", 121).
?DOC(false).
-spec encode_list(list(simplejson@jsonvalue:json_value()), binary()) -> binary().
encode_list(L, Acc) ->
case L of
[{json_array, _} = El | Rest] ->
encode_list(Rest, <<<<Acc/binary, ((case Acc of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<","/utf8>>
end))/binary>>/binary, (create_string(El, <<""/utf8>>))/binary>>);
[{json_bool, _} = El | Rest] ->
encode_list(Rest, <<<<Acc/binary, ((case Acc of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<","/utf8>>
end))/binary>>/binary, (create_string(El, <<""/utf8>>))/binary>>);
[json_null = El | Rest] ->
encode_list(Rest, <<<<Acc/binary, ((case Acc of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<","/utf8>>
end))/binary>>/binary, (create_string(El, <<""/utf8>>))/binary>>);
[{json_object, _} = El | Rest] ->
encode_list(Rest, <<<<Acc/binary, ((case Acc of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<","/utf8>>
end))/binary>>/binary, (create_string(El, <<""/utf8>>))/binary>>);
[{json_string, _} = El | Rest] ->
encode_list(Rest, <<<<Acc/binary, ((case Acc of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<","/utf8>>
end))/binary>>/binary, (create_string(El, <<""/utf8>>))/binary>>);
[{json_number, _, _, _} = El | Rest] ->
encode_list(Rest, <<<<Acc/binary, ((case Acc of
<<""/utf8>> ->
<<""/utf8>>;
_ ->
<<","/utf8>>
end))/binary>>/binary, (create_string(El, <<""/utf8>>))/binary>>);
[] ->
Acc
end.