mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-07 16:23:38 +03:00
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.
This commit is contained in:
parent
211606fe9c
commit
612e7ffd7f
@ -11,6 +11,9 @@
|
|||||||
#include "FS.h"
|
#include "FS.h"
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
|
|
||||||
|
// more and possibly updated information can be found at:
|
||||||
|
// https://arduinojson.org/v6/example/config/
|
||||||
|
|
||||||
bool loadConfig() {
|
bool loadConfig() {
|
||||||
File configFile = LittleFS.open("/config.json", "r");
|
File configFile = LittleFS.open("/config.json", "r");
|
||||||
if (!configFile) {
|
if (!configFile) {
|
||||||
@ -18,22 +21,8 @@ bool loadConfig() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size = configFile.size();
|
|
||||||
if (size > 1024) {
|
|
||||||
Serial.println("Config file size is 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.
|
|
||||||
configFile.readBytes(buf.get(), size);
|
|
||||||
|
|
||||||
StaticJsonDocument<200> doc;
|
StaticJsonDocument<200> doc;
|
||||||
auto error = deserializeJson(doc, buf.get());
|
auto error = deserializeJson(doc, configFile);
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.println("Failed to parse config file");
|
Serial.println("Failed to parse config file");
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user