[GH-ISSUE #1364] Question about AutoConnectWithFSParameters.ino code example #1170

Open
opened 2026-02-28 01:28:50 +03:00 by kerem · 0 comments
Owner

Originally created by @sgrass-valhalla on GitHub (Feb 19, 2022).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1364

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

Originally created by @sgrass-valhalla on GitHub (Feb 19, 2022). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1364 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
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#1170
No description provided.