Alternative to overriding Module::fromJson?

A few of the modules in my computerscare plugin currently override ModuleWidget::toJson and ModuleWidget::fromJson to handle the serialization of custom data. I now realize this is not the proper approach (and will break the functionality of the modules with v2’s headless mode), and that this should be handled in Module::dataToJson and Module::dataFromJson.

My problem is that all the current patches have my custom data saved in the root of the JSON serialization of the module like this:

 {
      "id": 2789,
      "plugin": "computerscare",
      "version": "1.2.0",
      "model": "computerscare-ohpeas",
      "params": [...],

      "sequences": "221223",
      "other-custom-data":"...",

      "pos": [
        0,
        0
      ]
    }

I know that I need to move all this serialization to the Module, and I don’t see any alternatives to overriding Module::fromJson because Module.cpp (line 175-177) only calls Module::dataFromJson if it finds a “data” key at the root of the JSON serialization:

json_t* dataJ = json_object_get(rootJ, "data");
	if (dataJ)
		dataFromJson(dataJ);

I don’t like the idea of overriding Module::fromJson because it could change in the future. Any ideas how I can properly grab my custom data from the root JSON of legacy patches from the Module part of my code? Thanks in advance!

In Rack v2, you can override Module::to/fromJson, but the use case will be so rare, you should ask me for review before releasing a Module subclass that overrides it.

1 Like

Will the deprecated ModuleWidget::fromJson still be available in the Rack v2 API?

ModuleWidget::toJson/fromJson() will exist but be non-virtual. However, you should never need to override it. If you have a rare use case where you need to read/write the .modules[] JSON object instead of just .modules[].data, you can use Module::toJson/fromJson()

1 Like

Reminder of the problem: my existing v1 modules save data in the root of the module JSON serialization. In Rack v2, would this be an acceptable solution to access the root JSON?

MyModule.cpp:

void MyModule::fromJson(json_t* rootJ) override {
  myCustomFunctionThatNeedsToAccessRootJSON(rootJ);
  Module::fromJson(rootJ);
}

Yes.

1 Like