Skip to content

Commit 612e7ff

Browse files
authored
Remove temporary buffer in ConfigFile.ino (#8298)
When reading from a `Stream`, like `File`, using a temporary buffer is counterproductive because it complexifies the code and increases memory usage. It's also a source of confusion because it can create dangling pointers in the `JsonDocument`. The only benefit of using a buffer is the reading speed, but I don't think speed is the focus in this example; if it were, it would use a buffer in `saveConfig()` too.
1 parent 211606f commit 612e7ff

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

libraries/esp8266/examples/ConfigFile/ConfigFile.ino

+4-15
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,18 @@
1111
#include "FS.h"
1212
#include <LittleFS.h>
1313

14+
// more and possibly updated information can be found at:
15+
// https://arduinojson.org/v6/example/config/
16+
1417
bool loadConfig() {
1518
File configFile = LittleFS.open("/config.json", "r");
1619
if (!configFile) {
1720
Serial.println("Failed to open config file");
1821
return false;
1922
}
2023

21-
size_t size = configFile.size();
22-
if (size > 1024) {
23-
Serial.println("Config file size is too large");
24-
return false;
25-
}
26-
27-
// Allocate a buffer to store contents of the file.
28-
std::unique_ptr<char[]> buf(new char[size]);
29-
30-
// We don't use String here because ArduinoJson library requires the input
31-
// buffer to be mutable. If you don't use ArduinoJson, you may as well
32-
// use configFile.readString instead.
33-
configFile.readBytes(buf.get(), size);
34-
3524
StaticJsonDocument<200> doc;
36-
auto error = deserializeJson(doc, buf.get());
25+
auto error = deserializeJson(doc, configFile);
3726
if (error) {
3827
Serial.println("Failed to parse config file");
3928
return false;

0 commit comments

Comments
 (0)