2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-12-03 01:49:51 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-12-27 05:22:09 +03:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
2021-06-07 23:36:34 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-12-03 01:44:49 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <unistd.h>
|
2019-12-27 05:22:09 +03:00
|
|
|
|
|
|
|
static bool use_color = false;
|
2021-12-03 01:49:51 +03:00
|
|
|
static void print(StringView name, JsonValue const&, Vector<String>& trail);
|
2019-12-27 05:22:09 +03:00
|
|
|
|
2021-12-03 01:49:51 +03:00
|
|
|
static StringView color_name = ""sv;
|
|
|
|
static StringView color_index = ""sv;
|
|
|
|
static StringView color_brace = ""sv;
|
|
|
|
static StringView color_bool = ""sv;
|
|
|
|
static StringView color_null = ""sv;
|
|
|
|
static StringView color_string = ""sv;
|
|
|
|
static StringView color_off = ""sv;
|
2019-12-27 05:22:09 +03:00
|
|
|
|
2021-12-03 01:44:49 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-12-27 05:22:09 +03:00
|
|
|
{
|
2021-12-03 01:44:49 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty"));
|
2020-02-11 14:42:06 +03:00
|
|
|
|
|
|
|
if (isatty(STDOUT_FILENO))
|
|
|
|
use_color = true;
|
|
|
|
|
2021-12-03 01:44:49 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-02-11 14:42:06 +03:00
|
|
|
|
2021-06-07 23:36:34 +03:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Print each value in a JSON file with its fully expanded key.");
|
|
|
|
|
2021-12-03 01:49:51 +03:00
|
|
|
StringView path;
|
2021-06-07 23:36:34 +03:00
|
|
|
args_parser.add_positional_argument(path, "Input", "input", Core::ArgsParser::Required::No);
|
2021-12-03 01:44:49 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-06-07 23:36:34 +03:00
|
|
|
|
|
|
|
RefPtr<Core::File> file;
|
|
|
|
|
2021-12-03 01:49:51 +03:00
|
|
|
if (path.is_null())
|
2021-06-07 23:36:34 +03:00
|
|
|
file = Core::File::standard_input();
|
2021-12-03 01:44:49 +03:00
|
|
|
else
|
|
|
|
file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
|
2019-12-27 05:22:09 +03:00
|
|
|
|
2021-12-03 01:44:49 +03:00
|
|
|
TRY(Core::System::pledge("stdio"));
|
2020-02-11 14:42:06 +03:00
|
|
|
|
2019-12-27 05:22:09 +03:00
|
|
|
auto file_contents = file->read_all();
|
2021-12-03 01:44:49 +03:00
|
|
|
auto json = TRY(JsonValue::from_string(file_contents));
|
2019-12-27 05:22:09 +03:00
|
|
|
|
|
|
|
if (use_color) {
|
2021-12-03 01:49:51 +03:00
|
|
|
color_name = "\033[33;1m"sv;
|
|
|
|
color_index = "\033[35;1m"sv;
|
|
|
|
color_brace = "\033[36m"sv;
|
|
|
|
color_bool = "\033[32;1m"sv;
|
|
|
|
color_string = "\033[31;1m"sv;
|
|
|
|
color_null = "\033[34;1m"sv;
|
|
|
|
color_off = "\033[0m"sv;
|
2019-12-27 05:22:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> trail;
|
2021-12-03 01:49:51 +03:00
|
|
|
print("json"sv, json, trail);
|
2019-12-27 05:22:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-03 01:49:51 +03:00
|
|
|
static void print(StringView name, JsonValue const& value, Vector<String>& trail)
|
2019-12-27 05:22:09 +03:00
|
|
|
{
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t i = 0; i < trail.size(); ++i)
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{}", trail[i]);
|
2019-12-27 05:22:09 +03:00
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{}{}{} = ", color_name, name, color_off);
|
2019-12-27 05:22:09 +03:00
|
|
|
|
|
|
|
if (value.is_object()) {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("{}{{}}{};", color_brace, color_off);
|
2021-04-21 23:40:20 +03:00
|
|
|
trail.append(String::formatted("{}{}{}.", color_name, name, color_off));
|
2019-12-27 05:22:09 +03:00
|
|
|
value.as_object().for_each_member([&](auto& on, auto& ov) { print(on, ov, trail); });
|
|
|
|
trail.take_last();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value.is_array()) {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("{}[]{};", color_brace, color_off);
|
2021-04-21 23:40:20 +03:00
|
|
|
trail.append(String::formatted("{}{}{}", color_name, name, color_off));
|
2021-06-28 12:57:37 +03:00
|
|
|
for (size_t i = 0; i < value.as_array().size(); ++i) {
|
2021-04-21 23:40:20 +03:00
|
|
|
auto element_name = String::formatted("{}{}[{}{}{}{}{}]{}", color_off, color_brace, color_off, color_index, i, color_off, color_brace, color_off);
|
2019-12-27 05:22:09 +03:00
|
|
|
print(element_name, value.as_array()[i], trail);
|
|
|
|
}
|
|
|
|
trail.take_last();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (value.type()) {
|
|
|
|
case JsonValue::Type::Null:
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{}", color_null);
|
2019-12-27 05:22:09 +03:00
|
|
|
break;
|
|
|
|
case JsonValue::Type::Bool:
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{}", color_bool);
|
2019-12-27 05:22:09 +03:00
|
|
|
break;
|
|
|
|
case JsonValue::Type::String:
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{}", color_string);
|
2019-12-27 05:22:09 +03:00
|
|
|
break;
|
|
|
|
default:
|
2021-05-31 17:43:25 +03:00
|
|
|
out("{}", color_index);
|
2019-12-27 05:22:09 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("{}{};", value.serialized<StringBuilder>(), color_off);
|
2019-12-27 05:22:09 +03:00
|
|
|
}
|