Packages

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

Current section

Files

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

gen/src/gleam_synapses@model@json_utils.erl

-module(gleam_synapses@model@json_utils).
-compile(no_auto_import).
-export([encode/1]).
-export_type([decimals/0, float_format/0]).
-type decimals() :: {decimals, integer()} | compact.
-type float_format() :: {float_format, list(decimals())}.
-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(gleam@jsone:json_value()) -> {ok, gleam@dynamic:dynamic()} |
{error, binary()}.
encode(Json_value) ->
Encode_option = [{float_format, [{decimals, 18}, compact]}],
decode:decode_dynamic(
jsone:try_encode(gleam@jsone:to_dynamic(Json_value), Encode_option),
jsone_try_decoder()
).