Skip to content

Possibility to use a JsonArray instead of JsonObject in read/update method #326

@spyflox

Description

@spyflox

Actually, I have the following definition:

struct Sensor {
  int value;
  int type;
};
class SensorState {
 public:
  Sensor sensors[4];

  static void read(SensorState& settings, JsonObject& root) {
    for (int i = 0; i < 4; i++) {
      root["value"][i] = settings.sensors[i].value;
      root["type"][i] = settings.sensors[i].type;
    }
  }
...
};

I would like to use a JSON array instead of an object to have code that looks more like this:

class SensorState {
 public:
  Sensor sensors[4];

  static void read(SensorState& settings, JsonArray& root) {
    for (int i = 0; i < 4; i++) {
      root[i]["value"] = settings.sensors[i].value;
      root[i]["type"] = settings.sensors[i].type;
    }
  }
...
};

Do you know how I can do that?
I tried modifying StatefullService to accept arrays, but I confess I'm not proficient enough in C++ to accomplish it.

I love this framework and I congratulate @rjwats once again for creating it.
It would be great in my case if I could pass arrays instead of objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions