// ---------------------------------------------------------------------------------------------------------------------
#include "QVector"
#include "QTextCodec"
#include "QDebug"
#include "QJson.h"
namespace JSK
{
JSONObject::JSONObject(const QString& key)
{
init();
setKey(key);
}
void JSONObject::init()
{
m_key = "";
m_type = vtfNull;
m_string = "";
m_int = 0;
m_double = 0.0;
m_bool = false;
m_datetime = QDateTime();
}
JSONObject::~JSONObject()
{
for( int i=0;i<m_list.count();i++ )
{
JSONObject* item = m_list.at(i);
delete item;
}
}
QString JSONObject::dequote(QString text)
{
int idx;
while((idx=text.indexOf("\\u"))>=0)
{
int nHex = text.mid(idx+2, 4).toInt(0, 16);
text.replace(idx, 6, QChar(nHex));
}
return text;
}
QString JSONObject::quote(const QString& Text)
{
QString ret;
QStdWString ws = Text.toStdWString();
const wchar_t* ptr = ws.c_str();
while(*ptr)
{
switch(*ptr)
{
case L'\\': ret += "\\\\"; break;
case L'\"': ret += "\\\""; break;
case L'\b': ret += "\\b"; break;
case L'\t': ret += "\\t"; break;
case L'\n': ret += "\\n"; break;
case L'\f': ret += "\\f"; break;
case L'\r': ret += "\\r"; break;
default:
if ( *ptr > 255 )
{
uint16_t v = *ptr;
QString S = QString::number(v, 16);
ret += "\\u" + S;
}else
{
ret += *ptr;
}
break;
}
ptr++;
}
return ret;
}
QString JSONObject::getKey() const
{
return m_key;
}
void JSONObject::setKey(QString value)
{
m_key = value.trimmed();
}
QString JSONObject::getJsonKey() const
{
return "\"" + quote(m_key) + "\"";
}
QString JSONObject::getJsonValue() const
{
QString ret;
switch(m_type)
{
case 0: // null
ret = "null";
break;
case 1: // string
ret = "\"" + quote(m_string) + "\"";
break;
case 2: // int
ret = QString::number(m_int);
break;
case 3: // double
ret = QString::number(m_double);
break;
case 4: // bool
ret = m_bool ? "true" : "false";
break;
case 5: // datetime
ret = "\"" + m_datetime.toString("yyyy/MM/dd HH:mm:ss.zzz") + "\"";
break;
default:
ret = "json.value.type error.";
break;
}
return ret;
}
JSONObject* JSONObject::addChild(const QString& key, int type)
{
JSONObject* ret = new JSONObject(key);
ret->setType(type);
m_list.append(ret);
return ret;
}
JSONObject* JSONObject::add(const QString& key, const char* value)
{
JSONObject* ret = new JSONObject(key);
ret->setValue(value);
m_list.append(ret);
return ret;
}
JSONObject* JSONObject::add(const QString& key)
{
JSONObject* ret = new JSONObject(key);
m_list.append(ret);
return ret;
}
JSONObject* JSONObject::add(const QString& key, const QString& value)
{
JSONObject* ret = new JSONObject(key);
ret->setValue(value);
m_list.append(ret);
return ret;
}
JSONObject* JSONObject::add(const QString& key, int value)
{
JSONObject* ret = new JSONObject(key);
ret->setValue(value);
m_list.append(ret);
return ret;
}
JSONObject* JSONObject::add(const QString& key, double value)
{
JSONObject* ret = new JSONObject(key);
ret->setValue(value);
m_list.append(ret);
return ret;
}
JSONObject* JSONObject::add(const QString& key, bool value)
{
JSONObject* ret = new JSONObject(key);
ret->setValue(value);
m_list.append(ret);
return ret;
}
JSONObject* JSONObject::add(const QString& key, QDateTime value)
{
JSONObject* ret = new JSONObject(key);
ret->setValue(value);
m_list.append(ret);
return ret;
}
int JSONObject::deleteChildren(const QString& key)
{
int ret = 0;
for( int i=m_list.count();i>0;i-- )
{
JSONObject* item = m_list.at(i-1);
if ( item->m_key.compare(key)==0 )
{
m_list.removeAt(i-1);
delete item;
ret ++;
}
}
return ret;
}
void JSONObject::clearChildren()
{
for( int i=0;i<m_list.count();i++ )
{
JSONObject* item = m_list.at(i);
delete item;
}
m_list.clear();
}
void JSONObject::clear()
{
init();
clearChildren();
}
QStringList JSONObject::keys() const
{
QStringList ret;
for( int i=0;i<m_list.count();i++ )
{
JSONObject* item = m_list.at(i);
ret.append(item->getKey());
}
return ret;
}
JSONObject* JSONObject::get(const QString& key) const
{
JSONObject* ret = NULL;
for( int i=0;i<m_list.count();i++ )
{
JSONObject* item = m_list.at(i);
if ( item->m_key.compare(key)==0 )
{
ret = item;
break;
}
}
return ret;
}
QString JSONObject::getString(const QString& key) const
{
JSONObject* res = get(key);
return res != NULL ? res->getString() : QString();
}
int JSONObject::getInt(const QString& key) const
{
JSONObject* res = get(key);
return res != NULL ? res->getInt() : 0;
}
double JSONObject::getDouble(const QString& key) const
{
JSONObject* res = get(key);
return res != NULL ? res->getDouble() : 0.0;
}
bool JSONObject::getBoolean(const QString& key) const
{
JSONObject* res = get(key);
return res != NULL ? res->getBoolean() : false;
}
QDateTime JSONObject::getDateTime(const QString& key) const
{
JSONObject* res = get(key);
return res != NULL ? res->getDateTime() : QDateTime();
}
QString JSONObject::toString() const
{
QString values;
if ( m_list.count() >0 )
{
for( int i=0;i<m_list.count();i++ )
{
JSONObject* item = m_list.at(i);
if ( m_type == vtfArray )
{
values += "," + item->getJsonValue();
}else
{
values += "," + item->toString(); //item->getJsonKey() + ":" + item->getJsonValue();
}
}
if ( ! values.isEmpty() )
{
values.remove(0, 1);
}
if ( m_type == vtfArray )
{
values = "[" + values + "]";
}else
{
values = "{" + values + "}";
}
}else
{
if ( m_type == vtfArray )
{
values = "[]";
}else
{
values = getJsonValue();
}
}
return m_key.isEmpty() ? values : getJsonKey() + ":" + values;
}
QString JSONObject::getString() const
{
QString ret;
switch(m_type)
{
default:
case 0: // null
ret = "";
break;
case 1: // string
ret = m_string;
break;
case 2: // int
ret = QString::number(m_int);
break;
case 3: // double
ret = QString::number(m_double);
break;
case 4: // bool
ret = m_bool ? "true" : "false";
break;
case 5: // datetime
ret = "\"" + m_datetime.toString("yyyy/MM/dd HH:mm:ss.zzz") + "\"";
break;
}
return ret;
}
int JSONObject::getInt() const
{
int ret;
switch(m_type)
{
default:
case 0: // null
ret = 0;
break;
case 1: // string
ret = m_string.toInt();
break;
case 2: // int
ret = m_int;
break;
case 3: // double
ret = m_double;
break;
case 4: // bool
ret = m_bool ? 1 : 0;
break;
case 5: // datetime
Jonix
- 粉丝: 78
- 资源: 13
最新资源
- Matlab版本2023b的Embedded Coder Support Package for ARM Cortex-M Processors支持包免费分享,1.8G压缩包分成3个(2/3)
- ghostscript-10.0.0
- 医疗保障信息平台定点医药机构接口规范
- Python编程基础入门到高级开发技巧指南
- 手机充电头外观尺寸检测机工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- JSP EIMS系统-OA子系统的设计与开发(源代码+LW).zip
- (JSP)JTBC_CMS_2.0.0.8.zip
- linux java jdk8
- Windows系统上Tomcat的安装与配置详解
- Linux-Shell基础命令语言
- 服装图像数据集,衣服图像数据,包含服装属性
- Matlab版本2023b的Embedded Coder Support Package for ARM Cortex-M Processors支持包免费分享,1.8G压缩包分成3个(3/3)
- glove11111wwee.pdf
- ECharts象形柱图-圣诞愿望清单和山峰高度-4.zip
- ECharts象形柱图-人体含水量-2.zip
- ECharts象形柱图-驯鹿的速度-6.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