You'll prefer JSON the minute it becomes:
cpu: 0.2
kb_read_s: 0.12
mac_addr: 10:AA:FF:00:55:66 cpu: 0.2
kb_read_s: 0.12
mac_addr: 10:AA:FF:00:55:66 dict((key.strip(), val.strip()) for key, _, val in line.strip().partition(‘:’) for line in text.split(‘\n’) if line and line.strip())
Also who says we can’t have a universal parser for this format just like we have for JSON? Not everyone needs to write the one liner like above, just use the libtextformat.parse(text) or whatever we would call it. {program -> human-text -> parser}+
When it could look like this: program
-> {struct-text -> program}+
[-> formatter] -> human-text . NAME="Rocky Linux"
VERSION="8.5 (Green Obsidian)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Rocky Linux 8.5 (Green Obsidian)"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:rocky:rocky:8.5:GA"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
ROCKY_SUPPORT_PRODUCT="Rocky Linux"
ROCKY_SUPPORT_PRODUCT_VERSION="8"
On the surface this seems great, but those quotation marks are kind of annoying. Is it possible there's an escape syntax that's used in case the name also includes quotes? eg VERSION="8.5 (Green \"Aqua\" Obsidian)"? Is it also possible you can embed newlines in between the quotes too? Who knows... Thankfully with JSON there is a simple spec. cpu: 0.2
kw_read_s: 0.12
is describing a single 'object'. How do you describe an array or list of these objects? Something like: cpu: 0.2
kw_read_s: 0.12
cpu: 1.3
kw_read_s: 0.4
Do you use a blank line to denote a new 'object'? In JSON it would be done this way, and this conforms to how the vast majority of command output maps (as they tend to be rows of columnar data): [
{
"cpu": 0.2,
"kw_read_s": 0.12
},
{
"cpu": 1.3,
"kw_read_s": 0.4
}
]
The other benefit to JSON here is that the formatting doesn't matter. This could also be expressed as a block of text with no spaces or newlines between elements: [{"cpu": 0.2,"kw_read_s": 0.12},{"cpu": 1.3,"kw_read_s": 0.4}]
Finally, this can also be streamed, using JSON Lines: {"cpu": 0.2, "kw_read_s": 0.12}
{"cpu": 1.3, "kw_read_s": 0.4} json.parse(input)Type support is lacking. Not date support for example.
Unnecessary quotes around keys make it harder for humans to read.
Different implementations allow for repeated keys.
otherformat.parse(input) // just as reasonable cut -d: -f2-