Koromix / tytools

Collection of tools to manage Teensy boards
https://koromix.dev/tytools
The Unlicense
215 stars 27 forks source link

Output JSON Array Correction. #40

Closed mrrees closed 5 years ago

mrrees commented 5 years ago

I am using the JSON output to query and catalog the teensy boards in my project. however the JSON output to PHP array needs formatting before it will be accepted. I also checked the JSON output with a validation tool and It was complaining about the delimiter.

Not an major issue but one that may help others that want error free JSON integration. For those who are stuck you can modify your output by formatting the header, delimiters and footer with your processing code with the following format.

The format I was able to manipulate in PHP and get it to work was to add "[" before and "]" after the JSON output, but also between each teensy detail I also had to replace "} {" with "}, {" (please note the space between } { is a /n new line character)

Example Output from JSON currently is: $output = '{"action": "add", "tag": "23797840-Teensy", "serial": 23797840, "description": "Teensy MIDI", "model": "Teensy", "location": "usb-1-1-1-3", "capabilities": ["unique", "run", "reboot", "serial"], "interfaces": [["Serial", "/dev/ttyACM0"]]} {"action": "add", "tag": "23797820-Teensy", "serial": 23797820, "description": "Teensy MIDI", "model": "Teensy", "location": "usb-1-1-1-2", "capabilities": ["unique", "run", "reboot", "serial"], "interfaces": [["Serial", "/dev/ttyACM1"]]}';

Changing to this format works with the PHP JSON convert to array and passes JSON validation.

$output = '[{"action": "add", "tag": "23797840-Teensy", "serial": 23797840, "description": "Teensy MIDI", "model": "Teensy", "location": "usb-1-1-1-3", "capabilities": ["unique", "run", "reboot", "serial"], "interfaces": [["Serial", "/dev/ttyACM0"]] }, { "action": "add", "tag": "23797820-Teensy", "serial": 23797820, "description": "Teensy MIDI", "model": "Teensy", "location": "usb-1-1-1-2", "capabilities": ["unique", "run", "reboot", "serial"], "interfaces": [["Serial", "/dev/ttyACM1"]]}]';

Also if there is a single board detected I believe you still want to add "[" before and "]" after the JSON output.

Incredible project! thank you for providing such a great and valuable tool.

Koromix commented 5 years ago

Hi,

The format output by tcmd (until the latest commit I just pushed) was a line-oriented JSON stream (supported by many command line-oriented JSON tools, such as jq), with one JSON object per line. I did it because tycmd supports a watch mode with tycmd list -w, which cannot output a proper JSON array because it never ends.

I've changed the code in the latest commit:

There is a Win64 build here: https://koromix.dev/files/tytools