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