[PATCH 2/2] Generate correct json
Gehad Elrobey
gehadelrobey at gmail.com
Tue Aug 26 13:13:22 PDT 2014
This is very useful thanks, I ll try to migrate the JSON exporter to this
model.
Dirk: I think you have missed a patch that adds the divecomputer data to
the exports.
On Tue, Aug 26, 2014 at 10:02 PM, Linus Torvalds <
torvalds at linux-foundation.org> wrote:
> On Tue, Aug 26, 2014 at 11:24 AM, Gehad Elrobey <gehadelrobey at gmail.com>
> wrote:
> > I think this can be done easily by just checking if the item is the last
> > item in the array before putting the commas.
>
> So, quite frankly, you guys need to learn a very clean and simple
> model for outputting lists with separators.
>
> The way to generate clean separators is to have a separator string
> variable, and output it *before* the entry. And initialize it to empty
> (or a prefix for the whole thing), and then in the loop just
> unconditionally set it to the separator for the next entry.
>
> For example, do something like this:
>
> char *separator = "\"tags\": [ \"";
>
> if (!list)
> return;
>
> do {
> put_string(b, separator);
> put_HTML_quoted(b, list->tag->name);
> separator = "\", \"";
> list = list->next;
> } while (list);
> put_string("\" ]");
>
> is a clean and efficient way to generate something like this:
>
> "tags": [ "entry1", "entry2" ]
>
> Note how it doesn't need any special cases or conditionals in the
> code. There's no test for "is this the first/last entry". It
> JustWorks(tm). The only special case is the empty list, which for
> things like this does tend to need to be a special case regardless
> (because you generally wouldn't want to output anything at all for
> that case - no empty list, no brackets, no "tags", no nothing). Also
> note that this shows how you can often combine the "first separator is
> different" with the prefix you need, and make the initial part of the
> string be that first "separator".
>
> It's a very useful and simple coding pattern for things like this.
>
> Linus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.hohndel.org/pipermail/subsurface/attachments/20140826/f4a7cc08/attachment.html>
More information about the subsurface
mailing list