To/From JSON Question

Hey, I’m having difficulty learning how to send/receive JSON objects, Only one text field communicating with JSON.

struct MpMapperWidget : ModuleWidget {
    TextField* textField;

    json_t* toJson() override {
        json_t* rootJ = ModuleWidget::toJson();

        json_object_set_new(rootJ, "text", json_string(textField->text.c_str()));

        return rootJ;
    }
    
    
    void fromJson(json_t* rootJ) override {
        ModuleWidget::fromJson(rootJ);

                json_t* textJ = json_object_get(rootJ, "text");
        if (textJ)
            textField->text = json_string_value(textJ);
    }
textField = createWidget<LedDisplayTextField>(Vec(10.2,81));
textField->box.size = mm2px(Vec(84.5, 12));
textField->multiline = true;
addChild(textField);

textField = createWidget<LedDisplayTextField>(Vec(10.2,177));
textField->box.size = mm2px(Vec(84.5, 12));
textField->multiline = true;
addChild(textField);

textField = createWidget<LedDisplayTextField>(Vec(10.2,273));
textField->box.size = mm2px(Vec(84.5, 12));
textField->multiline = true;
addChild(textField);

1.how I define each textField by a name? 2. best way to send and receive all TextField?

I hope you understand what I’m asking, Thank you.

ModuleWidget::toJson() and fromJson() are deprecated. Use Module::dataToJson() and dataToJson().

of curse I forgot to mention I need a further help,

struct MpMapperWidget : ModuleWidget {
    TextField* textField;

    json_t* toJson() override {
        json_t* rootJ = Module::dataToJson();

        json_object_set_new(rootJ, "text", json_string(textField->text.c_str()));

        return rootJ;
    }
    
    
    void fromJson(json_t* rootJ) override {
        Module::dataFromJson(rootJ);

                json_t* textJ = json_object_get(rootJ, "text");
        if (textJ)
            textField->text = json_string_value(textJ);
    }```


error: call to non-static member function without an object argument Module::dataFromJson();

Just override the virtual dataToJson() and dataFromJson() in your module, instead of trying to call them from other functions. The calling is done by Rack, you just provide the functions

json_t* dataToJson() override {
  json_t *rootJ = json_object();
  json_object_set_new(rootJ, "text", json_string(textField->text));
  return rootJ;
}
void dataFromJson(json_t* rootJ) override {  
  json_t* textJ = json_object_get(rootJ, "text");
  if (textJ)
    textField->text = json_string_value(textJ);
}

1 Like

Ok,thanks. 2 errors I know that might be funny on annoying, but because im just getting started with code Im spending too much time or silly things, really thanks for everyone who help and helped me:)

something I do wrong?

src/MpMapper.cpp:88:26: error: only virtual member functions can be marked
      'override'
    json_t* dataToJson() override {
                         ^~~~~~~~~
src/MpMapper.cpp:98:38: error: only virtual member functions can be marked
      'override'
    void dataFromJson(json_t* rootJ) override {
                                     ^~~~~~~~~

You’re probably trying to override methods in your ModuleWidget subclass. You need to override those methods in your Module subclass.

1 Like

alright thanks, after all attempts, now the most stupid error, src/MpMapper.cpp:92:1: error: expected unqualified-id

{

^

so I uploaded to GitHub my first repository, I hope someone can review MpMapper.cpp and fix this stupid error, so we can push this to the library:)

Oh my, this is no code for easy reading…

These two functions must be placed in the scope of struct MpMapper. It still won’t compile afterwards as you can’t access textField from MpMapper.

1 Like