[eeps] EEP 18 - JSON bifs; object representation

Richard A. O'Keefe <>
Mon Aug 18 03:42:57 CEST 2008


On 15 Aug 2008, at 8:14 pm, Raimo Niskanen wrote:

> I just want to vote for the tuple of one list
> representation for an JSON object.

...
There is no mapping of JSON forms to Erlang terms
that is free of drawbacks.  (Not "backdraws".)
Having to stick an extra tuple around every term
representing an "object" is a fairly nasty
drawback in my eyes.

> foo(Json, Key) ->
>    case Json of
>        {L} -> % JSON object
>            proplists:get_value(Key, L);
>        L when is_list(L) -> % JSON sequence
>            [foo(J) || J <- L];
>        _ ->
>            other(Json, Key)
>    end.

%  The following macros are to be used in guards ONLY.

-define(JSON_OBJECT(X), is_tuple(hd(X))).
-define(JSON_ARRAY(X),  is_list(X)).
-define(JSON_STRING(X), is_binary(X)).
-define(JSON_NUMBER(X), is_number(X)).

foo(Json, Key) ->
     if  ?JSON_OBJECT(Json) ->
             proplists:get_value(Key, Json)
       ; ?JSON_ARRAY(Json) ->
	    [foo(K) || K <- Json]
       ; ?JSON_STRING(Json) ->
             string_case(Json, Key)
       ; ?JSON_NUMBER(Json) ->
             number_case(Json, Key)
     end.

The sole restriction here is that JSON_OBJECT must be
tested before JSON_ARRAY.  Now that I come to think of
it, given the variety in the way that existing JSON/Erlang
packages handle the mapping, I believe that I would want
to use such macros *whatever* the mapping, not for my sake
as a writer, but to make it really obvious to the reader
what's going on.

Yes, [{}] is an anomaly,  but "is a list of tuples" is
actually _easier_ to check for than "is a list of
two-element tuples", so it's not _that_ much of one.
Does anyone have any idea how common {} actually is in
real-world JSON?

I wrote the JSON proposal mainly as an exercise in writing
EEPs.  I would be *delighted*, nay, thrilled to the very
socks, to see someone else take it over.




More information about the eeps mailing list