From owner-freebsd-hackers@freebsd.org Sat Sep 8 13:41:08 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 BCA6CFF442D for ; Sat, 8 Sep 2018 13:41:07 +0000 (UTC) (envelope-from churchers@gmail.com) Received: from mail-ua1-x933.google.com (mail-ua1-x933.google.com [IPv6:2607:f8b0:4864:20::933]) (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 5929994A14 for ; Sat, 8 Sep 2018 13:41:07 +0000 (UTC) (envelope-from churchers@gmail.com) Received: by mail-ua1-x933.google.com with SMTP id 101-v6so14034855uav.7 for ; Sat, 08 Sep 2018 06:41:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=NoVQcVvUc1uFoTPRJPp1msKPS24C+8fsqp3JLms5HGg=; b=VlW+MGltVnTwEadLc48VYU5GNct5wYlKky/MWhkRzezx1Ea7xgIK+XK1BUJ8wa030R TqA3WbQvcdxmpQ9wHZ65AJn6iuovXaicUU1SuIdzKjxghMqbBhyb9fGBY2rIf9zfRiaj QRNiNGXgMh1AelpV+6qXof3p2uJyb9eoqUzO8p/oI7TE9d7Gj+ve59jeyDt4TRHEM2Nx VQS2A87hwTT27K5DE2ifPEWTbxNxwE+/n3N0Bguc2WTPxmIiwyalPDziqqrR9vamtyzY bb+yBpOuzeVXm+e0MC8422XPP0dRNUGZMVxmdphRq2YcrLN4lmx84PzWzw+gVVcfrCUh djVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=NoVQcVvUc1uFoTPRJPp1msKPS24C+8fsqp3JLms5HGg=; b=IL2PXClvcjfDXZEaCvu5O4Tl+R9ZFultlqv1D9Z+vPyW+t1egzdt+t0Kxnw4naGyAi jPfOEje1W2kyptoV0EADCNPmw50hRaamRnVbOVH1/suoCF7QVpmGkzm6pbtUzBwGVhym R7KV3GnfbzLi0+5z/RxbG20RDGbJa+N/mZpwHrgYEhQYBcMNPaVuigh26ni5AF+k0Vgo 9k3cWPkTrSKGmxYKRvnYf3gyJxwWR3imhTpfKcm7yWhC+XXQJduSJh2BzJeY9hlZIJOM dWGAsPa/gnxetJOC8m+Y3NVNHSoP7W8pS2DiyvY2CpolVGlDOLrnyWSpJqOmlD7HjlsN n3Gw== X-Gm-Message-State: APzg51AfDvvB5y+3dbbuznckob7WcdKUt9fcabxBJ4JSFGzFnh8RKjEn 4xmL0qUSVRIcDN4iafzqPHWstd+yhU/aCjr04rDvYObp X-Google-Smtp-Source: ANB0VdZr3tiPDptt4x7R+5KolIXW8ZSaXtU1Vxitr0hKqRVwCQbTfuUsIOcgYWsCtRFVwr9oa/RtUPJori7nIeFxwAc= X-Received: by 2002:a67:3515:: with SMTP id c21-v6mr4497303vsa.42.1536414066586; Sat, 08 Sep 2018 06:41:06 -0700 (PDT) MIME-Version: 1.0 From: Matt Churchyard Date: Sat, 8 Sep 2018 14:41:10 +0100 Message-ID: Subject: Getting valid JSON output with xo(1) To: freebsd-hackers@freebsd.org X-Mailman-Approved-At: Sat, 08 Sep 2018 14:13:47 +0000 Content-Type: text/plain; charset="UTF-8" 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: Sat, 08 Sep 2018 13:41:08 -0000 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 - test1 2GB test2 4GB If I choose HTML output I get the following invalid output which is missing closing tags.
test1
,
2GB
test2
,
4GB
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 - With JSON specified I get the following output, which has 3 major issues: "bhyve": { "machines": { "machine": { "name": "test1", "memory": "2GB" } "machine": { "name": "test2", "memory": "4GB" } } } 1) The entire document needs to be inside braces "{}" so that "bhyve" is a member of an object. 2) There's a missing comma between the two machine entries 3) "machines" is an object, which erroneously contains 2 "machine" keys (There's also a 4th issue that the second "machine" entry really should be on the line below, but that's just a minor visual thing) Now 1 & 2 can be fixed fairly easily by just outputting the brace and comma characters myself. (I do also appreciate that in order to output the commas in the correct place, xo would need to either get all the records in one go, or be told which are part of a list and which are first or last). However it seems to me that xo should handle being given the exact same input, and produce valid HTML/JSON or XML just by the user specifying their output choice. I shouldn't really have to inject additional characters myself or alter the input for certain formats in order to get valid output. The 3rd issue is a bit more awkward. The xo(1) man page is fairly short and I can't see any obvious way that I can get it to produce a basic array (square brackets - []) rather than an object, meaning that it doesn't seem possible to create a list of objects. This works with XML due to a list being made up by effectively just repeating the same element. If I can't create a list with JSON, it seems to severely limit the usefulness of the JSON option (Which unfortunately is the most desired format). I could possibly use the machine name as the object key, but this isn't ideal, and again means I'll need to start running different xo commands based on the desired output format. I don't know if libxo can produce JSON arrays if you're actually using the proper API, but these problems (If I'm not already missing something obvious) could probably be fixed fairly easily if there was some option to xo to specify when it should create a list, and whether the current call is the first entry or not. Regards, Matt