1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:48:12 +00:00

AK+Spreadsheet+LibWeb: Remove JsonObject::get_or()

This removes JsonObject::get_or(), which is inefficient because it has
to copy the returned value. It was only used in a few cases, some of
which resulted in copying JsonObjects, which can become quite large.
This commit is contained in:
Max Wipfli 2021-06-28 12:40:04 +02:00 committed by Andreas Kling
parent e82611bb87
commit 9c8a2a5f69
4 changed files with 91 additions and 97 deletions

View file

@ -59,12 +59,6 @@ public:
return *value;
}
JsonValue get_or(String const& key, JsonValue const& alternative) const
{
auto* value = get_ptr(key);
return value ? *value : alternative;
}
JsonValue const* get_ptr(String const& key) const
{
auto it = m_members.find(key);

View file

@ -91,18 +91,18 @@ HelpWindow::HelpWindow(GUI::Window* parent)
auto& doc = doc_option.as_object();
const auto& name = url.fragment();
auto example_data_value = doc.get_or("example_data", JsonObject {});
if (!example_data_value.is_object()) {
auto* example_data_ptr = doc.get_ptr("example_data");
if (!example_data_ptr || !example_data_ptr->is_object()) {
GUI::MessageBox::show_error(this, String::formatted("No example data found for '{}'", url.path()));
return;
}
auto& example_data = example_data_ptr->as_object();
auto& example_data = example_data_value.as_object();
auto value = example_data.get(name);
if (!value.is_object()) {
if (!example_data.has_object(name)) {
GUI::MessageBox::show_error(this, String::formatted("Example '{}' not found for '{}'", name, url.path()));
return;
}
auto& value = example_data.get(name);
auto window = GUI::Window::construct(this);
window->resize(size());
@ -138,21 +138,15 @@ HelpWindow::HelpWindow(GUI::Window* parent)
String HelpWindow::render(const StringView& key)
{
auto doc_option = m_docs.get(key);
VERIFY(doc_option.is_object());
auto& doc = doc_option.as_object();
VERIFY(m_docs.has_object(key));
auto& doc = m_docs.get(key).as_object();
auto name = doc.get("name").to_string();
auto argc = doc.get("argc").to_u32(0);
auto argnames_value = doc.get("argnames");
VERIFY(argnames_value.is_array());
auto& argnames = argnames_value.as_array();
VERIFY(doc.has_array("argnames"));
auto& argnames = doc.get("argnames").as_array();
auto docstring = doc.get("doc").to_string();
auto examples_value = doc.get_or("examples", JsonObject {});
VERIFY(examples_value.is_object());
auto& examples = examples_value.as_object();
StringBuilder markdown_builder;
@ -184,9 +178,11 @@ String HelpWindow::render(const StringView& key)
markdown_builder.append(docstring);
markdown_builder.append("\n\n");
if (!examples.is_empty()) {
if (doc.has("examples")) {
auto& examples = doc.get("examples");
VERIFY(examples.is_object());
markdown_builder.append("# EXAMPLES\n");
examples.for_each_member([&](auto& text, auto& description_value) {
examples.as_object().for_each_member([&](auto& text, auto& description_value) {
dbgln("- {}\n\n```js\n{}\n```\n", description_value.to_string(), text);
markdown_builder.appendff("- {}\n\n```js\n{}\n```\n", description_value.to_string(), text);
});

View file

@ -343,10 +343,8 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
auto rows = object.get("rows").to_u32(default_row_count);
auto columns = object.get("columns");
auto name = object.get("name").as_string_or("Sheet");
auto cells_value = object.get_or("cells", JsonObject {});
if (!cells_value.is_object())
return nullptr;
auto& cells = cells_value.as_object();
if (object.has("cells") && !object.has_object("cells"))
return {};
sheet->set_name(name);
@ -376,7 +374,8 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
format.background_color = Color::from_string(value.as_string());
};
cells.for_each_member([&](auto& name, JsonValue const& value) {
if (object.has_object("cells")) {
object.get("cells").as_object().for_each_member([&](auto& name, JsonValue const& value) {
auto position_option = sheet->parse_cell_name(name);
if (!position_option.has_value())
return IterationDecision::Continue;
@ -398,7 +397,7 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
}
}
auto type_name = obj.get_or("type", "Numeric").to_string();
auto type_name = obj.has("type") ? obj.get("type").to_string() : "Numeric";
cell->set_type(type_name);
auto type_meta = obj.get("type_metadata");
@ -447,6 +446,7 @@ RefPtr<Sheet> Sheet::from_json(const JsonObject& object, Workbook& workbook)
sheet->m_cells.set(position, cell.release_nonnull());
return IterationDecision::Continue;
});
}
return sheet;
}

View file

@ -100,10 +100,14 @@ bool is_pseudo_property(PropertyID property_id)
json.value().as_object().for_each_member([&](auto& name, auto& value) {
VERIFY(value.is_object());
auto pseudo = value.as_object().get_or("pseudo", false);
VERIFY(pseudo.is_bool());
bool pseudo = false;
if (value.as_object().has("pseudo")) {
auto& pseudo_value = value.as_object().get("pseudo");
VERIFY(pseudo_value.is_bool());
pseudo = pseudo_value.as_bool();
}
if (pseudo.as_bool()) {
if (pseudo) {
auto member_generator = generator.fork();
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(