Skip to content
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

DeserializationError of ESP32 SPIFFS json file returns error 'incompleteInput' #1159

Closed
400GreyBike opened this issue Jan 2, 2020 · 2 comments
Labels
question v6 ArduinoJson 6

Comments

@400GreyBike
Copy link

400GreyBike commented Jan 2, 2020

I'm struggling to get a Json file in ESP32 SPIFFS to deserialize unless I create a buffer and read that. In the SD example it shows

File file = SD.open(filename);

and then calls

StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, file);`

If I open my file using

File fileConfig = SPIFFS.open("/PM8Config.json", FILE_READ);

and then deserialize using

StaticJsonDocument<1000> doc;
DeserializationError error = deserializeJson(doc, fileConfig);

I get a failure indicating "IncompleteInput"

If however I use the following, I am able to deserialise successfully where String data is in the json, but I get no data where int data items are i.e. pollRate

File fileConfig = SPIFFS.open("/PM8Config.json", FILE_READ); // Check if we can open the Config file
if (!fileConfig) {
Serial.println("There was an error opening the Config file");
Serial2.print(F("pgInitConfig.t10.txt="Failed"));
//return false;
}else{
Serial.println("Config File was successfully opened");
Serial2.print(F("pgInitConfig.t10.txt="Opened"));
//return true;
size_t fileSize = fileConfig.size();
std::unique_ptr<char[]> buf(new char[fileSize]);
fileConfig.readBytes(buf.get(), fileSize);
StaticJsonDocument<1000> doc;
DeserializationError error = deserializeJson(doc, buf.get());
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
//return;
}
int testJsonRead = doc["sensorData"]["pollRate"];
Serial.print("Test JSON Read variable (should read '30': " );
Serial.println(testJsonRead);
const char* testPW = doc["password"];
Serial.print("Test JSON Read variable (should show PW: " );
Serial.println(testPW); `

The json file in SPIFFS is (and I can Serial.print this without an issue):

{
"platform": "NodeMCU32s",
"framework": "Arduino",
"version": "0.1a",
"vdated": "30/12/19",
"ssid": "myssid",
"password": "mypassword",
"thingSpeakInfo":{
"thingspeakAddress": "api.thingspeak.com",
"thingspeakChannelID": 000000,
"thingspeakReadApiKey": "myAPIKey",
"thingspeakWriteApiKey": "myAPIKey"
},
"checkWxApiKey": "myAPIKey",
"sensorData":{
"pollRate": 30,
"stuff": "stuff"
}
}

I'm clearly missing something here but can't figure out why I can't simply call the fileConfig object without having to use buf.get()?

Any help would be appreciated and I think it's time to buy the book :-)

@400GreyBike
Copy link
Author

Having found another thread, it needs the removal of the readBytes() statement to ensure the pointer isn't at the end of the file.

Apologies for adding a somewhat wasted "issue" to an excellent library.

@bblanchon
Copy link
Owner

No worries @400GreyBike.
Thank you for using ArduinoJson.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 2, 2020
@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants