WiFiManager icon indicating copy to clipboard operation
WiFiManager copied to clipboard

Question about AutoConnectWithFSParameters.ino code example

Open sgrass-valhalla opened this issue 3 years ago • 0 comments

Hi all, sorry for the newbie question, I'm trying to understand the code example here: https://github.com/tzapu/WiFiManager/blob/master/examples/Parameters/SPIFFS/AutoConnectWithFSParameters/AutoConnectWithFSParameters.ino

specifically this code segment:

#ifdef ARDUINOJSON_VERSION_MAJOR >= 6
        DynamicJsonDocument json(1024);
        auto deserializeError = deserializeJson(json, buf.get());
        serializeJson(json, Serial);
        if ( ! deserializeError ) {
#else
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
#endif
          Serial.println("\nparsed json");
          strcpy(mqtt_server, json["mqtt_server"]);
          strcpy(mqtt_port, json["mqtt_port"]);
          strcpy(api_token, json["api_token"]);
        } else {
          Serial.println("failed to load json config");
        }
        configFile.close();

I'm not familiar with how the #ifdef-#else-#endif structure works. I understand the logic here, but the code has more open parentheses '{' than closing ones '}', and I'm getting complications errors when I try to integrate this code with mine. Lines if ( ! deserializeError ) { and if (json.success()) { are not supposed to be used together (due to the #ifdef) but they are both closed by } else { and it seems that the compiler expects both '{' to be closed.

Explanations appreciated. tx

sgrass-valhalla avatar Feb 19 '22 11:02 sgrass-valhalla