Date: Sat, 8 Sep 2018 14:41:10 +0100 From: Matt Churchyard <churchers@gmail.com> To: freebsd-hackers@freebsd.org Subject: Getting valid JSON output with xo(1) Message-ID: <CANV9Nz=3aOd5RM9KyTfz-1_%2BEnffKEOVQbYZxJY=i9_x0gorpA@mail.gmail.com>
next in thread | raw e-mail | index | archive | help
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 - 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANV9Nz=3aOd5RM9KyTfz-1_%2BEnffKEOVQbYZxJY=i9_x0gorpA>