[erlang-questions] Records / Proplists / JSON / BSON

Steve Strong <>
Fri Jun 14 12:12:46 CEST 2013


Hi, 

We have built a number of projects recently that have a mongodb backend, an HTML / javascript frontend and Erlang in the middle - pretty standard stuff.  One of the things the we end up repeating over and over is mapping data from BSON (the mongo format) to Records (our preferred in-memory format for Erlang) and JSON (to send / receive from the browser).  To add to the mix, we also like using proplists in configuration files, so have mappings from those to records as well.

On the last project I finally got sick of doing it by hand, so knocked up a fairly simple parse transform to take the records (with their type specifications) and generate the mapping code, which has resulted in being able to do things like (note - pseudocode only!):

Foo = build_my_record(),
mongo:insert(collection, mapper:record_to_bson(Foo)),
web_socket:send(Client, mapper:record_to_json(Foo)),

receive
{client, Response} ->
do_stuff_with(mapper:json_to_record(foo, Response)
end

This has worked very well, and handles about 80% of the types we throw at it.  The sorts of types that it doesn't deal with are unions and tuples, e.g.

-record(bla, {
metadata :: x() | y(),
ratio :: {integer(), integer()}
}). 
 
Due to the value the simple version has had to us, I'm about to embark on a re-work that is going to aim to handle (pretty much) any type you can throw at it, and give full two-directional fidelity on the conversions (e.g., you can assert that Data == xxx_to_record(record_to_xxx(Data)) ).  

So, a couple of questions:

1. Does such a thing already exist?  I'm no fan of re-inventing the wheel :)
2. If not, would anyone be interested in it being open-sourced?
3. If 2., then does anyone have opinions on the functionality / API etc?

Interested in any feedback,

Cheers,

Steve
-- 
Steve Strong
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130614/ba4004bf/attachment.html>


More information about the erlang-questions mailing list