[erlang-questions] Decoding term_to_binary in javascript

Witold Baryluk baryluk@REDACTED
Sun Nov 27 23:12:05 CET 2011

On 11-15 12:57, Joe Armstrong wrote:
> I'd like to send a term_to_binary encoded term to a web client and decode
> it in javascript.
> If I could do this it would eliminate the impedance mismatch between
> server-side databases
> and client code in a browser. I want to totally eliminate conversions to
> and from JSON. Just send
> raw blobs containing term_to_binary encoded data and pluck what I want in
> the client.
> I would not necessarily want to decode the entire blob, I might just want
> to extract a specific item
> So for example if In Erlang I created a blob by saying
>       Blob = term_to_binary({thing,"foo","bar",["baz"]})
> I'd like to send this to a browser and in Javascript say:
>      var z = mylib_hd(my_lib_element(4,Blob))
> Whereby z would be the javascript string "baz".
> Has anybody implemented anything like this?
> /Joe

I was trying to implement it about year ago, but failed due problem
with using full 0-255 range of values in returned string.
Probably JavaScript TypedArray (supported in Chrome, Opera 11.6 and Firefox 4+)
could solve it now.

For now I encode term into specially structured JSON, and then
after parsing JSON in browser, I run it through the
speciall function which changes it to actual 'Erlang-like' objects.
This way one can differentiate easly beetwen atoms and strings,
as well handle strange things like improper lists.

It works nice, but space overhead is very big (compansated somehow
when using gzip compression in HTTP).

Witold Baryluk

More information about the erlang-questions mailing list