1

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.

CarbonMan
  • 4,350
  • 12
  • 54
  • 75
  • Try to break down exactly where the error is. You have `std::map anchors{};`, so the obvious reason for the error is that the key you are inserting cannot be converted to a `String`. If you did `String s = anchor["id"].as();` does that compile? If not, that's where you should start. – PaulMcKenzie Nov 28 '22 at 08:55
  • And note, I have never used Arduino, or the Json library you are using. Just simple "dvide and conquer" debugging to see where the issue is. – PaulMcKenzie Nov 28 '22 at 08:56
  • Thanks for the reply, I've edited the question as I tried "String id = anchor["id"];" but it still falls over on the insert. – CarbonMan Nov 28 '22 at 10:56
  • Then try the simplest thing you can think of: `anchors.insert(String(), Location());` -- That has to work -- if that doesn't work, then there is something very strange going on. A `String` should be copyable and assignable to make it eligible to be a key in a `std::map`. – PaulMcKenzie Nov 28 '22 at 13:49
  • Thanks, tried your suggestion and Looks like the Arduino implementation of STL is incomplete. – CarbonMan Nov 28 '22 at 22:17

0 Answers0