New Json logic

This commit is contained in:
Krad
2021-12-07 14:09:24 +08:00
parent 6627b8bb2a
commit 3db49c8cfb
4 changed files with 680 additions and 346 deletions

View File

@@ -83,7 +83,8 @@ static const char *parse_number(cJSON *item,const char *num)
if (*num >= '1' && *num <= '9') do n = (n * 10.0) + (*num++ - '0'); while (*num >= '0' && *num <= '9'); /* Number? */ if (*num >= '1' && *num <= '9') do n = (n * 10.0) + (*num++ - '0'); while (*num >= '0' && *num <= '9'); /* Number? */
if (*num == '.' && num[1] >= '0' && num[1] <= '9') { num++; do n = (n * 10.0) + (*num++ - '0'), scale--; while (*num >= '0' && *num <= '9'); } /* Fractional part? */ if (*num == '.' && num[1] >= '0' && num[1] <= '9') { num++; do n = (n * 10.0) + (*num++ - '0'), scale--; while (*num >= '0' && *num <= '9'); } /* Fractional part? */
if (*num == 'e' || *num == 'E') /* Exponent? */ if (*num == 'e' || *num == 'E') /* Exponent? */
{ num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */ {
num++; if (*num == '+') num++; else if (*num == '-') signsubscale = -1, num++; /* With sign? */
while (*num >= '0' && *num <= '9') subscale = (subscale * 10) + (*num++ - '0'); /* Number? */ while (*num >= '0' && *num <= '9') subscale = (subscale * 10) + (*num++ - '0'); /* Number? */
} }
@@ -465,17 +466,47 @@ void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (
void cJSON_AddItemReferenceToArray(cJSON* array, cJSON* item) { cJSON_AddItemToArray(array, create_reference(item)); } void cJSON_AddItemReferenceToArray(cJSON* array, cJSON* item) { cJSON_AddItemToArray(array, create_reference(item)); }
void cJSON_AddItemReferenceToObject(cJSON* object, const char* string, cJSON* item) { cJSON_AddItemToObject(object, string, create_reference(item)); } void cJSON_AddItemReferenceToObject(cJSON* object, const char* string, cJSON* item) { cJSON_AddItemToObject(object, string, create_reference(item)); }
cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0; cJSON* cJSON_DetachItemFromArray(cJSON* array, int which) {
if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;} cJSON* c = array->child; while (c && which > 0) c = c->next, which--; if (!c) return 0;
if (c->prev) c->prev->next = c->next; if (c->next) c->next->prev = c->prev; if (c == array->child) array->child = c->next; c->prev = c->next = 0; return c;
}
void cJSON_DeleteItemFromArray(cJSON* array, int which) { cJSON_Delete(cJSON_DetachItemFromArray(array, which)); } void cJSON_DeleteItemFromArray(cJSON* array, int which) { cJSON_Delete(cJSON_DetachItemFromArray(array, which)); }
cJSON* cJSON_DetachItemFromObject(cJSON* object, const char* string) { int i = 0; cJSON* c = object->child; while (c && cJSON_strcasecmp(c->string, string)) i++, c = c->next; if (c) return cJSON_DetachItemFromArray(object, i); return 0; } cJSON* cJSON_DetachItemFromObject(cJSON* object, const char* string) { int i = 0; cJSON* c = object->child; while (c && cJSON_strcasecmp(c->string, string)) i++, c = c->next; if (c) return cJSON_DetachItemFromArray(object, i); return 0; }
void cJSON_DeleteItemFromObject(cJSON* object, const char* string) { cJSON_Delete(cJSON_DetachItemFromObject(object, string)); } void cJSON_DeleteItemFromObject(cJSON* object, const char* string) { cJSON_Delete(cJSON_DetachItemFromObject(object, string)); }
/* Replace array/object items with new ones. */ /* Replace array/object items with new ones. */
void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return; void cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem) {
cJSON* c = array->child; while (c && which > 0) c = c->next, which--; if (!c) return;
newitem->next = c->next; newitem->prev = c->prev; if (newitem->next) newitem->next->prev = newitem; newitem->next = c->next; newitem->prev = c->prev; if (newitem->next) newitem->next->prev = newitem;
if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);} if (c == array->child) array->child = newitem; else newitem->prev->next = newitem; c->next = c->prev = 0; cJSON_Delete(c);
void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}} }
cJSON* cJSON_FindItemInArray(cJSON* array, int which)
{
cJSON* c = array->child;
while (c && which > 0) c = c->next, which--;
return c;
}
void cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newitem)
{
int i = 0;
cJSON* c = object->child;
while (c && cJSON_strcasecmp(c->string, string))i++, c = c->next;
if (c)
{
newitem->string = cJSON_strdup(string);
cJSON_ReplaceItemInArray(object, i, newitem);
}
}
cJSON* cJSON_FindItemInObject(cJSON* object, const char* string)
{
int i = 0;
cJSON* c = object->child;
while (c && cJSON_strcasecmp(c->string, string))i++, c = c->next;
return c;
}
/* Create basic types: */ /* Create basic types: */
cJSON* cJSON_CreateNull() { cJSON* item = cJSON_New_Item(); if (item)item->type = cJSON_NULL; return item; } cJSON* cJSON_CreateNull() { cJSON* item = cJSON_New_Item(); if (item)item->type = cJSON_NULL; return item; }

