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

Steve Strong steve@REDACTED
Fri Jun 14 12:34:28 CEST 2013


My plan for the actual JSON encode / decode is for the mapper to produce a structure that's compatible with jsx (https://github.com/talentdeficit/jsx) - that's the encoder / decoder that we currently use.   If we go the open-source route, I'd probably aim to make that pluggable so that folk can use their preferred JSON encoder.

Cheers,

Steve 

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


On Friday, 14 June 2013 at 12:31, Slava Yurin wrote:

>  See https://github.com/iskra/jsonx. Maybe it decoder/encoder will help you.
>  
> 14.06.2013, 17:13, "Steve Strong" <steve@REDACTED (mailto:steve@REDACTED)>:
> > 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)
> >  
> >  
> > 
> > ,
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED (mailto:erlang-questions@REDACTED)
> > http://erlang.org/mailman/listinfo/erlang-questions 

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


More information about the erlang-questions mailing list