[erlang-questions] JSON Parser

Matthew Fitzpatrick matthew.fitzpatrick6012@REDACTED
Thu Jul 16 00:07:16 CEST 2015

I'm in a project that's stuck back on a very old version of Erlang.
We don't have the capability of using maps so we use records as our
"Erlang" model. I'm not sure if you're stuck using records. If you can use
maps, I think that fits best. If you HAVE to use records though, We've had
good luck utilizing jiffy along with an additional translation layer in our
code. Your translation layer can be responsible for handling the
transformation to and from EJSON.

The key component is using `record_info(fields, _)` which will allow you to
get your record keys at run-time. I've often done this with a macro:

-record(my_record, {id}).
-define(RECORD_KEYS, record_info(fields, my_record))
%% ?RECORD_KEYS = [id].

After you've got your keys, you should be able to do all sorts of
shenanigans to make the mapping for your intermediary layer.

Fair warning though, the intermediary layer starts to get a little weird if
you use records that contain other records or if you want to make it
generic. If you decide to go that route and have other questions feel free
to hit me up off list.

First mailing list reply btw, if i did something wrong/incorrect, just
email me off list about it and I'll fix it for next time.

On Wed, Jul 15, 2015 at 1:28 PM, Kannan <vasdeveloper@REDACTED> wrote:

> I was out of Erlang programing for the last five years, I think maps has
> been introduced during that time. I was not aware of it earlier. Just went
> through the documentation, and yes it is the best format to match JSONs.
> The syntax is quite complicated though. Using two characters to map key
> and value, assignment, update, access and matching have different K-V
> separator symbols etc. However, as a declaration free K-V grouping
> structure, it is the best option.
> Regards,
> Theepan
> On Wed, Jul 15, 2015 at 11:33 PM, Garry Hodgson <garry@REDACTED>
> wrote:
>>  I prefer mapping json objects to erlang maps, lists to lists, and
>> strings to binaries. i use a wrapper around mochijson to do that.
>> On 7/15/15 2:00 PM, Kannan wrote:
>> Is any of them supporting Erlang 'record' as their base for
>> encoding/decoding. I see many of them are doing it with just list of tuples
>> of binaries.Erlang records best match the structure of JSON format.
>>  JSON
>> ----------
>> {"name": "Theepan",
>>   "work": "Coding",
>>   "salary": "0"
>> }
>>  Matching Erlang record
>> -----------------------------------
>> -record( json_record,
>> {
>>   'name' = "Theepan",
>>   'work' = "Coding",
>>   'salary' = "0"
>> }
>> }
>>  Thanks,
>> Theepan
>> On Wed, Jul 15, 2015 at 2:33 PM, Jesper Louis Andersen <
>> jesper.louis.andersen@REDACTED> wrote:
>>> On Tue, Jul 14, 2015 at 10:06 PM, Kannan <vasdeveloper@REDACTED> wrote:
>>>> I come across many JSON libraries. Once from MochiWeb, Other one from
>>>> Yaws. Third one from CouchDB. And some others through Googling.
>>>  There are two very popular JSON parsers in Erlang: jsx and jiffy.
>>>  jsx is written in plain Erlang. It is fast, correct and since it is
>>> written in Erlang, it will also automatically yield for other processes in
>>> the system.
>>>  jiffy is written as a C NIF. It is about 10 times faster than jsx, but
>>> the caveat is everything that has to do with C NIFs: blocking a scheduler,
>>> C code having errors, security considerations, etc.
>>>  I tend to run with `jsx` in my projects, and then I switch away from
>>> JSON when it gets to slow. JSON is a bad format that should never have
>>> existed in the first place. We are stuck with it because it's historic
>>> alternative, XML, was far worse in every aspect.
>>> --
>>> J.
>> _______________________________________________
>> erlang-questions mailing listerlang-questions@REDACTED://erlang.org/mailman/listinfo/erlang-questions
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> 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/20150715/eeb2c323/attachment.htm>

More information about the erlang-questions mailing list