View File

@@ -114,6 +114,10 @@ extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
extern void cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem); extern void cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem);
extern void cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newitem); extern void cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newitem);
/*created by huxy*/
extern cJSON* cJSON_FindItemInObject(cJSON* object, const char* string);
extern cJSON* cJSON_FindItemInArray(cJSON* array, int which);
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())

230
src/json/jsonobject.cpp Normal file
View File

@@ -0,0 +1,230 @@
#include "jsonobject.h"
#include <fstream>
#include <sstream>
#include <QDateTime>
#include <vector>
#include <QTranslator>
#include <QDebug>
#include <QApplication>
const char* strProductFileName = "./cfgs/usct-product.json";
JsonObject::JsonObject()
{
loadcfg();
}
JsonObject::~JsonObject()
{
savecfg();
}
void JsonObject::setJsonString(const char* catergory, const char* stringName, const char* stringValue)
{
if (!loadcfg())
return;
cJSON* first = cJSON_FindItemInObject(json_root, catergory);
if (!first) return;
cJSON* Item = cJSON_CreateString(stringValue);
cJSON_ReplaceItemInObject(first, stringName, Item);
savecfg();
}
char* JsonObject::getJsonString(const char* catergory, const char* stringName)
{
if (!loadcfg())
return "";
cJSON* first = cJSON_FindItemInObject(json_root, catergory);
if (!first) return "";
cJSON* second = cJSON_FindItemInObject(first, stringName);
return second->valuestring;
}
QStringList JsonObject::protocals()
{
if (!loadcfg())
return QStringList();
cJSON* first = cJSON_FindItemInObject(json_root, "protocol");
if (!first) return QStringList();
cJSON* second = cJSON_FindItemInObject(first, "lists");
std::string lans = second->valuestring;
QString str = QString::fromLocal8Bit(QByteArray::fromRawData(lans.c_str(), lans.size()));
return str.split(";");
}
QString JsonObject::defaultProtocal()
{
char* str = getJsonString("protocol", "default");
return QString(str);
}
void JsonObject::setDefaultProtocal(QString str)
{
setJsonString("protocol", "default", str.toStdString().c_str());
}
QString JsonObject::defaultFilter()
{
char* str = getJsonString("worklistfilter", "default");
return QString(str);
}
void JsonObject::setDefaultFilter(QString str)
{
setJsonString("worklistfilter", "default", str.toStdString().c_str());
}
QStringList JsonObject::worklistFilters()
{
if (!loadcfg())
return QStringList();
cJSON* first = cJSON_FindItemInObject(json_root, "worklistfilter");
if (!first) return QStringList();
cJSON* second = cJSON_FindItemInObject(first, "lists");
std::string lans = second->valuestring;
QString str = QString::fromLocal8Bit(QByteArray::fromRawData(lans.c_str(), lans.size()));
return str.split(";");
}
QStringList JsonObject::language()
{
if (!loadcfg())
return QStringList();
cJSON* first = cJSON_FindItemInObject(json_root, "general");
if (!first) return QStringList();
cJSON* second = cJSON_FindItemInObject(first, "language");
std::string lans = second->valuestring;
QString str = QString::fromLocal8Bit(QByteArray::fromRawData(lans.c_str(), lans.size()));
return str.split(";");
}
void JsonObject::setDefaultLanguage(QString str)
{
setJsonString("general", "defaultlanguage", str.toStdString().c_str());
}
QString JsonObject::defaultLanguage()
{
char* str = getJsonString("general", "defaultlanguage");
return QString(str);
}
QString JsonObject::institutionName()
{
char* str = getJsonString("general", "institutionName");
return QString(str);
}
void JsonObject::setInstitutionName(QString str)
{
setJsonString("general", "institutionName", str.toStdString().c_str());
}
QString JsonObject::institutionAddr()
{
char* str = getJsonString("general", "institutionAddr");
return QString(str);
}
void JsonObject::setInstitutionAddr(QString str)
{
setJsonString("general", "institutionAddr", str.toStdString().c_str());
}
int JsonObject::lockerCount()
{
char* str = getJsonString("general", "lockscreen");
int interval = QString(str).toInt() * 1000;
return interval;
}
QString JsonObject::lockScreenTimeout()
{
char* str = getJsonString("general", "lockscreen");
return QString(str);
}
void JsonObject::setLockScreenTimeout(QString str)
{
setJsonString("general", "lockscreen", str.toStdString().c_str());
}
bool JsonObject::loadcfg()
{
if (m_bLoaded)
return true;
std::ifstream inFile(strProductFileName);
if (!inFile.is_open())
{
return -1;
}
std::stringstream ss;
ss << inFile.rdbuf();
std::string strJsonData = ss.str();
json_root = cJSON_Parse(strJsonData.c_str());
//modify somthing
//QJsonValueRef ref = m_JsonRootObject.find("general").value();
//QJsonObject obj = ref.toObject();
//obj["language"] = language;
//cJSON* parent = cJSON_FindItemInObject(json_root, "waitinglist");
//if (parent)
//{
// //const char* newtime = QDateTime::currentDateTime().toString("yyyyMMdd").toStdString().c_str();
// cJSON* Item = cJSON_CreateString(QDateTime::currentDateTime().toString("yyyy-MM-dd").toStdString().c_str());
// cJSON_ReplaceItemInObject(parent, "waitinglistDefaultDate", Item);
//}
//cJSON* parent = cJSON_FindItemInObject(json_root, "pacsservers");
//if (parent)
//{
// cJSON* p2 = cJSON_FindItemInArray(parent, 1);
// cJSON* Item = cJSON_CreateString("KISS");
// //const char* newtime = QDateTime::currentDateTime().toString("yyyyMMdd").toStdString().c_str();
// cJSON_ReplaceItemInObject(p2, "ae", Item);
//}
inFile.close();
m_bLoaded = true;
return true;
}
bool JsonObject::savecfg()
{
if (!m_bLoaded)
return false;
std::ofstream outFile(strProductFileName);
if (!outFile.is_open())
{
return -1;
}
char* strJsonData = cJSON_Print(json_root);
std::stringstream ss;
ss << strJsonData;
outFile << ss.rdbuf();
outFile.close();
m_bLoaded = false;
return true;
}

