/****************************************************************************
**
** Copyright (C) Qxt Foundation. Some rights reserved.
**
** This file is part of the QxtGui module of the Qxt library.
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the Common Public License, version 1.0, as published
** by IBM, and/or under the terms of the GNU Lesser General Public License,
** version 2.1, as published by the Free Software Foundation.
**
** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
** FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the CPL and the LGPL along with this
** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
** included with the source distribution for more information.
** If you did not receive a copy of the licenses, contact the Qxt Foundation.
**
** <http://libqxt.org> <foundation@libqxt.org>
**
****************************************************************************/
/*!
\class QxtJSON
\inmodule QxtCore
\brief The QxtJSON class implements serializing/deserializing from/to JSON
implements JSON (JavaScript Object Notation) is a lightweight data-interchange format.
see http://www.json.org/
\section2 Type Conversion
\table 80%
\header \o JSON Type \o Qt Type
\row \o object \o QVariantMap/QVariantHash
\row \o array \o QVariantList/QStringList
\row \o string \o QString
\row \o number \o int,double
\row \o true \o bool
\row \o false \o bool
\row \o null \o QVariant()
\endtable
*/
#include "qxtjson.h"
#include <QVariant>
#include <QDebug>
#include <QTextStream>
#include <QStringList>
QString QxtJSON::stringify(QVariant v){
if (v.isNull()){
return "null";
}// QXT dev's: WTF????? mb just return empty string? with best regards, flareguner.
int t = v.type();
if (t == QVariant::String){
return "\""+v.toString()+"\"";
}
else if (t == QVariant::Bool){
return v.toBool()?"true":"false";
}else if (t == QVariant::Int){
return QString::number(v.toInt());
}else if (t == QVariant::Double){
return QString::number(v.toDouble());
}else if (t == QVariant::Map){
QString r="{";
QMap<QString, QVariant> map = v.toMap();
QMapIterator<QString, QVariant> i(map);
while (i.hasNext()){
i.next();
r+="\""+i.key()+"\":"+stringify(i.value())+",";
}
if(r.length()>1)
r.chop(1);
r+="}";
return r;
}else if (t == QVariant::Hash){
QString r="{";
QHash<QString, QVariant> map = v.toHash();
QHashIterator<QString, QVariant> i(map);
while (i.hasNext()){
i.next();
r+="\""+i.key()+"\":"+stringify(i.value())+",";
}
if(r.length()>1)
r.chop(1);
r+="}";
return r;
}else if (t == QVariant::StringList){
QString r="[";
QStringList l = v.toStringList();
foreach(QString i, l){
r+="\""+i+"\",";
}
if(r.length()>1)
r.chop(1);
r+="]";
return r;
}else if (t == QVariant::List){
QString r="[";
QVariantList l = v.toList();
foreach(QVariant i, l){
r+=stringify(i)+",";
}
if(r.length()>1)
r.chop(1);
r+="]";
return r;
}
return QString();
}
static QVariant parseValue(QTextStream &s,bool & error);
static QVariantMap parseObject (QTextStream & s,bool & error);
static QVariantList parseArray (QTextStream & s,bool & error);
static QString parseString (QTextStream & s,bool & error);
static QVariant parseLiteral (QTextStream & s,bool & error);
QVariant QxtJSON::parse(QString string){
QTextStream s(&string);
bool error=false;
QVariant v=parseValue(s,error);
if(error)
return QVariant();
return v;
}
static QVariant parseValue(QTextStream &s,bool & error){
s.skipWhiteSpace();
QChar c;
while(!s.atEnd() && !error){
s>>c;
if (c=='{'){
return parseObject(s,error);
} else if (c=='"'){
return parseString(s,error);
} else if (c=='['){
return parseArray(s,error);
} else {
return parseLiteral(s,error);
}
s.skipWhiteSpace();
}
return QVariant();
}
static QVariantMap parseObject (QTextStream & s,bool & error){
s.skipWhiteSpace();
QVariantMap o;
QString key;
bool atVal=false;
QChar c;
while(!s.atEnd() && !error){
s>>c;
if (c=='}'){
return o;
} else if (c==',' || c==':'){
/*
They're syntactic sugar, since key:value come in bundles anyway
Could check for error handling. too lazy.
*/
} else if (c=='"'){
if(atVal){
o[key]=parseString(s,error);
atVal=false;
}else{
key=parseString(s,error);
atVal=true;
}
} else if (c=='['){
if(atVal){
o[key]=parseArray(s,error);
atVal=false;
}else{
error=true;
return QVariantMap();
}
} else if (c=='{'){
if(atVal){
o[key]=parseObject(s,error);
atVal=false;
}else{
error=true;
return QVariantMap();
}
} else {
if(atVal){
o[key]=parseLiteral(s,error);
atVal=false;
}else{
error=true;
return QVariantMap();
}
}
s.skipWhiteSpace();
}
error=true;
return QVariantMap();
}
static QVariantList parseArray (QTextStream & s,bool & error){
s.skipWhiteSpace();
QVariantList l;
QChar c;
while(!s.atEnd() && !error){
s>>c;
if (c==']'){
return l;
} else if (c==','){
} else if (c=='"'){
l.append(parseString(s,error));
} else if (c=='['){
l.append(parseArray(s,error));
} else if (c=='{'){
l.append(parseObject(s,error));
} else {
l.append(parseLiteral(s,error));
}
s.skipWhiteSpace();
}
error=true;
return QVariantList();
}
static QString parseString (QTextStream & s,bool & error){
QString str;
QChar c;
while(!s.atEnd() && !error){
s>>c;
if(c=='"'){
return str;
}else if(c=='\\'){
s>>c;
if(c=='b'){
str.append('\b');
}else if(c=='f'){
str.append('\f');
}else if(c=='n'){
str.append('\n');
}else if(c=='r'){
str.append('\r');
}else if(c=='t'){
str.append('\t');
}else if(c=='f'){
str.append('\f');
}else if(c=='u'){
//Big thanks arsi for this fix!
// short u;
// s>>u;
// str.append(QChar(u));
str.append(QChar(s.read(4).toUShort(0, 16)));
}else{
str.append(c);
}
}else{
str.append(c);
}
}
error=true;
return QString();
}
static QVariant parseLiteral (QTextStream & s,bool & error){
s.seek(s.pos()-1);
QChar c;
while(!s.atEnd() && !error){
s>>c;
if (c=='t'){
s>>c;//r
s>>c;//u
s>>c;//e
return true;
} else if (c=='f'){
s>>c;//a
s>>c;//l
s>>c;/
没有合适的资源?快使用搜索试试~ 我知道了~
QxtJson及网络翻译例子
共6个文件
cpp:3个
h:3个
5星 · 超过95%的资源 需积分: 17 81 下载量 110 浏览量
2015-05-22
15:20:28
上传
评论
收藏 11KB ZIP 举报
温馨提示
QxtJson库,QT下最简洁,高效解析json方案 http://blog.csdn.net/work4blue/article/details/45917537
资源推荐
资源详情
资源评论
收起资源包目录
QxtJson及网络翻译例子演示.zip (6个子文件)
3rdparty
qxtjson.h 1KB
translate.h 1KB
qxtglobal.h 6KB
qxtglobal.cpp 8KB
translate.cpp 7KB
qxtjson.cpp 8KB
共 6 条
- 1
蓝点工坊
- 粉丝: 35
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页