Scan Json create and save.

This commit is contained in:
Krad
2022-05-11 15:44:43 +08:00
parent 8f6ddc6119
commit fb38907d3c
5 changed files with 89 additions and 8 deletions

20
src/json/ScanJson.cpp Normal file
View File

@@ -0,0 +1,20 @@
//
// Created by Krad on 2022/5/11.
//
#include "ScanJson.h"
#include <QFile>
#include <QDir>
#include <QCoreApplication>
void ScanJson::save() {
if (!root) return;
QFile f(QString("%1/%2.json").arg(QCoreApplication::applicationDirPath(),scanID.c_str()));
f.open(QFileDevice::ReadWrite);
cJSON_AddItemToObject(root, "EmptyScanID", cJSON_CreateString(emptyScanID.c_str()));
cJSON_AddItemToObject(root, "ScanID", cJSON_CreateString(scanID.c_str()));
char* content = cJSON_Print(root);
f.write(content);
f.flush();
f.close();
free(content);
}

44
src/json/ScanJson.h Normal file
View File

@@ -0,0 +1,44 @@
//
// Created by Krad on 2022/5/11.
//
#ifndef GUI_SCANJSON_H
#define GUI_SCANJSON_H
#include <string>
#include "cJSON.h"
class ScanJson {
public:
~ScanJson(){
if(root)cJSON_Delete(root);
}
void store(cJSON* json){
if(root){
cJSON_Delete(root);
root = nullptr;
}
root = json;
}
void setScanID(const char * id){
scanID.clear();
scanID.append(id);
}
void setEmptyScanID(const char * id){
emptyScanID.clear();
emptyScanID.append(id);
setScanID(id);
}
void save();
static ScanJson* Current(){
static ScanJson instance;
return &instance;
}
private:
std::string emptyScanID;
std::string scanID;
cJSON* root = nullptr;
};
#endif //GUI_SCANJSON_H