1

I would like to send erlang terms (erlang-based back end) to the web browser. It is easy enough to encode a term on the erlang side using something like:

term_to_binary(Term)

or:

binary_to_list(term_to_binary(Term))

The problem of course is that scrambled garbage shows up on the browser end.

Question: Is there either some encoding I can use on the browser end, or more likely, some Content-Type I can accept on the browser end to unscramble this?

Thanks.

X Y Z
  • 65
  • 1
  • 3

4 Answers4

2

There is piqi that provides extensive mapping mechanisms between .piqi (its record definition language), json, xml and protobuf. It's a really cool tool, that we use all the time to map between all of these formats. Typically when I build something (in Erlang) that needs to provide some sort of data to something else, I start with a piqi definition file that defines the structure. The piqic compiler generates Erlang record definitions and conversion code to do conversions easily.

Highly recommended, but it might be overkill for what you're doing.

bart van deenen
  • 661
  • 6
  • 16
2

Use io_lib:format("~p",[Term]). It will produce a string representation of the erlang term which can be showed on a web page. Consider also checking out this question and its answer.

Community
  • 1
  • 1
Muzaaya Joshua
  • 7,736
  • 3
  • 47
  • 86
1

Encode it with base64. Get it via ajax, then decode either with native window.atob or any of numerous available libs.

c69
  • 19,951
  • 7
  • 52
  • 82
1

If it is for a web browser, I would go for a Json string, it's unicode and browsers support it nativaly.

Maybe consider Json and do something like this for strings:

1> HelloJerome = "Hello Jérôme".
"Hello Jérôme"
2> HelloJeromeBin = list_to_binary(HelloJerome).
<<"Hello Jérôme">>
3> HelloJeromeJson = << <<"{\"helloJerome\":\"">>/bits, HelloJeromeBin/bits, $\", $} >>.
<<"{\"helloJerome\":\"Hello Jérôme\"}">>

In the browser console:

jerome = JSON.parse('{\"hello\":\"Hello Jérôme\"}')

Now

jerome.hello == "Hello Jérôme"

There are some good lib out there ejson or mochijson2 are the classic ones but ktuo or ...

jerome
  • 173
  • 1
  • 7