Skip to content

Unable to deserialize. #285

Closed
Closed
@mayankraichura

Description

@mayankraichura

I'm trying to deserialize a json data but it is silently failing for me.

struct wifi_ap_config_t {
    const char* wifi_ssid;
    const char* wifi_ssid_password;
    bool wifi_static_ip;
    bool wifi_ssid_requires_password;
    const char* wifi_static_ip_address;
    const char* wifi_static_ip_subnet;
    const char* wifi_static_ip_gateway;
};

struct wifi_config_t {
    bool use_soft_ap;
    const char* soft_ap_ssid;
    const char* soft_ap_password;
    struct wifi_ap_config_t sta_ap_config;
};


struct wifi_config_t wc;

void wifi_deserialize_json(JsonObject& json) {
    wc.use_soft_ap = json["use_soft_ap"];   
    wc.soft_ap_ssid = json["soft_ap_ssid"];
    wc.soft_ap_password = json["soft_ap_password"];
    JsonObject& jsonSTA = json["sta_ap_config"].asObject();
    wc.sta_ap_config.wifi_ssid = jsonSTA["wifi_ssid"];
    wc.sta_ap_config.wifi_ssid_requires_password = jsonSTA["wifi_ssid_requires_password"].as<bool>();
    wc.sta_ap_config.wifi_ssid_password = jsonSTA["wifi_ssid_password"];
    wc.sta_ap_config.wifi_static_ip = jsonSTA["wifi_static_ip"].as<bool>();
    wc.sta_ap_config.wifi_static_ip_address = jsonSTA["wifi_static_ip_address"];
    wc.sta_ap_config.wifi_static_ip_subnet = jsonSTA["wifi_static_ip_subnet"];
    wc.sta_ap_config.wifi_static_ip_gateway = jsonSTA["wifi_static_ip_gateway"];
}

bool wifi_load_config() {
    Serial.printf("Loading wifi configuration from %s \n", c_wifi_config_path);
    if (_fs.exists(c_wifi_config_path)) {
        File cf = _fs.open(c_wifi_config_path, "r");
        if (cf) {
            size_t size = cf.size();

            if (size == 0) {
                Serial.println("Config file empty!"); return false;
            }

            if (size > 1024) {
                Serial.println("Config file too large."); return false;
            }

            // Allocate a buffer to store contents of the file.
            std::unique_ptr<char[]> buf(new char[size]);


            // We don't use String here because ArduinoJson library requires the input
            // buffer to be mutable. If you don't use ArduinoJson, you may as well
            // use configFile.readString instead.
            cf.readBytes(buf.get(), size);

            StaticJsonBuffer<500> jsonBuffer;
            JsonObject& json = jsonBuffer.parseObject(buf.get());

            if (!json.success()) {
                Serial.println("Failed to parse config file");
                return false;
            }

            Serial.printf("Reading from %s \n", c_wifi_config_path);
            json.prettyPrintTo(Serial);

            wifi_deserialize_json(json);

            cf.close();

        }
        else {
            Serial.printf("Could not log '%s'\n", c_wifi_config_path);
            return false;
        }


    }
    else {
        Serial.printf("Config file'%s' does not exist.", c_wifi_config_path);
        return false;
    }

    return true;
}

void dump_wc() {
    Serial.println("dumping wc");
    Serial.printf("wc.use_soft_ap: %s\n", b2yn(wc.use_soft_ap));
    Serial.printf("wc.soft_ap_ssid: %s\n", wc.soft_ap_ssid);
    Serial.printf("wc.soft_ap_password: %s\n", wc.soft_ap_password);
    Serial.printf("wc.sta_ap_config.wifi_ssid: %s\n", wc.sta_ap_config.wifi_ssid);
    Serial.printf("wc.sta_ap_config.wifi_ssid_requires_password: %s\n", b2yn(wc.sta_ap_config.wifi_ssid_requires_password));
    Serial.printf("wc.sta_ap_config.wifi_ssid_password: %s\n", wc.sta_ap_config.wifi_ssid_password);
    Serial.printf("wc.sta_ap_config.wifi_static_ip: %s\n", b2yn(wc.sta_ap_config.wifi_static_ip));
    Serial.printf("wc.sta_ap_config.wifi_static_ip_address: %s\n", wc.sta_ap_config.wifi_static_ip_address);
    Serial.printf("wc.sta_ap_config.wifi_static_ip_subnet: %s\n", wc.sta_ap_config.wifi_static_ip_subnet);
    Serial.printf("wc.sta_ap_config.wifi_static_ip_gateway: %s\n", wc.sta_ap_config.wifi_static_ip_gateway);
}

Here, as you can see wc is a global variable. But the structure remains empty even after calling deserialize routine.

After calling wifi_load_config(), here is the outpout:

dumping wc
wc.use_soft_ap: No
wc.soft_ap_ssid: <null>
wc.soft_ap_password: <null>
wc.sta_ap_config.wifi_ssid: <null>
wc.sta_ap_config.wifi_ssid_requires_password: No
wc.sta_ap_config.wifi_ssid_password: <null>
wc.sta_ap_config.wifi_static_ip: No
wc.sta_ap_config.wifi_static_ip_address: <null>
wc.sta_ap_config.wifi_static_ip_subnet: <null>
wc.sta_ap_config.wifi_static_ip_gateway: <null>

And here is the JSON file

 {
    "use_soft_ap": true,
    "soft_ap_ssid": "LocalWiFi",
    "soft_ap_password": "",
    "sta_ap_config": {
      "wifi_ssid": "APNAME",
      "wifi_ssid_requires_password": true,
      "wifi_ssid_password": "AP_PASSWORD",
      "wifi_static_ip": false,
      "wifi_static_ip_address": "",
      "wifi_static_ip_subnet": "",
      "wifi_static_ip_gateway": ""
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions