Packages

A plug-and-play library for neural networks written in Gleam

Current section

Files

Jump to
gleam_synapses gen src gleam_synapses@model@edited_jsone.erl
Raw

gen/src/gleam_synapses@model@edited_jsone.erl

-module(gleam_synapses@model@edited_jsone).
-compile(no_auto_import).
-export([default_options/0, decode/1, decode_with_options/2, string/1, int/1, float/1, array/2, bool/1, null/0, object/1, to_dynamic/1, encode/1]).
-export_type([duplicate_map_keys/0, options/0, json_value/0, json_number/0, decimals/0, float_format/0]).
-type duplicate_map_keys() :: first | last.
-type options() :: {options, boolean(), boolean(), duplicate_map_keys()}.
-type json_value() :: {json_string, binary()} |
{json_number, json_number()} |
{json_array, list(json_value())} |
{json_bool, boolean()} |
json_null |
{json_object, list({binary(), json_value()})}.
-type json_number() :: {json_int, integer()} | {json_float, float()}.
-type decimals() :: {decimals, integer()} | compact.
-type float_format() :: {float_format, list(decimals())}.
-spec default_options() -> options().
default_options() ->
{options, false, false, first}.
-spec transform_options(options()) -> gleam@dynamic:dynamic().
transform_options(Options) ->
{options, Allow_ctrl_chars, Reject_invalid_utf8, Duplicate_map_keys} = Options,
Duplicate_map_keys_dynamic = gleam@dynamic:from(
{gleam@atom:create_from_string(<<"duplicate_map_keys"/utf8>>),
case Duplicate_map_keys of
first ->
gleam@atom:create_from_string(<<"first"/utf8>>);
last ->
gleam@atom:create_from_string(<<"last"/utf8>>)
end}
),
Allow_ctrl_chars_dynamic = gleam@dynamic:from(
{gleam@atom:create_from_string(<<"allow_ctrl_chars"/utf8>>),
Allow_ctrl_chars}
),
Reject_invalid_utf8_dynamic = gleam@dynamic:from(
gleam@atom:create_from_string(<<"reject_invalid_utf8"/utf8>>)
),
Maybe_prepend_reject_invalid_utf8_dynamic = fun(Options@1) ->
case Reject_invalid_utf8 of
true ->
[Reject_invalid_utf8_dynamic | Options@1];
false ->
Options@1
end
end,
gleam@dynamic:from(
Maybe_prepend_reject_invalid_utf8_dynamic(
[Duplicate_map_keys_dynamic, Allow_ctrl_chars_dynamic]
)
).
-spec jsone_try_decode_decoder() -> decode:decoder(gleam@dynamic:dynamic()).
jsone_try_decode_decoder() ->
Ok_decoder = decode:element(1, decode:dynamic()),
Error_decoder = decode:fail(<<"Invalid JSON"/utf8>>),
decode:ok_error_tuple(Ok_decoder, Error_decoder).
-spec decode(binary()) -> {ok, gleam@dynamic:dynamic()} | {error, binary()}.
decode(Json) ->
decode:decode_dynamic(jsone:try_decode(Json), jsone_try_decode_decoder()).
-spec decode_with_options(binary(), options()) -> {ok, gleam@dynamic:dynamic()} |
{error, binary()}.
decode_with_options(Json, Options) ->
decode:decode_dynamic(
jsone:try_decode(Json, transform_options(Options)),
jsone_try_decode_decoder()
).
-spec string(binary()) -> json_value().
string(String) ->
{json_string, String}.
-spec int(integer()) -> json_value().
int(Int) ->
{json_number, {json_int, Int}}.
-spec float(float()) -> json_value().
float(Float) ->
{json_number, {json_float, Float}}.
-spec array(list(FBH), fun((FBH) -> json_value())) -> json_value().
array(List, Encoder) ->
{json_array, gleam@list:map(List, Encoder)}.
-spec bool(boolean()) -> json_value().
bool(Bool) ->
{json_bool, Bool}.
-spec null() -> json_value().
null() ->
json_null.
-spec object(list({binary(), json_value()})) -> json_value().
object(Object) ->
{json_object, Object}.
-spec to_dynamic(json_value()) -> gleam@dynamic:dynamic().
to_dynamic(Json_value) ->
case Json_value of
{json_string, String} ->
gleam@dynamic:from(String);
{json_number, Json_number} ->
case Json_number of
{json_int, Int} ->
gleam@dynamic:from(Int);
{json_float, Float} ->
gleam@dynamic:from(Float)
end;
{json_array, List} ->
gleam@dynamic:from(gleam@list:map(List, fun to_dynamic/1));
json_null ->
gleam@dynamic:from(gleam@atom:create_from_string(<<"null"/utf8>>));
{json_bool, Bool} ->
gleam@dynamic:from(Bool);
{json_object, Object} ->
gleam@dynamic:from(
gleam@map:from_list(
gleam@list:map(
Object,
fun(Gleam@capture_variable) ->
gleam@pair:map_second(
Gleam@capture_variable,
fun to_dynamic/1
)
end
)
)
)
end.
-spec jsone_try_decoder() -> decode:decoder(gleam@dynamic:dynamic()).
jsone_try_decoder() ->
Ok_decoder = decode:element(1, decode:dynamic()),
Error_decoder = decode:fail(<<"Invalid JSON value"/utf8>>),
decode:ok_error_tuple(Ok_decoder, Error_decoder).
-spec encode(json_value()) -> {ok, gleam@dynamic:dynamic()} | {error, binary()}.
encode(Json_value) ->
Encode_option = [{float_format, [{decimals, 18}, compact]}],
decode:decode_dynamic(
jsone:try_encode(to_dynamic(Json_value), Encode_option),
jsone_try_decoder()
).