./ct_report/coverage/mongoose_graphql_response.COVER.html

1 -module(mongoose_graphql_response).
2
3 -export([term_to_json/1, term_to_pretty_json/1]).
4
5 term_to_json(Term) ->
6 606 jiffy:encode(fixup(Term)).
7
8 term_to_pretty_json(Term) ->
9 440 jiffy:encode(fixup(Term), [pretty]).
10
11 %% Ground types
12 18923 fixup(Term) when is_number(Term) -> Term;
13 5022 fixup(Term) when is_atom(Term) -> Term;
14 95286 fixup(Term) when is_binary(Term) -> Term;
15 %% Compound types
16 fixup(Term) when is_list(Term) ->
17 19174 [fixup(T) || T <- Term];
18 fixup(Term) when is_map(Term) ->
19 24494 KVs = maps:to_list(Term),
20 24494 maps:from_list([{fixup_key(K), fixup(V)} || {K, V} <- KVs]);
21 fixup(Term) ->
22 %% Every other term is transformed into a binary value
23 1 iolist_to_binary(
24 io_lib:format("~p", [Term])).
25
26 fixup_key(Term) ->
27 56708 case fixup(Term) of
28 T when is_binary(T) ->
29 52828 T;
30 T ->
31 3880 iolist_to_binary(io_lib:format("~p", [T]))
32 end.
Line Hits Source