I recursively parse a map to convert to json. Here is my o
output fn:
hash(Map) ->
binary:bin_to_list(jiffy:encode(Map)).
atomJSON(Atom) -> binary:list_to_bin(atom_to_list(Atom)).
% json output
o(Number) when is_number(Number) ->
Number;
o(Atom) when is_atom(Atom) ->
atomJSON(Atom);
o(Tuple) when is_tuple(Tuple) ->
o(tuple_to_list(Tuple), []);
o(List) when is_list(List) ->
o(List, []);
o(Map) when is_map(Map) -> %expects atoms for internal map keys
o(maps:keys(Map), Map, #{}).
o([], List) ->
lists:reverse(List);
o([H | T], List) ->
o(T, [o(H) | List]).
o([], _Map, Res) ->
hash(Res);
o([H | T], Map, Res) ->
o(T, Map, maps:put(atomJSON(H), o(maps:get(H, Map)), Res)).
Most of this gets converted to JSON fine. A status key & value, being strings, do not.
I know the erl
shell conveniently detects a string but is_string/1
doesn't exist. How can I detect a string of characters and convert in so I can display the proper text in my javascript?
2> [123, 34, 115, 116, 97, 116, 117, 115, 34, 58].
"{\"status\":"