From owner-freebsd-hackers@freebsd.org Sun Sep 9 16:13:20 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 1BC971097FC0 for ; Sun, 9 Sep 2018 16:13:20 +0000 (UTC) (envelope-from se@freebsd.org) Received: from mailout10.t-online.de (mailout10.t-online.de [194.25.134.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mailout00.t-online.de", Issuer "TeleSec ServerPass DE-2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 9C352771AD for ; Sun, 9 Sep 2018 16:13:19 +0000 (UTC) (envelope-from se@freebsd.org) Received: from fwd11.aul.t-online.de (fwd11.aul.t-online.de [172.20.27.152]) by mailout10.t-online.de (Postfix) with SMTP id EA2D241F02AF; Sun, 9 Sep 2018 18:13:11 +0200 (CEST) Received: from Stefans-MBP-LAN.fritz.box (TD4y4yZaYhWmllzK7eg2RqibhcA8S40ULB5Gb5Vp8+ByWpWGOYFYvOtS3T-9AjTgSM@[93.200.59.58]) by fwd11.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384 encrypted) esmtp id 1fz2Kh-1sL3tA0; Sun, 9 Sep 2018 18:13:07 +0200 Subject: Re: Getting valid JSON output with xo(1) To: Matt Churchyard , freebsd-hackers@freebsd.org References: From: Stefan Esser Openpgp: preference=signencrypt Autocrypt: addr=se@freebsd.org; prefer-encrypt=mutual; keydata= xsBNBFVxiRIBCADOLNOZBsqlplHUQ3tG782FNtVT33rQli9EjNt2fhFERHIo4NxHlWBpHLnU b0s4L/eItx7au0i7Gegv01A9LUMwOnAc9EFAm4EW3Wmoa6MYrcP7xDClohg/Y69f7SNpEs3x YATBy+L6NzWZbJjZXD4vqPgZSDuMcLU7BEdJf0f+6h1BJPnGuwHpsSdnnMrZeIM8xQ8PPUVQ L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RLnrB/9gzcRlpx71sDMosoZULWn7wysBJ/8AIEfIByRaHQe3 pn/KwE57pB+zFbbQqB7YzeZb7/UUgR4zU2ZbOcEfwDZcHUbj0B3fGRsS3t0uiLlAd8w0sBZb SxrqzjdpDjIbOZkxssqUmvrsN67UG1AFWH9aD24keBS7YjPBS8hLxPeYV+Xz6vUL8fRZje/Z JgiBMIwyj6g2lH/zkdnxBdC0iG1xxJOLTaghMMeQyCdH6ef8+VMyAlAJsMckbOTvx63tY8z7 DFcrnTJfbe1EziRilVsEaK8tTzJzhcTfos+f3eBYWEilxe5HzIhYKJeC7lmsSUcGwa6+9VRg a0ctmi9Z8OgX Message-ID: <228b2a88-6134-c8c2-a28d-cb09cbc03ef6@freebsd.org> Date: Sun, 9 Sep 2018 18:13:04 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit X-ID: TD4y4yZaYhWmllzK7eg2RqibhcA8S40ULB5Gb5Vp8+ByWpWGOYFYvOtS3T-9AjTgSM X-TOI-MSGID: 3bc294a9-15da-4ede-8e06-901306ab1095 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: Sun, 09 Sep 2018 16:13:20 -0000 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 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. 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 != 0: case XO_STYLE_JSON: pre_nl = XOF_ISSET(xop, XOF_PRETTY) ? "\n" : ""; ppn = (xop->xo_depth <= 1) ? "\n" : ""; xo_depth_change(xop, name, -1, -1, XSS_CLOSE_CONTAINER, 0); rc = xo_printf(xop, "%s%*s}%s", pre_nl, xo_indent(xop), "", ppn); xop->xo_stack[xop->xo_depth].xs_flags |= XSF_NOT_FIRST; break; The reason appears to be, that closing the outer level construct will print 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 way > 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 calls to "xo --wrap", as you already mentioned as a work-around. This is simpler than adding list_open/close functions and it has the same effect, if you know that you are emitting JSON. Best regards, STefan