-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ESP8266 Object.printTo(SPIFFS_File) -> caused empty File #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sorry, I have absolutely no clue. |
Should it work like this? |
I found my mistake......
I read the the json object into a String, but the buffer was one char to smal, so the "}" was lost. That was the reason while no objet was written. My fault, sorry.... |
You can print directly to a String jsonData;
json.printTo(jsonData); |
String FFSjsonFile::readJsonString(){
File jsonFile;
String jsonData = "NIL";
Serial.print("reading " + filePath + "...");
if (SPIFFS.exists(filePath)) {
jsonFile = SPIFFS.open(filePath, "r");
if (jsonFile) {
Serial.println("OK");
size = jsonFile.size();
std::unique_ptr<char[]> buf(new char[size]);
jsonFile.readBytes(buf.get(), size);
if (type == TYPE_ARRAY){
//Array
DynamicJsonBuffer jsonBufferArray;
JsonArray& jsonArray = jsonBufferArray.parseArray(buf.get());
//jsonArray.printTo(jsonData);
if (jsonArray.success()) {
char buffer[size];
jsonArray.printTo(buffer, sizeof(buffer));
jsonData = String(buffer);
}
}else{
//Object
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (json.success()) {
//char buffer[size+1];
//json.printTo(buffer, sizeof(buffer));
//jsonData = String(buffer);
json.printTo(jsonData);
json.printTo(Serial);
Serial.println("");
Serial.println(jsonData);
}
}
}
}
jsonFile.close();
return jsonData;
}
|
ArduinoJson can read directly from a JsonArray& jsonArray = jsonBufferArray.parseArray(jsonFile); Also, you can use JsonVariant json = jsonBuffer.parse(jsonFile);
if (json.is<JsonArray>()) {
JsonArray& arr = json.as<JsonArray>();
// ...
}
if (json.is<JsonObject>()) {
JsonObject& obj = json.as<JsonObject>();
// ...
} |
Great hints!
seems to be additive, my string is initializized with "NIL".
that results a string like
Now it runs very well! String FFSjsonFile::readJsonString(){
File jsonFile;
String jsonData;
Serial.print("reading " + filePath + "...");
if (SPIFFS.exists(filePath)) {
jsonFile = SPIFFS.open(filePath, "r");
if (jsonFile) {
Serial.println("OK");
size = jsonFile.size();
DynamicJsonBuffer jsonBuffer;
JsonVariant json = jsonBuffer.parse(jsonFile);
if (json.is<JsonArray>()) {
JsonArray& arr = json.as<JsonArray>();
json.printTo(jsonData);
}
if (json.is<JsonObject>()) {
JsonObject& obj = json.as<JsonObject>();
json.printTo(jsonData);
}
}else{
Serial.println("ERROR openFile");
return "NIL";
}
jsonFile.close();
}
return jsonData;
} The reason why I want to do this, is to read the file (access to SPIFFS) only once. Do you think, that my approach is the right way to do that or am I thinking to complicated again? Greatings |
Hi Pf@nne, It seems you're trying to implement the oh-so-common global configuration object. In my opinion, it should be implemented like that (not tested): class Config {
DynamicJsonBuffer _jb;
JsonObject& _root;
public:
Config(const char* configFile) : _jb(), _root(parse(_jb, configFile)) {
}
JsonVariant operator[] (const char* key) const {
return _root[key];
}
private:
JsonObject& parse(JsonBuffer&, const char* configFile);
}; This works OK if the configuration is read-only. I understand this class seems a good idea, since it allows to store the application configuration in a JSON file with a convenient interface. However, I don't like this approach for the following reasons:
In my opinion, configuration should be stored in custom I hope this helps. Regards, |
Hi Benoit, your hints are always helpful! to 1.) to 2.) to 3.) Naturaly I will consider your suggestions! Greetings |
Hi,
I want to write back an jsonObject to my ESP8266 FFS in my own Class.
But after writing the file by using
Object.printTo(SPIFFS_File)
the File is Empty.On wich Point I make my mistake?
FFS.h
FFScpp
main.ino
The text was updated successfully, but these errors were encountered: