1 /* This file is part of qjson
3 * Copyright (C) 2009 Till Adam <adam@kde.org>
4 * Copyright (C) 2009 Flavio Castelli <flavio@castelli.name>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "serializer.h"
24 #include <QtCore/QDataStream>
25 #include <QtCore/QStringList>
26 #include <QtCore/QVariant>
30 #ifdef _MSC_VER // using MSVC compiler
34 using namespace QJson;
36 class Serializer::SerializerPrivate {
39 specialNumbersAllowed(false),
40 indentMode(QJson::IndentNone),
43 bool specialNumbersAllowed;
44 IndentMode indentMode;
46 QByteArray buildIndent(int spaces);
47 QByteArray serialize( const QVariant &v, int reserved = 0);
48 QString sanitizeString( QString str );
49 QByteArray join( const QList<QByteArray>& list, const QByteArray& sep );
52 QByteArray Serializer::SerializerPrivate::join( const QList<QByteArray>& list, const QByteArray& sep ) {
54 Q_FOREACH( const QByteArray& i, list ) {
62 QByteArray Serializer::SerializerPrivate::serialize( const QVariant &v, int reserved )
68 if ( ! v.isValid() ) { // invalid or null?
70 } else if (( v.type() == QVariant::List ) || ( v.type() == QVariant::StringList )){ // an array or a stringlist?
71 const QVariantList list = v.toList();
72 QList<QByteArray> values;
73 Q_FOREACH( const QVariant& var, list )
76 QByteArray serializedValue = serialize( var,reserved );
78 if ( serializedValue.isNull() ) {
82 values << serializedValue;
85 if (indentMode == QJson::IndentMinimum) {
86 QByteArray indent = buildIndent(reserved - 1);
87 str = "[\n" + join( values, ",\n" ) + "\n" + indent + "]";
89 else if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) {
90 QByteArray indent = buildIndent(reserved);
91 str = "[\n" + join( values, ",\n" ) + "\n" + indent + "]";
93 else if (indentMode == QJson::IndentCompact) {
94 str = "[" + join( values, "," ) + "]";
97 str = "[ " + join( values, ", " ) + " ]";
100 } else if ( v.type() == QVariant::Map ) { // variant is a map?
101 const QVariantMap vmap = v.toMap();
102 QMapIterator<QString, QVariant> it( vmap );
104 if (indentMode == QJson::IndentMinimum) {
105 QByteArray indent = buildIndent(reserved);
108 else if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) {
109 QByteArray indent = buildIndent(reserved);
110 QByteArray nextindent = buildIndent(reserved + 1);
111 str = indent + "{\n" + nextindent;
113 else if (indentMode == QJson::IndentCompact) {
120 QList<QByteArray> pairs;
121 while ( it.hasNext() ) {
124 QByteArray serializedValue = serialize( it.value() , reserved);
126 if ( serializedValue.isNull() ) {
130 QByteArray key = sanitizeString( it.key() ).toUtf8();
131 QByteArray value = serializedValue;
132 if (indentMode == QJson::IndentCompact) {
133 pairs << key + ":" + value;
135 pairs << key + " : " + value;
139 if (indentMode == QJson::IndentFull) {
140 QByteArray indent = buildIndent(reserved + 1);
141 str += join( pairs, ",\n" + indent);
143 else if (indentMode == QJson::IndentCompact) {
144 str += join( pairs, "," );
147 str += join( pairs, ", " );
150 if (indentMode == QJson::IndentMedium || indentMode == QJson::IndentFull) {
151 QByteArray indent = buildIndent(reserved);
152 str += "\n" + indent + "}";
154 else if (indentMode == QJson::IndentCompact) {
161 } else if (( v.type() == QVariant::String ) || ( v.type() == QVariant::ByteArray )) { // a string or a byte array?
162 str = sanitizeString( v.toString() ).toUtf8();
163 } else if (( v.type() == QVariant::Double) || ((QMetaType::Type)v.type() == QMetaType::Float)) { // a double or a float?
164 const double value = v.toDouble();
165 #if defined _WIN32 && !defined(Q_OS_SYMBIAN)
166 const bool special = _isnan(value) || !_finite(value);
167 #elif defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID)
168 const bool special = isnan(value) || isinf(value);
170 const bool special = std::isnan(value) || std::isinf(value);
173 if (specialNumbersAllowed) {
174 #if defined _WIN32 && !defined(Q_OS_SYMBIAN)
176 #elif defined(Q_OS_SYMBIAN) || defined(Q_OS_ANDROID)
179 if (std::isnan(value)) {
189 qCritical("Attempt to write NaN or infinity, which is not supported by json");
193 str = QByteArray::number( value , 'g', doublePrecision);
194 if( ! str.contains( "." ) && ! str.contains( "e" ) ) {
198 } else if ( v.type() == QVariant::Bool ) { // boolean value?
199 str = ( v.toBool() ? "true" : "false" );
200 } else if ( v.type() == QVariant::ULongLong ) { // large unsigned number?
201 str = QByteArray::number( v.value<qulonglong>() );
202 } else if ( v.canConvert<qlonglong>() ) { // any signed number?
203 str = QByteArray::number( v.value<qlonglong>() );
204 } else if ( v.canConvert<QString>() ){ // can value be converted to string?
205 // this will catch QDate, QDateTime, QUrl, ...
206 str = sanitizeString( v.toString() ).toUtf8();
207 //TODO: catch other values like QImage, QRect, ...
219 QByteArray Serializer::SerializerPrivate::buildIndent(int spaces)
225 for (int i = 0; i < spaces; i++ ) {
231 QString Serializer::SerializerPrivate::sanitizeString( QString str )
233 str.replace( QLatin1String( "\\" ), QLatin1String( "\\\\" ) );
235 // escape unicode chars
237 const ushort* unicode = str.utf16();
240 while ( unicode[ i ] ) {
241 if ( unicode[ i ] < 128 ) {
242 result.append( QChar( unicode[ i ] ) );
245 QString hexCode = QString::number( unicode[ i ], 16 ).rightJustified( 4,
248 result.append( QLatin1String ("\\u") ).append( hexCode );
254 str.replace( QLatin1String( "\"" ), QLatin1String( "\\\"" ) );
255 str.replace( QLatin1String( "\b" ), QLatin1String( "\\b" ) );
256 str.replace( QLatin1String( "\f" ), QLatin1String( "\\f" ) );
257 str.replace( QLatin1String( "\n" ), QLatin1String( "\\n" ) );
258 str.replace( QLatin1String( "\r" ), QLatin1String( "\\r" ) );
259 str.replace( QLatin1String( "\t" ), QLatin1String( "\\t" ) );
261 return QString( QLatin1String( "\"%1\"" ) ).arg( str );
264 Serializer::Serializer()
265 : d( new SerializerPrivate )
269 Serializer::~Serializer() {
273 void Serializer::serialize( const QVariant& v, QIODevice* io, bool* ok)
280 if (!io->open(QIODevice::WriteOnly)) {
281 qCritical ("Error opening device");
286 if (!io->isWritable()) {
287 qCritical ("Device is not readable");
292 const QByteArray str = serialize( v );
293 if (io->write(str) == str.count()) {
299 QByteArray Serializer::serialize( const QVariant &v)
301 return d->serialize(v);
304 void QJson::Serializer::allowSpecialNumbers(bool allow) {
305 d->specialNumbersAllowed = allow;
308 bool QJson::Serializer::specialNumbersAllowed() const {
309 return d->specialNumbersAllowed;
312 void QJson::Serializer::setIndentMode(IndentMode mode) {
313 d->indentMode = mode;
316 void QJson::Serializer::setDoublePrecision(int precision) {
317 d->doublePrecision = precision;
320 IndentMode QJson::Serializer::indentMode() const {
321 return d->indentMode;