Packages

Encode and decode JSON from and to Gleam types

Current section

Files

Jump to
convert_json src convert@json.erl
Raw

src/convert@json.erl

-module(convert@json).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([encode_value/1, json_encode/2, decode_value/1, json_decode/1]).
-file("/home/runner/work/convert_json/convert_json/src/convert/json.gleam", 29).
-spec encode_value(convert:glitr_value()) -> gleam@json:json().
encode_value(Val) ->
case Val of
{string_value, V} ->
gleam@json:string(V);
{bool_value, V@1} ->
gleam@json:bool(V@1);
{float_value, V@2} ->
gleam@json:float(V@2);
{int_value, V@3} ->
gleam@json:int(V@3);
{list_value, Vals} ->
gleam@json:array(Vals, fun encode_value/1);
{dict_value, V@4} ->
gleam@json:array(
begin
_pipe = V@4,
maps:to_list(_pipe)
end,
fun(Keyval) ->
gleam@json:array(
[erlang:element(1, Keyval), erlang:element(2, Keyval)],
fun encode_value/1
)
end
);
{object_value, V@5} ->
gleam@json:object(
gleam@list:map(
V@5,
fun(F) ->
{erlang:element(1, F),
encode_value(erlang:element(2, F))}
end
)
);
{optional_value, V@6} ->
gleam@json:nullable(V@6, fun encode_value/1);
{result_value, V@7} ->
case V@7 of
{ok, Res} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"ok"/utf8>>)},
{<<"value"/utf8>>, encode_value(Res)}]
);
{error, Err} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"error"/utf8>>)},
{<<"value"/utf8>>, encode_value(Err)}]
)
end;
{enum_value, Variant, V@8} ->
gleam@json:object(
[{<<"variant"/utf8>>, gleam@json:string(Variant)},
{<<"value"/utf8>>, encode_value(V@8)}]
);
_ ->
gleam@json:null()
end.
-file("/home/runner/work/convert_json/convert_json/src/convert/json.gleam", 11).
-spec json_encode(GPR, convert:converter(GPR)) -> gleam@json:json().
json_encode(Value, Converter) ->
_pipe = Value,
_pipe@1 = (convert:encode(Converter))(_pipe),
encode_value(_pipe@1).
-file("/home/runner/work/convert_json/convert_json/src/convert/json.gleam", 68).
-spec decode_value(convert:glitr_type()) -> fun((gleam@dynamic:dynamic_()) -> {ok,
convert:glitr_value()} |
{error, list(gleam@dynamic:decode_error())}).
decode_value(Of) ->
case Of of
string ->
fun(Val) -> _pipe = Val,
_pipe@1 = gleam@dynamic:string(_pipe),
gleam@result:map(
_pipe@1,
fun(Field@0) -> {string_value, Field@0} end
) end;
bool ->
fun(Val@1) -> _pipe@2 = Val@1,
_pipe@3 = gleam@dynamic:bool(_pipe@2),
gleam@result:map(
_pipe@3,
fun(Field@0) -> {bool_value, Field@0} end
) end;
float ->
fun(Val@2) -> _pipe@4 = Val@2,
_pipe@5 = gleam@dynamic:float(_pipe@4),
gleam@result:map(
_pipe@5,
fun(Field@0) -> {float_value, Field@0} end
) end;
int ->
fun(Val@3) -> _pipe@6 = Val@3,
_pipe@7 = gleam@dynamic:int(_pipe@6),
gleam@result:map(
_pipe@7,
fun(Field@0) -> {int_value, Field@0} end
) end;
{list, El} ->
fun(Val@4) -> _pipe@8 = Val@4,
_pipe@9 = (gleam@dynamic:list(fun gleam@dynamic:dynamic/1))(
_pipe@8
),
_pipe@11 = gleam@result:then(
_pipe@9,
fun(Val_list) ->
gleam@list:fold(
Val_list,
{ok, []},
fun(Result, List_el) ->
case {Result,
begin
_pipe@10 = List_el,
(decode_value(El))(_pipe@10)
end} of
{{ok, Result_list}, {ok, Jval}} ->
{ok, [Jval | Result_list]};
{{ok, _}, {error, Errs}} ->
{error, Errs};
{{error, Errs}, {ok, _}} ->
{error, Errs};
{{error, Errs@1}, {error, New_errs}} ->
{error, lists:append(Errs@1, New_errs)}
end
end
)
end
),
_pipe@12 = gleam@result:map(_pipe@11, fun lists:reverse/1),
gleam@result:map(
_pipe@12,
fun(Field@0) -> {list_value, Field@0} end
) end;
{dict, K, V} ->
fun(Val@5) -> _pipe@13 = Val@5,
_pipe@14 = (gleam@dynamic:list(
gleam@dynamic:list(
gleam@dynamic:any([decode_value(K), decode_value(V)])
)
))(_pipe@13),
_pipe@15 = gleam@result:then(
_pipe@14,
fun(_capture) ->
gleam@list:fold(
_capture,
{ok, []},
fun(Result@1, El@1) -> case {Result@1, El@1} of
{{ok, Vals}, [First, Second | _]} ->
{ok, [{First, Second} | Vals]};
{{ok, _}, _} ->
{error,
[{decode_error,
<<"2 elements"/utf8>>,
<<"0 or 1"/utf8>>,
[]}]};
{{error, Errs@2}, [_, _ | _]} ->
{error, Errs@2};
{{error, Errs@3}, _} ->
{error,
[{decode_error,
<<"2 elements"/utf8>>,
<<"0 or 1"/utf8>>,
[]} |
Errs@3]}
end end
)
end
),
_pipe@16 = gleam@result:map(_pipe@15, fun maps:from_list/1),
gleam@result:map(
_pipe@16,
fun(Field@0) -> {dict_value, Field@0} end
) end;
{object, Fields} ->
fun(Val@6) ->
_pipe@18 = gleam@list:fold(
Fields,
{ok, []},
fun(Result@2, F) ->
case {Result@2,
begin
_pipe@17 = Val@6,
(gleam@dynamic:field(
erlang:element(1, F),
decode_value(erlang:element(2, F))
))(_pipe@17)
end} of
{{ok, Field_list}, {ok, Jval@1}} ->
{ok,
[{erlang:element(1, F), Jval@1} |
Field_list]};
{{ok, _}, {error, Errs@4}} ->
{error, Errs@4};
{{error, Errs@4}, {ok, _}} ->
{error, Errs@4};
{{error, Errs@5}, {error, New_errs@1}} ->
{error, lists:append(Errs@5, New_errs@1)}
end
end
),
_pipe@19 = gleam@result:map(_pipe@18, fun lists:reverse/1),
gleam@result:map(
_pipe@19,
fun(Field@0) -> {object_value, Field@0} end
)
end;
{optional, Of@1} ->
fun(Val@7) -> _pipe@20 = Val@7,
_pipe@21 = (gleam@dynamic:optional(decode_value(Of@1)))(
_pipe@20
),
gleam@result:map(
_pipe@21,
fun(Field@0) -> {optional_value, Field@0} end
) end;
{result, Res, Err} ->
fun(Val@8) ->
gleam@result:'try'(
begin
_pipe@22 = Val@8,
(gleam@dynamic:field(
<<"type"/utf8>>,
fun gleam@dynamic:string/1
))(_pipe@22)
end,
fun(Type_val) -> case Type_val of
<<"ok"/utf8>> ->
_pipe@23 = Val@8,
_pipe@24 = (gleam@dynamic:field(
<<"value"/utf8>>,
decode_value(Res)
))(_pipe@23),
_pipe@25 = gleam@result:map(
_pipe@24,
fun(Field@0) -> {ok, Field@0} end
),
gleam@result:map(
_pipe@25,
fun(Field@0) -> {result_value, Field@0} end
);
<<"error"/utf8>> ->
_pipe@26 = Val@8,
_pipe@27 = (gleam@dynamic:field(
<<"value"/utf8>>,
decode_value(Err)
))(_pipe@26),
_pipe@28 = gleam@result:map(
_pipe@27,
fun(Field@0) -> {error, Field@0} end
),
gleam@result:map(
_pipe@28,
fun(Field@0) -> {result_value, Field@0} end
);
Other ->
{error,
[{decode_error,
<<"'ok' or 'error'"/utf8>>,
Other,
[<<"type"/utf8>>]}]}
end end
)
end;
{enum, Variants} ->
fun(Val@9) ->
gleam@result:'try'(
begin
_pipe@29 = Val@9,
(gleam@dynamic:field(
<<"variant"/utf8>>,
fun gleam@dynamic:string/1
))(_pipe@29)
end,
fun(Variant_name) ->
gleam@result:'try'(
begin
_pipe@30 = gleam@list:key_find(
Variants,
Variant_name
),
gleam@result:replace_error(
_pipe@30,
[{decode_error,
<<"One of: "/utf8,
(begin
_pipe@31 = Variants,
_pipe@32 = gleam@list:map(
_pipe@31,
fun(V@1) ->
erlang:element(
1,
V@1
)
end
),
gleam@string:join(
_pipe@32,
<<"/"/utf8>>
)
end)/binary>>,
Variant_name,
[<<"variant"/utf8>>]}]
)
end,
fun(Variant_def) ->
gleam@result:'try'(
begin
_pipe@33 = Val@9,
_pipe@34 = (gleam@dynamic:field(
<<"value"/utf8>>,
fun gleam@dynamic:dynamic/1
))(_pipe@33),
gleam@result:then(
_pipe@34,
decode_value(Variant_def)
)
end,
fun(Variant_value) ->
{ok,
{enum_value,
Variant_name,
Variant_value}}
end
)
end
)
end
)
end;
_ ->
fun(_) -> {ok, null_value} end
end.
-file("/home/runner/work/convert_json/convert_json/src/convert/json.gleam", 16).
-spec json_decode(convert:converter(GPT)) -> fun((gleam@dynamic:dynamic_()) -> {ok,
GPT} |
{error, list(gleam@dynamic:decode_error())}).
json_decode(Converter) ->
fun(Value) -> _pipe = Value,
_pipe@1 = (decode_value(convert:type_def(Converter)))(_pipe),
gleam@result:then(_pipe@1, convert:decode(Converter)) end.