69
src/json/jsonobject.h Normal file
View File

@@ -0,0 +1,69 @@
#ifndef JSONOBJECT_H
#define JSONOBJECT_H
//#include <QObject>
#include "json/cJSON.h"
class QString;
class QStringList;
class QTranslator;
//#define setJsonString(catergory,stringName,stringValue)\
// if (!loadcfg())\
// return;\
// cJSON* first = cJSON_FindItemInObject(json_root, #catergory);\
// if (first){\
// cJSON* Item = cJSON_CreateString(#stringValue);\
// cJSON_ReplaceItemInObject(first, #stringName, Item);\
// }\
// savecfg();
class JsonObject
{
public:
static JsonObject* Instance()
{
static JsonObject obj;
return &obj;
}
QStringList language();
void setDefaultLanguage(QString str);
QString defaultLanguage();
QString institutionName();
void setInstitutionName(QString str);
QString institutionAddr();
void setInstitutionAddr(QString str);
int lockerCount();
QString lockScreenTimeout();
void setLockScreenTimeout(QString str);
QStringList protocals();
QString defaultProtocal();
void setDefaultProtocal(QString str);
QStringList worklistFilters();
QString defaultFilter();
void setDefaultFilter(QString str);
private:
void setJsonString(const char* catergory, const char* stringName, const char* stringValue);
char* getJsonString(const char* catergory, const char* stringName);
bool loadcfg();
bool savecfg();
JsonObject();
~JsonObject();
cJSON* json_root = nullptr;
bool m_bLoaded = false;
int counter;
};
#endif // JSONOBJECT_H