Packages

A fast, spec compliant, generic JSON parser and encoder in Gleam

Current section

Files

Jump to
gj gen src gj_encode.erl
Raw

gen/src/gj_encode.erl

-module(gj_encode).
-compile(no_auto_import).
-export([encode/1]).
encode(J) ->
case J of
{boolean, true} ->
<<"true"/utf8>>;
{boolean, false} ->
<<"false"/utf8>>;
null ->
<<"null"/utf8>>;
{array, Vals} ->
gleam@string:concat(
[<<"["/utf8>>,
gleam@string:join(
gleam@list:map(Vals, fun encode/1),
<<","/utf8>>
),
<<"]"/utf8>>]
);
{object, Vals@1} ->
gleam@string:concat(
[<<"{"/utf8>>,
gleam@string:join(
gleam@list:map(Vals@1, fun(Kv) -> {Key, Val} = Kv,
gleam@string:concat(
[encode({string, Key}),
<<":"/utf8>>,
encode(Val)]
) end),
<<","/utf8>>
),
<<"}"/utf8>>]
);
{number, F} ->
gleam@float:to_string(F);
{string, S} ->
gleam@string:concat([<<"\""/utf8>>, S, <<"\""/utf8>>])
end.