From owner-freebsd-hackers@freebsd.org Mon Sep 10 19:34:49 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D6CC31099D89 for ; Mon, 10 Sep 2018 19:34:48 +0000 (UTC) (envelope-from churchers@gmail.com) Received: from mail-ua1-x92a.google.com (mail-ua1-x92a.google.com [IPv6:2607:f8b0:4864:20::92a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6D12E718D6; Mon, 10 Sep 2018 19:34:48 +0000 (UTC) (envelope-from churchers@gmail.com) Received: by mail-ua1-x92a.google.com with SMTP id m26-v6so18445065uap.2; Mon, 10 Sep 2018 12:34:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=4J4GeDU2lhMkipbVDOqmDOfaDcTGFD7p9Yfpt7EFFoM=; b=ObktK/hmaiPZhGkMOfc3jPxbZVnb+qwLL3sC6C+Gv0skhD5qoD+XlalrLP4FY6xX7o 6iq5n+HONpAG+C60wo1iXEVZlLwm5cxZnJUewv0OisjHRHOiimDjfOq7hmU0MRuhalW6 q138Wz8OdadGXofIuEReET4pjSaBydbJ0eJVZHQ0O2RZVQwvfWxJd4HM/EVV/tPghhoc s/O51Vy7bLax7D6DiCXMaDfcTspv6ibKbOSUW/mUdSAyWqA5aR1kmf87tcSeToAU6A3+ RC+4Rr42Rv4qka3nonb3GgF8VaotesTRHVBdGHuB2snIJE/o5uSc+nZKr0VSOFEYAGFp /k2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=4J4GeDU2lhMkipbVDOqmDOfaDcTGFD7p9Yfpt7EFFoM=; b=kjlVaojtNHEUUvAKSZeFNJCUfBzLXGLmYqwQX+c1k2K958vhKND77abda3Zrbb93hq hXnMpV/Z7RcHgOZwcjgRlN+YAkLFPeIuYyhoLmEOK57IWDoAQf92hahAsw6gyA2q7esS b0hs6timuq9kAKnDFc4Sys/GgfzlcEAaGgyLEN7S57Hd8G30y0+PtOBRG3/3gVvBpGfx qQJXOZ7XJwfgLXUqewIrKRY6gnmL4YBmjNRph4b62uD5RVKK3e1+7DIaR1qu6xh8yisY WVuQTHCLsYfBTyqUMmN2oOyfg+J0vyI2lVB4au+yLF5nGu47XnHUfoDLW6bl7bhdwQsz 2c6A== X-Gm-Message-State: APzg51BNGoY6h7LdH9IMg463B1xNSpI/C/lL95vJssJIdEXrcj1cFHMh k0fWQFcQ+kJqxl4AbVS0V6fCYRMq5B8NazETvbFLsg== X-Google-Smtp-Source: ANB0VdZJSULVGiZ2j8KHI0JGP9ds2nlWcXk3XUOvnUpJfBavBC2m54lHjgDKzvXNsggRXB+zbjXtcfSOAmCGdoeSpj8= X-Received: by 2002:a67:e9d6:: with SMTP id q22-v6mr7249600vso.39.1536608087817; Mon, 10 Sep 2018 12:34:47 -0700 (PDT) MIME-Version: 1.0 References: <228b2a88-6134-c8c2-a28d-cb09cbc03ef6@freebsd.org> In-Reply-To: <228b2a88-6134-c8c2-a28d-cb09cbc03ef6@freebsd.org> From: Matt Churchyard Date: Mon, 10 Sep 2018 20:34:37 +0100 Message-ID: Subject: Re: Getting valid JSON output with xo(1) To: Stefan Esser Cc: freebsd-hackers@freebsd.org X-Mailman-Approved-At: Mon, 10 Sep 2018 19:57:53 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.27 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Sep 2018 19:34:49 -0000 Thanks Stefan For now I=E2=80=99ve altered my code to output the extra characters when ne= eded, and switch {} to [] using tr. I may raise an issue with the libxo guys and see what happens, as it seems to be a bit of a pain that I have to go through all this to generate a fairly simple JSON document. Matt On Sun, 9 Sep 2018 at 17:13, Stefan Esser wrote: > Am 09.09.18 um 16:10 schrieb Matt Churchyard: > > Hi Stefan, > > > > I did include the commands used to generate the sample output in my fir= st > > message. I am using the xo(1) utility, not libxo directly. I did mentio= n > > 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. > > Hi Matt, > > I'm sorry - you are of course right and there was everything required to > reproduce the effect in your previous mail ... > > > 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= . > > The following code fragment in libxo.c does suppress the terminating > new-line > character on purpose if depth !=3D 0: > > case XO_STYLE_JSON: > pre_nl =3D XOF_ISSET(xop, XOF_PRETTY) ? "\n" : ""; > ppn =3D (xop->xo_depth <=3D 1) ? "\n" : ""; > > xo_depth_change(xop, name, -1, -1, XSS_CLOSE_CONTAINER, 0); > rc =3D xo_printf(xop, "%s%*s}%s", pre_nl, xo_indent(xop), "", ppn= ); > xop->xo_stack[xop->xo_depth].xs_flags |=3D XSF_NOT_FIRST; > break; > > The reason appears to be, that closing the outer level construct will pri= nt > a new-line before other text. If you omit the second "xo --wrap", > everything > will look OK. And if you test with --depth 0, you'll see that the output = is > actually terminated with a new-line. > > > 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 w= ay > > to specify that a single call should be output as a part of a list. > > I do not see a better solution than adding an "echo" command between call= s > to "xo --wrap", as you already mentioned as a work-around. This is simple= r > than adding list_open/close functions and it has the same effect, if you > know that you are emitting JSON. > > Best regards, STefan >