Fetching current json adding to it and storing it back in the file
-
im doing something wrong here but i can't see what!! so im trying to read the json file and add to it and then store it back into the json file but for some reason it dont add/append to the current json it completely rewrites it with that 1 entry...
bool DeviceData::devicedataupdated() { DeviceDataDebugger.LevelInfo("DEVICEDATA", "Adding New Device", "!"); QFile deviceListFile(_JsonFile); if( !deviceListFile.open( QIODevice::ReadOnly ) ) { DeviceDataDebugger.LevelError("DEVICEDATA", "Could not read the json file", "!"); return false; } QJsonDocument jsonOrg = QJsonDocument::fromJson( deviceListFile.readAll() ); deviceListFile.close(); if (deviceListFile.open(QIODevice::WriteOnly | QIODevice::Text)) { QJsonObject newDevice; newDevice.insert("IPA", "newdevice"); newDevice.insert("Icon", "fa_bolt"); newDevice.insert("Name", "New Device"); newDevice.insert("Status", "online"); newDevice.insert("Type", "Strip"); newDevice.insert("UVLights", "false"); QJsonArray devices = jsonOrg.array(); devices.push_back(newDevice); QJsonDocument jsonDoc( devices ); QJsonObject obj = jsonDoc.object(); deviceListFile.write(jsonDoc.toJson()); deviceListFile.close(); return true; } return false; }This is how it looks befor running the code above (and how it should look with the object "Device")
{ "Device": [ { "IPA": "studiolights", "Icon": "fa_bolt", "Name": "Studio Lights", "Status": "online", "Type": "Strip", "UVLights": "false" } ] }This is after
[ { "IPA": "newdevice", "Icon": "fa_bolt", "Name": "New Device", "Status": "online", "Type": "Strip", "UVLights": "false" } ] -
The complete answer :)
bool DeviceData::devicedataupdated() { QFile deviceListFile(_JsonFile); if( !deviceListFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) { return false; } QJsonDocument jsonOrg = QJsonDocument::fromJson( deviceListFile.readAll() ); deviceListFile.close(); if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) { QJsonObject newDevice; newDevice.insert( "IPA", "newdevice" ); newDevice.insert( "Icon", "fa_bolt" ); newDevice.insert( "Name", "New Device" ); newDevice.insert( "Status", "online" ); newDevice.insert( "Type", "Strip" ); newDevice.insert( "UVLights", "false" ); auto top = jsonOrg.object(); auto devices = top.value( "Device" ).toArray(); devices.push_back( newDevice ); top.insert("Device", devices); QJsonDocument jsonDoc( top ); deviceListFile.write( jsonDoc.toJson() ); deviceListFile.close(); return true; } return false; } -
@Kris-Revi said in Fetching current json adding to it and storing it back in the file:
if( !deviceListFile.open( QIODevice::ReadOnly ) )
{You are opening the file without text mode.
if( !deviceListFile.open( QFile::ReadOnly | QFile::Text ) )QJsonArray devices = jsonOrg.array();
Add a log here (or look it up in debugging session) to see if there is actually any data read from the file.
-
@Kris-Revi said in Fetching current json adding to it and storing it back in the file:
if( !deviceListFile.open( QIODevice::ReadOnly ) )
{You are opening the file without text mode.
if( !deviceListFile.open( QFile::ReadOnly | QFile::Text ) )QJsonArray devices = jsonOrg.array();
Add a log here (or look it up in debugging session) to see if there is actually any data read from the file.
-
@sierdzio wait
QFile::ReadOnly&QFile::Texti thought it wasQIODevice::ReadOnlyandQIODevice::Text@Kris-Revi said in Fetching current json adding to it and storing it back in the file:
@sierdzio wait
QFile::ReadOnly&QFile::Texti thought it wasQIODevice::ReadOnlyandQIODevice::TextDoesn't matter it's the same enum and the values are the same. I prefer using QFile because it's shorter and more consistent.
-
@Kris-Revi said in Fetching current json adding to it and storing it back in the file:
@sierdzio wait
QFile::ReadOnly&QFile::Texti thought it wasQIODevice::ReadOnlyandQIODevice::TextDoesn't matter it's the same enum and the values are the same. I prefer using QFile because it's shorter and more consistent.
@sierdzio said in Fetching current json adding to it and storing it back in the file:
QJsonArray devices = jsonOrg.array();
Add a log here (or look it up in debugging session) to see if there is actually any data read from the file.
hmmm the print out comes up empty :S infact it does not print at all
for(int i = 0; i < devices.size(); i++) qDebug() << "Json Original : " << devices[i]; -
Then at least we know the problem lies in file reading, not in saving.
Check what
readAll()actually returns. Plus add error checking to JSON document (add and check QJsonParseError to yourfromJson()call). -
Then at least we know the problem lies in file reading, not in saving.
Check what
readAll()actually returns. Plus add error checking to JSON document (add and check QJsonParseError to yourfromJson()call). -
OK, case clear. The file is empty :-) Verify the path, and open the file in system to see if it's true.
-
OK, case clear. The file is empty :-) Verify the path, and open the file in system to see if it's true.
-
@Kris-Revi said in Fetching current json adding to it and storing it back in the file:
deviceListFile.readAll();
Except: make sure you call
readAll()ONCE - store it in a variable, then print with qDebug(), then pass to the logic below it.If you call it twice, the second call will likely be empty.
-
@Kris-Revi said in Fetching current json adding to it and storing it back in the file:
deviceListFile.readAll();
Except: make sure you call
readAll()ONCE - store it in a variable, then print with qDebug(), then pass to the logic below it.If you call it twice, the second call will likely be empty.
@sierdzio so by
qDebug() << "File Readl All : " << jsonOrg;i gotFile Readl All : QJsonDocument({"Device":[{"IPA":"studiolights","Icon":"fa_bolt","Name":"Studio Lights","Status":"online","Type":"Strip","UVLights":"false"}]}) Error : "no error occurred" 0 -
Of course it clears the file but that has no consequence - you are building a new JSON will all the data in it, and then save it. So it's good that at that point the file is empty.
-
Of course it clears the file but that has no consequence - you are building a new JSON will all the data in it, and then save it. So it's good that at that point the file is empty.
@sierdzio so now the only part is this
if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) { QJsonObject newDevice; newDevice.insert("IPA", "newdevice"); newDevice.insert("Icon", "fa_bolt"); newDevice.insert("Name", "New Device"); newDevice.insert("Status", "online"); newDevice.insert("Type", "Strip"); newDevice.insert("UVLights", "false"); QJsonArray devices = jsonOrg.array(); devices.push_back(newDevice); QJsonDocument jsonDoc( devices ); QJsonObject obj = jsonDoc.object(); deviceListFile.write(jsonDoc.toJson()); deviceListFile.close(); return true; } -
Hey wait. The jsonOrg is not an array! You have a top-level object there called
"Device". So that's why this code is failing for you.auto top = jsonOrg.object(); auto devices = top.value("Device").toArray(); [...] QJsonDocument jsonDoc( top );With that it should work.
-
Hey wait. The jsonOrg is not an array! You have a top-level object there called
"Device". So that's why this code is failing for you.auto top = jsonOrg.object(); auto devices = top.value("Device").toArray(); [...] QJsonDocument jsonDoc( top );With that it should work.
if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) { QJsonObject newDevice; // projectDetails newDevice.insert("IPA", "newdevice"); newDevice.insert("Icon", "fa_bolt"); newDevice.insert("Name", "New Device"); newDevice.insert("Status", "online"); newDevice.insert("Type", "Strip"); newDevice.insert("UVLights", "false"); auto top = jsonOrg.object(); QJsonArray devices = top.value("Device").toArray(); devices.push_back(newDevice); QJsonDocument jsonDoc( top ); deviceListFile.write(jsonDoc.toJson()); deviceListFile.close(); return true; }does nothing! like the file remains as it was :/ no new entry was added when running the code nor any error!
-
Ah sorry, it's working on a copy of the array, obviously. You need to also add
devicesback totop:top.insert("Device", devices); -
Ah sorry, it's working on a copy of the array, obviously. You need to also add
devicesback totop:top.insert("Device", devices); -
The complete answer :)
bool DeviceData::devicedataupdated() { QFile deviceListFile(_JsonFile); if( !deviceListFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) { return false; } QJsonDocument jsonOrg = QJsonDocument::fromJson( deviceListFile.readAll() ); deviceListFile.close(); if ( deviceListFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) { QJsonObject newDevice; newDevice.insert( "IPA", "newdevice" ); newDevice.insert( "Icon", "fa_bolt" ); newDevice.insert( "Name", "New Device" ); newDevice.insert( "Status", "online" ); newDevice.insert( "Type", "Strip" ); newDevice.insert( "UVLights", "false" ); auto top = jsonOrg.object(); auto devices = top.value( "Device" ).toArray(); devices.push_back( newDevice ); top.insert("Device", devices); QJsonDocument jsonDoc( top ); deviceListFile.write( jsonDoc.toJson() ); deviceListFile.close(); return true; } return false; }