-module(jasper). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/jasper.gleam"). -export([decoder/0, parse/1, parse_bits/1, to_json/1, to_string/1, to_string_tree/1, to_pretty_string/2, to_pretty_string_tree/2, 'query'/2]). -export_type([json_value/0, indentation/0, json_query/0, inv_json_query/0, json_query_error/0]). -type json_value() :: {object, gleam@dict:dict(binary(), json_value())} | {array, list(json_value())} | {string, binary()} | {int, integer()} | {float, float()} | {bool, boolean()} | null. -type indentation() :: {spaces, integer()} | tab | {tabs, integer()}. -type json_query() :: root | {key, json_query(), binary()} | {key_or, json_query(), binary(), json_value()} | {index, json_query(), integer()} | {index_or, json_query(), integer(), json_value()} | {filter, json_query(), fun((json_value()) -> boolean())} | {map, json_query(), fun((json_value()) -> json_value())} | {map_keys, json_query(), fun((binary()) -> binary())} | {map_values, json_query(), fun((binary(), json_value()) -> json_value())} | {filter_map, json_query(), fun((json_value()) -> {ok, json_value()} | {error, nil})} | {for_each, json_query()} | {for_each_ok, json_query()}. -type inv_json_query() :: inv_end | {inv_key, binary(), inv_json_query()} | {inv_key_or, binary(), json_value(), inv_json_query()} | {inv_index, integer(), inv_json_query()} | {inv_index_or, integer(), json_value(), inv_json_query()} | {inv_filter, fun((json_value()) -> boolean()), inv_json_query()} | {inv_map, fun((json_value()) -> json_value()), inv_json_query()} | {inv_map_keys, fun((binary()) -> binary()), inv_json_query()} | {inv_map_values, fun((binary(), json_value()) -> json_value()), inv_json_query()} | {inv_filter_map, fun((json_value()) -> {ok, json_value()} | {error, nil}), inv_json_query()} | {inv_for_each, inv_json_query()} | {inv_for_each_ok, inv_json_query()}. -type json_query_error() :: {unexpected_type, json_value()} | {missing_object_key, json_value(), binary()} | {index_out_of_bounds, json_value(), integer()}. -file("src/jasper.gleam", 22). -spec decoder() -> gleam@dynamic@decode:decoder(json_value()). decoder() -> gleam@dynamic@decode:one_of( begin _pipe = {decoder, fun gleam@dynamic@decode:decode_string/1}, gleam@dynamic@decode:map( _pipe, fun(Field@0) -> {string, Field@0} end ) end, [begin _pipe@1 = {decoder, fun gleam@dynamic@decode:decode_int/1}, gleam@dynamic@decode:map( _pipe@1, fun(Field@0) -> {int, Field@0} end ) end, begin _pipe@2 = {decoder, fun gleam@dynamic@decode:decode_float/1}, gleam@dynamic@decode:map( _pipe@2, fun(Field@0) -> {float, Field@0} end ) end, begin _pipe@3 = {decoder, fun gleam@dynamic@decode:decode_bool/1}, gleam@dynamic@decode:map( _pipe@3, fun(Field@0) -> {bool, Field@0} end ) end, begin _pipe@4 = gleam@dynamic@decode:list( gleam@dynamic@decode:recursive(fun decoder/0) ), gleam@dynamic@decode:map( _pipe@4, fun(Field@0) -> {array, Field@0} end ) end, begin _pipe@5 = gleam@dynamic@decode:dict( {decoder, fun gleam@dynamic@decode:decode_string/1}, gleam@dynamic@decode:recursive(fun decoder/0) ), gleam@dynamic@decode:map( _pipe@5, fun(Field@0) -> {object, Field@0} end ) end, gleam@dynamic@decode:success(null)] ). -file("src/jasper.gleam", 34). -spec parse(binary()) -> {ok, json_value()} | {error, gleam@json:decode_error()}. parse(Value) -> gleam@json:parse(Value, decoder()). -file("src/jasper.gleam", 38). -spec parse_bits(bitstring()) -> {ok, json_value()} | {error, gleam@json:decode_error()}. parse_bits(Value) -> gleam@json:parse_bits(Value, decoder()). -file("src/jasper.gleam", 42). -spec to_json(json_value()) -> gleam@json:json(). to_json(Value) -> case Value of {string, S} -> gleam@json:string(S); {int, I} -> gleam@json:int(I); {float, F} -> gleam@json:float(F); {bool, B} -> gleam@json:bool(B); {array, A} -> gleam@json:array(A, fun to_json/1); {object, O} -> gleam@json:dict(O, fun gleam@function:identity/1, fun to_json/1); null -> gleam@json:null() end. -file("src/jasper.gleam", 54). -spec to_string(json_value()) -> binary(). to_string(Value) -> _pipe = to_json(Value), gleam@json:to_string(_pipe). -file("src/jasper.gleam", 58). -spec to_string_tree(json_value()) -> gleam@string_tree:string_tree(). to_string_tree(Value) -> _pipe = to_json(Value), gleam_json_ffi:json_to_iodata(_pipe). -file("src/jasper.gleam", 80). -spec do_to_pretty_string(json_value(), binary(), integer()) -> binary(). do_to_pretty_string(Value, Space, Depth) -> case Value of {object, Obj} -> case maps:to_list(Obj) of [] -> <<"{}"/utf8>>; Ls -> <<<<<<<<<<"{\n"/utf8, (gleam@string:repeat(Space, Depth + 1))/binary>>/binary, (begin _pipe = gleam@list:map( Ls, fun(Kv) -> <<<<<<"\""/utf8, (erlang:element( 1, Kv ))/binary>>/binary, "\": "/utf8>>/binary, (do_to_pretty_string( erlang:element(2, Kv), Space, Depth + 1 ))/binary>> end ), gleam@string:join( _pipe, <<",\n"/utf8, (gleam@string:repeat( Space, Depth + 1 ))/binary>> ) end)/binary>>/binary, "\n"/utf8>>/binary, (gleam@string:repeat(Space, Depth))/binary>>/binary, "}"/utf8>> end; {array, []} -> <<"[]"/utf8>>; {array, Arr} -> <<<<<<<<<<"[\n"/utf8, (gleam@string:repeat(Space, Depth + 1))/binary>>/binary, (begin _pipe@1 = Arr, _pipe@2 = gleam@list:map( _pipe@1, fun(_capture) -> do_to_pretty_string( _capture, Space, Depth + 1 ) end ), gleam@string:join( _pipe@2, <<",\n"/utf8, (gleam@string:repeat(Space, Depth + 1))/binary>> ) end)/binary>>/binary, "\n"/utf8>>/binary, (gleam@string:repeat(Space, Depth))/binary>>/binary, "]"/utf8>>; {string, S} -> <<<<"\""/utf8, S/binary>>/binary, "\""/utf8>>; {int, I} -> erlang:integer_to_binary(I); {float, F} -> gleam_stdlib:float_to_string(F); {bool, true} -> <<"true"/utf8>>; {bool, false} -> <<"false"/utf8>>; null -> <<"null"/utf8>> end. -file("src/jasper.gleam", 68). -spec to_pretty_string(json_value(), indentation()) -> binary(). to_pretty_string(Value, Indentation) -> do_to_pretty_string(Value, case Indentation of {spaces, N} -> gleam@string:repeat(<<" "/utf8>>, N); tab -> <<"\t"/utf8>>; {tabs, N@1} -> gleam@string:repeat(<<"\t"/utf8>>, N@1) end, 0). -file("src/jasper.gleam", 122). -spec to_pretty_string_tree(json_value(), indentation()) -> gleam@string_tree:string_tree(). to_pretty_string_tree(Value, Indentation) -> _pipe = to_pretty_string(Value, Indentation), gleam_stdlib:identity(_pipe). -file("src/jasper.gleam", 162). -spec invert_query_rec(json_query(), inv_json_query()) -> inv_json_query(). invert_query_rec(Query, State) -> case Query of root -> State; {key, Query@1, Key} -> invert_query_rec(Query@1, {inv_key, Key, State}); {key_or, Query@2, Key@1, O} -> invert_query_rec(Query@2, {inv_key_or, Key@1, O, State}); {index, Query@3, Index} -> invert_query_rec(Query@3, {inv_index, Index, State}); {index_or, Query@4, Index@1, Or} -> invert_query_rec(Query@4, {inv_index_or, Index@1, Or, State}); {filter, Query@5, Predicate} -> invert_query_rec(Query@5, {inv_filter, Predicate, State}); {map, Query@6, Mapping} -> invert_query_rec(Query@6, {inv_map, Mapping, State}); {map_keys, Query@7, Mapping@1} -> invert_query_rec(Query@7, {inv_map_keys, Mapping@1, State}); {map_values, Query@8, Mapping@2} -> invert_query_rec(Query@8, {inv_map_values, Mapping@2, State}); {filter_map, Query@9, Mapping@3} -> invert_query_rec(Query@9, {inv_filter_map, Mapping@3, State}); {for_each, Query@10} -> invert_query_rec(Query@10, {inv_for_each, State}); {for_each_ok, Query@11} -> invert_query_rec(Query@11, {inv_for_each_ok, State}) end. -file("src/jasper.gleam", 184). -spec invert_query(json_query()) -> inv_json_query(). invert_query(Query) -> invert_query_rec(Query, inv_end). -file("src/jasper.gleam", 194). -spec do_query(json_value(), inv_json_query()) -> {ok, json_value()} | {error, json_query_error()}. do_query(Json, Query) -> case Query of inv_end -> {ok, Json}; {inv_key, Key, Q} -> _pipe@2 = case Json of {object, Obj} = J -> _pipe = Obj, _pipe@1 = gleam_stdlib:map_get(_pipe, Key), gleam@result:replace_error( _pipe@1, {missing_object_key, J, Key} ); J@1 -> {error, {unexpected_type, J@1}} end, _pipe@3 = gleam@result:map( _pipe@2, fun(_capture) -> do_query(_capture, Q) end ), gleam@result:flatten(_pipe@3); {inv_key_or, Key@1, Or, Q@1} -> _pipe@7 = case Json of {object, Obj@1} -> _pipe@4 = Obj@1, _pipe@5 = gleam_stdlib:map_get(_pipe@4, Key@1), _pipe@6 = gleam@result:unwrap(_pipe@5, Or), {ok, _pipe@6}; J@2 -> {error, {unexpected_type, J@2}} end, _pipe@8 = gleam@result:map( _pipe@7, fun(_capture@1) -> do_query(_capture@1, Q@1) end ), gleam@result:flatten(_pipe@8); {inv_index, Index, Q@2} -> _pipe@12 = case Json of {array, Arr} = J@3 -> _pipe@9 = Arr, _pipe@10 = gleam@list:drop(_pipe@9, Index), _pipe@11 = gleam@list:first(_pipe@10), gleam@result:replace_error( _pipe@11, {index_out_of_bounds, J@3, Index} ); J@4 -> {error, {unexpected_type, J@4}} end, _pipe@13 = gleam@result:map( _pipe@12, fun(_capture@2) -> do_query(_capture@2, Q@2) end ), gleam@result:flatten(_pipe@13); {inv_index_or, Index@1, Or@1, Q@3} -> _pipe@18 = case Json of {array, Arr@1} -> _pipe@14 = Arr@1, _pipe@15 = gleam@list:drop(_pipe@14, Index@1), _pipe@16 = gleam@list:first(_pipe@15), _pipe@17 = gleam@result:unwrap(_pipe@16, Or@1), {ok, _pipe@17}; J@5 -> {error, {unexpected_type, J@5}} end, _pipe@19 = gleam@result:map( _pipe@18, fun(_capture@3) -> do_query(_capture@3, Q@3) end ), gleam@result:flatten(_pipe@19); {inv_filter, Predicate, Q@4} -> case Json of {array, Arr@2} -> _pipe@20 = Arr@2, _pipe@21 = gleam@list:filter(_pipe@20, Predicate), _pipe@22 = {array, _pipe@21}, do_query(_pipe@22, Q@4); J@6 -> {error, {unexpected_type, J@6}} end; {inv_map, Mapping, Q@5} -> case Json of {array, Arr@3} -> _pipe@23 = Arr@3, _pipe@24 = gleam@list:map(_pipe@23, Mapping), _pipe@25 = {array, _pipe@24}, do_query(_pipe@25, Q@5); J@7 -> {error, {unexpected_type, J@7}} end; {inv_map_keys, Mapping@1, Q@6} -> case Json of {object, Obj@2} -> _pipe@26 = Obj@2, _pipe@27 = maps:to_list(_pipe@26), _pipe@28 = gleam@list:map( _pipe@27, fun(Kv) -> {Mapping@1(erlang:element(1, Kv)), erlang:element(2, Kv)} end ), _pipe@29 = maps:from_list(_pipe@28), _pipe@30 = {object, _pipe@29}, do_query(_pipe@30, Q@6); J@8 -> {error, {unexpected_type, J@8}} end; {inv_map_values, Mapping@2, Q@7} -> case Json of {object, Obj@3} -> _pipe@31 = Obj@3, _pipe@32 = gleam@dict:map_values(_pipe@31, Mapping@2), _pipe@33 = {object, _pipe@32}, do_query(_pipe@33, Q@7); J@9 -> {error, {unexpected_type, J@9}} end; {inv_filter_map, Mapping@3, Q@8} -> case Json of {array, Arr@4} -> _pipe@34 = Arr@4, _pipe@35 = gleam@list:filter_map(_pipe@34, Mapping@3), _pipe@36 = {array, _pipe@35}, do_query(_pipe@36, Q@8); J@10 -> {error, {unexpected_type, J@10}} end; {inv_for_each, Q@9} -> case Json of {array, Arr@5} -> _pipe@37 = Arr@5, _pipe@38 = gleam@list:map( _pipe@37, fun(_capture@4) -> do_query(_capture@4, Q@9) end ), _pipe@39 = gleam@result:all(_pipe@38), gleam@result:map( _pipe@39, fun(Field@0) -> {array, Field@0} end ); J@11 -> {error, {unexpected_type, J@11}} end; {inv_for_each_ok, Q@10} -> case Json of {array, Arr@6} -> _pipe@40 = Arr@6, _pipe@41 = gleam@list:map( _pipe@40, fun(_capture@5) -> do_query(_capture@5, Q@10) end ), _pipe@42 = gleam@result:values(_pipe@41), _pipe@43 = {array, _pipe@42}, {ok, _pipe@43}; J@12 -> {error, {unexpected_type, J@12}} end end. -file("src/jasper.gleam", 313). -spec 'query'(json_value(), json_query()) -> {ok, json_value()} | {error, json_query_error()}. 'query'(Json, Query) -> do_query(Json, invert_query(Query)).