Can't use ArduinoJSON data in an ArduinnoSTL map

132 views Asked by At

I am trying to decode JSON using ArduinoJSON, the results need to be stored in a map defined by ArduinoSTL The document is structured like this;

{
 "type":"init",
 "anchors": [
     {
       "id", "xyz",
       "loc": {
         "x": 10,
         "y": 30
       }
     }
     ...
   ]
}

The document needs to be passed to a function. Every anchor needs to update or insert into the anchors map

class Location {
  public:
    int x;
    int y;
    Location() : x(0), y(0) {}
    Location(int x, int y) : x(x), y(y) {}
};

std::map<String, Location> anchors{};

The function looks like this;

void AP_init(const JsonDocument& doc) {
  const JsonArray& arr = doc["anchors"].as<JsonArray>();
  for (const JsonObject anchor : arr) {
    // I tried insert_or_assign but the function was not found.
    anchors.insert(anchor["id"].as<const char*>(), Location(anchor["loc"]["x"].as<int>(), anchor["loc"]["y"].as<int>()) );
  }
}

The following error appears;

error: no matching function for call to 'std::map<String, Location>::insert(ArduinoJson6194_F1::enable_if<true, const char*>::type, Location)'

EDIT: Changed to

String id = anchor["id"];
anchors.insert(id, Location(anchor["loc"]["x"].as<int>(), anchor["loc"]["y"].as<int>()) );

Throws

error: no matching function for call to 'std::map<String, Location>::insert(String&, Location)'

EDIT: Tried

anchors.insert(String(), Location() );

Throws

error: no matching function for call to 'std::map<String, Location>::insert(String, Location)'

Looks like the Arduino implementation of STL is incomplete.

0

There are 0 answers