[erlang-questions] [ANN] Erlson - dynamic name-value dictionary data type and syntax

Bob Ippolito bob@REDACTED
Tue Aug 30 03:19:34 CEST 2011


On Mon, Aug 29, 2011 at 6:05 PM, James Aimonetti <james@REDACTED> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> On 08/29/2011 05:12 PM, Bob Ippolito wrote:
>> On Mon, Aug 29, 2011 at 4:53 PM, Loïc Hoguin <essen@REDACTED> wrote:
>>> On 08/30/2011 01:36 AM, Jack Moffitt wrote:
>>>>> May I suggest switching the decoded JSON format to that used by ejson
>>>>> (https://github.com/benoitc/ejson) and jiffy
>>>>> (https://github.com/davisp/jiffy) instead of the format used by
>>>>> mochiweb/mochijson2. Most of the projects that make heavy use of JSON seem
>>>>> to be moving to the format I'm suggesting.
>>>>
>>>> A tuple of an array of tuples seems less than elegant  Why not just a
>>>> tuple of tuples? Is scanning through a tuple significantly slower than
>>>> scanning an array?
>>>
>>> This should explain the whys:
>>>
>>> http://www.erlang.org/eeps/eep-0018.html
>>>
>>>> As far as formats go, this is a new one to me. Most projects I
>>>> interact with seem to use either mochijson or mochijson2. I'd love for
>>>> there to be a canonical one though.
>>>
>>> All recent JSON projects follow that EEP as far as I know. If OTP gets
>>> JSON support then your code will be working with the OTP implementation.
>>> It's also less annoying to write than {struct, ...} because it's trying
>>> (and not failing on me yet) to be a 1:1 match with Erlang types or when
>>> not possible to give expected results.
>>>
>>> Personally I prefer jsx (https://github.com/talentdeficit/jsx) simply
>>> because it's not a NIF and thus doesn't impact the scheduler. But your
>>> needs and requirements may vary.
>>
>> It would be basically trivial to add {proplist()} support to
>> mochijson2 as an addition to {struct, proplist()}. There's not much of
>> a difference really, it's still wrapping a tuple around it but just
>> without a tag. I'll have a look at that.
>>
> An old version of Couchbeam accomplished that conversion thusly, if that
> helps:
>
[snip code]

Yeah, it was never hard to do it from the outside using object_hook on
decode and handler on encode. It's now natively supported in mochiweb
v2.1.0 without having to write any funs.

https://github.com/mochi/mochiweb

JSON = <<"{\"k\": \"v\"}">>.

[{<<"k">>, <<"v">>}] =:= mochijson2:decode(JSON, [{format, proplist}]).

{[{<<"k">>, <<"v">>}]} =:= mochijson2:decode(JSON, [{format, eep18}]).

%% the default
{struct, [{<<"k">>, <<"v">>}]} =:= mochijson2:decode(JSON, [{format, struct}]).

These will also round-trip with the encoder without any surprises or
additional options.

I've also added support for the {} and {P} format to kvc. kvc is
basically a simple query language for a host of proplist-like data
structures.
https://github.com/etrepum/kvc

-bob



More information about the erlang-questions mailing list