0

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?

enter image description here

2> [123, 34, 115, 116, 97, 116, 117, 115, 34, 58].
"{\"status\":"
quantumpotato
  • 9,637
  • 14
  • 70
  • 146
  • Do you want to detect this in Erlang or JavaScript? For Erlang, you can use this: https://stackoverflow.com/a/8035531/320615. – Dogbert Jun 30 '17 at 05:19
  • Hello again Dogbert! Yes that looks useful. From this I can figure out when I need to convert my list of characters to something the JSON can read as a string. – quantumpotato Jun 30 '17 at 22:14
  • Looks like I can use <> per https://stackoverflow.com/questions/42044491/encoding-erlang-maps-as-json-with-strings-for-parsing-by-javascript – quantumpotato Jun 30 '17 at 22:18

1 Answers1

0

Simply check each element have value as an integer from 8 to 255. But I think Erlang did this job. So maybe the reason is some element in your array have value not in range 8 to 255.

duongtd23
  • 101
  • 1
  • 6