Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 9 Sep 2018 15:10:09 +0100
From:      Matt Churchyard <churchers@gmail.com>
To:        freebsd-hackers@freebsd.org
Subject:   Fwd: Getting valid JSON output with xo(1)
Message-ID:  <CANV9Nznba3ua_W17BnGOpiEcsW5pp-Jk4vFOcJxYnzVbUfWQ6w@mail.gmail.com>
In-Reply-To: <CANV9NzmyPoDk6qN19wW0SvzPu4aN9Ypsj9a_ttS2gp1EmqHUwQ@mail.gmail.com>
References:  <CANV9Nz=3aOd5RM9KyTfz-1_%2BEnffKEOVQbYZxJY=i9_x0gorpA@mail.gmail.com> <ef40f50a-3a80-cc8d-5e9e-4bf40f8ff604@freebsd.org> <CANV9NzmyPoDk6qN19wW0SvzPu4aN9Ypsj9a_ttS2gp1EmqHUwQ@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi Stefan,

I did include the commands used to generate the sample output in my first
message. I am using the xo(1) utility, not libxo directly. I did mention
this might not be the right list (you've obviously assumed I'm directly
using libxo), but it does seem to me this is more an issue with the xo
utility, rather than just a usage error.

I've since looked further into the libxo documentation, and as you suggest,
it does seem that I need to be using the xo_open_list/xo_close_list
functions to generate the right output. However, these are not exposed via
xo(1). The documentation for xo_open_list describes how it is used to
create lists of individual entities, whereas the "l" modifier (which is all
I can access via xo(1)) creates "leaf lists", which are only for single
values. As such, it seems there's a big hole in the functionality of xo(1),
meaning it can only really be used to create very simple JSON documents.

As I mentioned in my first message, it looks like xo(1) could do with some
way to open/close lists, similar to the --open/--close options, and a way
to specify that a single call should be output as a part of a list.

Matt

On Sun, Sep 9, 2018 at 10:29 AM Stefan Esser <se@freebsd.org> wrote:

> Am 08.09.18 um 15:41 schrieb Matt Churchyard:
> > Hello,
> >
> > Sorry if this is the wrong list, I wasn't sure which was the most
> > appropriate.
> >
> > I've been asked to look at generating machine output from vm-bhyve using
> > xo(1), and though it would be an interesting task. However, I'm coming
> > across a few issues getting valid output. I've reduced my code down to
> the
> > following example -
> >
> > xo -pX --open bhyve/machines
> > xo -pX --depth 2 --wrap machine "{:name},{:memory}" test1 2GB
> > xo -pX --depth 2 --wrap machine "{:name},{:memory}" test2 4GB
> > xo -pX --close bhyve/machines
> >
> > This produces the following XML output which looks good -
> >
> > <bhyve>
> >   <machines>
> >     <machine>
> >       <name>test1</name>
> >       <memory>2GB</memory>
> >     </machine>
> >     <machine>
> >       <name>test2</name>
> >       <memory>4GB</memory>
> >     </machine>
> >   </machine>
> > </bhyve>
> >
> > If I choose HTML output I get the following invalid output which is
> missing
> > closing </div> tags.
> >
> > <div class="line">
> >   <div class="data" data-tag="name">test1</div>
> >   <div class="text">,</div>
> >   <div class="data" data-tag="memory">2GB</div>
> > <div class="line">
> >   <div class="data" data-tag="name">test2</div>
> >   <div class="text">,</div>
> >   <div class="data" data-tag="memory">4GB</div>
> >
> > This can be fixed by adding "\n" to the end of the format lines, which
> may
> > be required, but it seems strange that I can so easily generate broken
> > output. I'm not too bothered about HTML though as it seems fairly
> useless,
> > JSON is my biggest issue -
> It would be a lot easier to help if you posted a (stripped down) version of
> the code used to generate this output.
>
> You are not correctly using the list functions, and thus the outer lists
> are
> not closed. I had suggested a better syntax and semantics for these
> functions
> to automatically take care of such cases, but they were rejected by the
> main
> libxo author.
>
> Every xo_open_list() must be matched by a matching xo_close_list() and you
> may need to use another type (list, container, instance) than you currently
> do.
>
> Did you run xolint to test your usage of xo functions?
>
> Regards, STefan
>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANV9Nznba3ua_W17BnGOpiEcsW5pp-Jk4vFOcJxYnzVbUfWQ6w>