diff options
author | Linus Groh <mail@linusgroh.de> | 2020-12-14 18:50:07 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-14 20:25:01 +0100 |
commit | b04e0a7677960bb8d80966b01c592572bdb38281 (patch) | |
tree | 54013797ef16cb3edf0ef13358a5c582a72e3803 | |
parent | c07176bd533f4453a7cb0761ed349d45ec0a6fee (diff) | |
download | serenity-b04e0a7677960bb8d80966b01c592572bdb38281.zip |
jp: Remove trailing comma at end of object/array to make output valid JSON
It's ironic that `jp` currently refuses to parse its own output :^)
-rw-r--r-- | Userland/jp.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Userland/jp.cpp b/Userland/jp.cpp index 7eefbcf3fa..cb7bef1f95 100644 --- a/Userland/jp.cpp +++ b/Userland/jp.cpp @@ -82,26 +82,36 @@ int main(int argc, char** argv) void print(const JsonValue& value, int indent, bool use_color) { if (value.is_object()) { + size_t printed_members = 0; + auto object = value.as_object(); outln("{{"); - value.as_object().for_each_member([&](auto& member_name, auto& member_value) { + object.for_each_member([&](auto& member_name, auto& member_value) { + ++printed_members; print_indent(indent + 1); if (use_color) out("\"\033[33;1m{}\033[0m\": ", member_name); else out("\"{}\": ", member_name); print(member_value, indent + 1, use_color); - outln(","); + if (printed_members < static_cast<size_t>(object.size())) + out(","); + outln(); }); print_indent(indent); out("}}"); return; } if (value.is_array()) { + size_t printed_entries = 0; + auto array = value.as_array(); outln("["); - value.as_array().for_each([&](auto& entry_value) { + array.for_each([&](auto& entry_value) { + ++printed_entries; print_indent(indent + 1); print(entry_value, indent + 1, use_color); - outln(","); + if (printed_entries < static_cast<size_t>(array.size())) + out(","); + outln(); }); print_indent(indent); out("]"); |