<div dir="ltr"><br><br><div class="gmail_quote">On Tue, Jul 29, 2008 at 3:13 AM, Richard A. O'Keefe <span dir="ltr"><<a href="mailto:ok@cs.otago.ac.nz">ok@cs.otago.ac.nz</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">On 29 Jul 2008, at 9:51 am, Paulo Sérgio Almeida wrote:<br>
> I think there is no doubt that lists will be more useful than<br>
> tuples. There is, however another option, that I have been using in<br>
> a json parser I wrote:<br>
><br>
> (C) an object is simply a proplist, i.e. a list of tuples.<br>
<br>
</div>This is in fact what I originally proposed,<br>
the tricky point being that {} is a legal empty object in JSON,<br>
and we can't map that to [] because that's the representation<br>
for the empty sequence [].<br>
<br>
(O) Original proposal: {} => {}, other objects => list of pairs<br>
(A) Armstrong version: object => tuple of pairs, no exceptions.<br>
(B) Object => {list of pairs}.<br>
(C) Almeida proposal: as (O) but {} => [{}].<br>
<br>
The arguments for usability of the result in Erlang are the<br>
arguments that originally had me proposing (O).<br>
<br>
However, I note that nothing stops us providing a range of<br>
handy-dandy functions that work on tuples of pairs.<br>
<br>
%(O)<br>
is_object({})        -> true;<br>
is_object([{_,_}|_]) -> true;<br>
is_object(_)         -> false.<br>
<br>
%(A)<br>
is_object(T)         -> is_tuple(T).<br>
<br>
%(B)<br>
is_object({T})       -> is_list(T).</blockquote><div>is_object({T})       -> is_list(T);<br>is_object(_)       -> false.   % avoid exception<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
%(C)<br>
is_object([T|_])     -> is_tuple(T);<br>
is_object(_)         -> false.<br>
<br>
It's rather annoying to be so bothered about empty objects;<br>
do they occur in practical JSON?  Proposal (C) seems neat enough;<br>
the main problem is fitting the results with @type.</blockquote><div><br>(C) seems good for me too, because proplist works fine with it.<br><br>> proplists:get_bool(a, [{}]).<br>false<br>> proplists:get_bool(a, [{a, true}]).<br>
true<br>> proplists:get_value(a, [{a, true}]).<br>true<br>> proplists:get_value(a, [{a, heh}]).<br>heh<br>> proplists:get_value(a, [{}]).<br>undefined<br><br>atom is used only for simplicity, but works with binaries too. (JSON's boolean should be true/false atom of course I assume.)<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<font color="#888888"><br>
--<br>
If stupidity were a crime, who'd 'scape hanging?<br>
</font><div><div></div><div class="Wj3C7c"><br>
<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br>
</div>