/* This file is part of qjson
*
* Copyright (C) 2009 Till Adam <adam@kde.org>
* Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "serializer.h"
#include <QtCore/QDataStream>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
// cmath does #undef for isnan and isinf macroses what can be defined in math.h
#if defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY) || defined(Q_OS_SOLARIS)
# include <math.h>
#else
# include <cmath>
#endif
#ifdef Q_OS_SOLARIS
# ifndef isinf
# include <ieeefp.h>
# define isinf(x) (!finite((x)) && (x)==(x))
# endif
#endif
#ifdef _MSC_VER // using MSVC compiler
#include <float.h>
#endif
using namespace QJson;
class Serializer::SerializerPrivate {
public:
SerializerPrivate() :
specialNumbersAllowed(false),
indentMode(QJson::IndentNone),
doublePrecision(6) {
errorMessage.clear();
}
QString errorMessage;
bool specialNumbersAllowed;
IndentMode indentMode;
int doublePrecision;
QByteArray serialize( const QVariant &v, bool *ok, int indentLevel = 0);
static QByteArray buildIndent(int spaces);
static QByteArray escapeString( const QString& str );
static QByteArray join( const QList<QByteArray>& list, const QByteArray& sep );
};
QByteArray Serializer::SerializerPrivate::join( const QList<QByteArray>& list, const QByteArray& sep ) {
QByteArray res;
Q_FOREACH( const QByteArray& i, list ) {
if ( !res.isEmpty() )
res += sep;
res += i;
}
return res;
}
QByteArray Serializer::SerializerPrivate::serialize( const QVariant &v, bool *ok, int indentLevel)
{
QByteArray str;
if ( ! v.isValid() ) { // invalid or null?
str = "null";
} else if (( v.type() == QVariant::List ) || ( v.type() == QVariant::StringList )){ // an array or a stringlist?
const QVariantList list = v.toList();
QList<QByteArray> values;
Q_FOREACH( const QVariant& var, list )
{
QByteArray serializedValue;
serializedValue = serialize( var, ok, indentLevel+1);
if ( !*ok ) {
break;
}
switch(indentMode) {
case QJson::IndentFull :
case QJson::IndentMedium :
case QJson::IndentMinimum :
values << serializedValue;
break;
case QJson::IndentCompact :
case QJson::IndentNone :
default:
values << serializedValue.trimmed();
break;
}
}
if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull ) {
QByteArray indent = buildIndent(indentLevel);
str = indent + "[\n" + join( values, ",\n" ) + "\n" + indent + "]";
}
else if (indentMode == QJson::IndentMinimum) {
QByteArray indent = buildIndent(indentLevel);
str = indent + "[\n" + join( values, ",\n" ) + "\n" + indent + "]";
}
else if (indentMode == QJson::IndentCompact) {
str = "[" + join( values, "," ) + "]";
}
else {
str = "[ " + join( values, ", " ) + " ]";
}
} else if ( v.type() == QVariant::Map ) { // variant is a map?
const QVariantMap vmap = v.toMap();
QMapIterator<QString, QVariant> it( vmap );
if (indentMode == QJson::IndentMinimum) {
QByteArray indent = buildIndent(indentLevel);
str = indent + "{ ";
}
else if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) {
QByteArray indent = buildIndent(indentLevel);
QByteArray nextindent = buildIndent(indentLevel + 1);
str = indent + "{\n" + nextindent;
}
else if (indentMode == QJson::IndentCompact) {
str = "{";
}
else {
str = "{ ";
}
QList<QByteArray> pairs;
while ( it.hasNext() ) {
it.next();
indentLevel++;
QByteArray serializedValue = serialize( it.value(), ok, indentLevel);
indentLevel--;
if ( !*ok ) {
break;
}
QByteArray key = escapeString( it.key() );
QByteArray value = serializedValue.trimmed();
if (indentMode == QJson::IndentCompact) {
pairs << key + ":" + value;
} else {
pairs << key + " : " + value;
}
}
if (indentMode == QJson::IndentFull) {
QByteArray indent = buildIndent(indentLevel + 1);
str += join( pairs, ",\n" + indent);
}
else if (indentMode == QJson::IndentCompact) {
str += join( pairs, "," );
}
else {
str += join( pairs, ", " );
}
if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) {
QByteArray indent = buildIndent(indentLevel);
str += "\n" + indent + "}";
}
else if (indentMode == QJson::IndentCompact) {
str += "}";
}
else {
str += " }";
}
} else if ( v.type() == QVariant::Hash ) { // variant is a hash?
const QVariantHash vhash = v.toHash();
QHashIterator<QString, QVariant> it( vhash );
if (indentMode == QJson::IndentMinimum) {
QByteArray indent = buildIndent(indentLevel);
str = indent + "{ ";
}
else if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) {
QByteArray indent = buildIndent(indentLevel);
QByteArray nextindent = buildIndent(indentLevel + 1);
str = indent + "{\n" + nextindent;
}
else if (indentMode == QJson::IndentCompact) {
str = "{";
}
else {
str = "{ ";
}
QList<QByteArray> pairs;
while ( it.hasNext() ) {
it.next();
QByteArray serializedValue = serialize( it.value(), ok, indentLevel + 1);
if ( !*ok ) {
break;
}
QByteArray key = escapeString( it.key() );
QByteArray value = serializedValue.trimmed();
if (indentMode == QJson::IndentCompact) {
pairs << key + ":" + value;
} else {
pairs << key + " : " + value;
}
}
if (indentMode == QJson::IndentFull) {
QByteArray indent = buildIndent(indentLevel + 1);
str += join( pairs, ",\n" + indent);
}
else if (indentMode == QJson::IndentCompact) {
str += join( pairs, "," );
}
else {
str += join( pairs, ", " );
}
if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) {
QByteArray indent = buildIndent(indentLevel);
str += "\n" + indent + "}";
}
else if (indentMode == QJson::IndentCompact) {
str += "}";
}
else {
str += " }";
}
} else {
// Add indent, we may need to remove it later for some layouts
switch(indentMode) {
case QJson::IndentFull :
case QJson::IndentMedium :
case QJson::IndentMinimum :
str += buildIndent(indentLevel);
break;
case QJson::IndentCompact :
case QJson::IndentNone :
default:
break;
}
if (( v.type() == QVariant::String ) || ( v.type() == QVariant::ByteArray )) { // a string or a byte array?
str += escapeString( v.toString() );
} else if (( v.type() == QVariant::Double) || ((QMetaType::Type)v.type() == QMetaType::Float)) { // a double or a float?
const double value = v.toDouble();
#if defined _WIN32 && !defined(Q_OS_SYMBIAN)
const bool special = _isnan(value) || !_finite(value);
#elif defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID) || defined(Q_OS_BLACKBERRY) || define
QJson使用VS2010+QT4.8编译
4星 · 超过85%的资源 需积分: 48 29 浏览量
2015-04-23
14:20:56
上传
评论 4
收藏 240KB ZIP 举报
「已注销」
- 粉丝: 1
- 资源: 8
最新资源
- 常用工具集参考用于图像等数据处理
- 音乐展示网页、基于Stenography的图像数字水印添加与提取,以及基于颜色矩和Tamura算法的图像相似度评估算法py源码
- 基于EmguCV(OpenCV .net封装),图像数字水印加解密算法的实现,其中包含最低有效位算法,离散傅里叶变换算法+文档书
- 基于matlab+DWT的图像水印项目,数字水印+源代码+文档说明+图片+报告pdf
- (优秀毕业设计)基于python实现的数字图像可视化水印系统的设计与实现,多种数字算法实现+源代码+文档说明+理论演示pdf
- 基于DWT-DCT-SVD和deflate压缩的数字水印方法python源码+Gui界面+演示视频(高分毕业设计)
- 基于matlab实现DWT、DCT、SVD算法数字图像水印可视化系统+GUI界面+文档说明+详细注释(高分毕业设计)
- NCIAE-Data-Structure大一大二笔记
- 学习wireshark笔记
- digital-image-数据可视化笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页