From f1635cd96bce6cb214f26a7382abbe49a32e6ab9 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 2 Sep 2021 06:13:34 -0400 Subject: [PATCH] Add Module::jsonStripIds(). --- include/engine/Module.hpp | 3 ++- src/app/ModuleWidget.cpp | 16 +++++----------- src/app/RackWidget.cpp | 13 +++---------- src/engine/Module.cpp | 8 ++++++++ 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/include/engine/Module.hpp b/include/engine/Module.hpp index 9b0eb516..f0cf1ff3 100644 --- a/include/engine/Module.hpp +++ b/include/engine/Module.hpp @@ -327,7 +327,7 @@ struct Module { virtual json_t* dataToJson() { return NULL; } - virtual void dataFromJson(json_t* root) {} + virtual void dataFromJson(json_t* rootJ) {} /////////////////////// // Events @@ -433,6 +433,7 @@ struct Module { INTERNAL int meterLength(); INTERNAL int meterIndex(); INTERNAL void doProcess(const ProcessArgs& args); + INTERNAL static void jsonStripIds(json_t* rootJ); }; diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index 0bc6186b..0f7fb89c 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -459,12 +459,6 @@ void ModuleWidget::onDragHover(const DragHoverEvent& e) { OpaqueWidget::onDragHover(e); } -static void cleanupModuleJson(json_t* moduleJ) { - json_object_del(moduleJ, "id"); - json_object_del(moduleJ, "leftModuleId"); - json_object_del(moduleJ, "rightModuleId"); -} - json_t* ModuleWidget::toJson() { json_t* moduleJ = APP->engine->moduleToJson(module); return moduleJ; @@ -475,7 +469,7 @@ void ModuleWidget::fromJson(json_t* moduleJ) { } void ModuleWidget::pasteJsonAction(json_t* moduleJ) { - cleanupModuleJson(moduleJ); + engine::Module::jsonStripIds(moduleJ); json_t* oldModuleJ = toJson(); @@ -499,7 +493,7 @@ void ModuleWidget::pasteJsonAction(json_t* moduleJ) { void ModuleWidget::copyClipboard() { json_t* moduleJ = toJson(); - cleanupModuleJson(moduleJ); + engine::Module::jsonStripIds(moduleJ); DEFER({json_decref(moduleJ);}); char* json = json_dumps(moduleJ, JSON_INDENT(2)); @@ -539,7 +533,7 @@ void ModuleWidget::load(std::string filename) { throw Exception("File is not a valid patch file. JSON parsing error at %s %d:%d %s", error.source, error.line, error.column, error.text); DEFER({json_decref(moduleJ);}); - cleanupModuleJson(moduleJ); + engine::Module::jsonStripIds(moduleJ); fromJson(moduleJ); } @@ -613,7 +607,7 @@ void ModuleWidget::save(std::string filename) { assert(moduleJ); DEFER({json_decref(moduleJ);}); - cleanupModuleJson(moduleJ); + engine::Module::jsonStripIds(moduleJ); FILE* file = std::fopen(filename.c_str(), "w"); if (!file) { @@ -764,7 +758,7 @@ void ModuleWidget::cloneAction() { // JSON serialization is the obvious way to do this json_t* moduleJ = toJson(); - cleanupModuleJson(moduleJ); + engine::Module::jsonStripIds(moduleJ); // Clone Module engine::Module* clonedModule = model->createModule(); diff --git a/src/app/RackWidget.cpp b/src/app/RackWidget.cpp index 87c24df9..ce1297ee 100644 --- a/src/app/RackWidget.cpp +++ b/src/app/RackWidget.cpp @@ -387,13 +387,6 @@ void RackWidget::fromJson(json_t* rootJ) { } } -static void cleanupModuleJson(json_t* moduleJ) { - json_object_del(moduleJ, "id"); - json_object_del(moduleJ, "leftModuleId"); - json_object_del(moduleJ, "rightModuleId"); -} - - struct PasteJsonReturn { std::map newModuleIds; }; @@ -415,7 +408,7 @@ static PasteJsonReturn RackWidget_pasteJson(RackWidget* that, json_t* rootJ, his continue; int64_t id = json_integer_value(idJ); - cleanupModuleJson(moduleJ); + engine::Module::jsonStripIds(moduleJ); ModuleWidget* mw; try { @@ -524,7 +517,7 @@ void RackWidget::pasteJsonAction(json_t* rootJ) { } void RackWidget::pasteModuleJsonAction(json_t* moduleJ) { - cleanupModuleJson(moduleJ); + engine::Module::jsonStripIds(moduleJ); ModuleWidget* mw; try { @@ -929,7 +922,7 @@ void RackWidget::saveSelection(std::string path) { assert(rootJ); DEFER({json_decref(rootJ);}); - cleanupModuleJson(rootJ); + engine::Module::jsonStripIds(rootJ); FILE* file = std::fopen(path.c_str(), "w"); if (!file) { diff --git a/src/engine/Module.cpp b/src/engine/Module.cpp index df6452a5..c0ebd949 100644 --- a/src/engine/Module.cpp +++ b/src/engine/Module.cpp @@ -384,5 +384,13 @@ void Module::doProcess(const ProcessArgs& args) { } +void Module::jsonStripIds(json_t* rootJ) { + json_object_del(rootJ, "id"); + json_object_del(rootJ, "leftModuleId"); + json_object_del(rootJ, "rightModuleId"); +} + + + } // namespace engine } // namespace rack