Description
Hi. To my knowledge my config.json works perfectly in my program. It is able to store and update/overwrite existing key/value pairs. However, I have noticed that upon overwriting or resaving the config.json file, it preappends a whole heap of garbled text and I'm not sure why. Here is initially what the config.json file is saved as in the program:
After resaving or overwriting:
Below is the snippet of my code that saves the config file:
// Read and Save Updated Config Parameters
strcpy(Server_Name, custom_Server_Name.getValue());
strcpy(LED_Strip_Num, custom_LED_Strip_Num.getValue());
if (shouldSaveConfig) {
Serial.println("Saving Config");
doc["Server_Name"] = Server_Name;
doc["LED_Strip_Num"] = LED_Strip_Num;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config.json to write to...");
}
serializeJson(doc, Serial);
serializeJson(doc, configFile);
configFile.close();
}
Hopefully someone here knows why this might be happening; incase it help i'm running an esp32 sparkfun thingy, 4MB flash, 921600 upload, 115200 baud, 80MHz. Here is how I initialise the document, where size = 1024:
if (!SPIFFS.exists("/config.json")) {
Serial.println("No Saved Configuration to Read");
} else {
Serial.println("Reading Saved Config!");
File configFile = SPIFFS.open("/config.json", "r");
// If opened and has contents, allocate an appropriately sized buffer and parse
if (configFile) {
Serial.println("Config Opened Successfully");
size_t size = configFile.size();
std::unique_ptr<char[]> buff(new char[size]);
configFile.readBytes(buff.get(), size);
DeserializationError JsonError = deserializeJson(doc, buff.get());
serializeJson(doc, Serial);
// If parse is successful, load parsed variables
if (JsonError) {
Serial.println("Failed to load config.json");
} else {
Serial.println("\nSuccessfully Loaded config.json!");
strcpy(Server_Name, doc["Server_Name"]);
strcpy(LED_Strip_Num, doc["LED_Strip_Num"]);
Serial.println("Loaded Info:");
Serial.print("Server_Name: ");
Serial.println(Server_Name);
Serial.print("LED_Strip_Num: ");
Serial.println(LED_Strip_Num);
}
}
}