+
+QByteArray Serializer::SerializerPrivate::buildIndent(int spaces)
+{
+ QByteArray indent;
+ if (spaces < 0) {
+ spaces = 0;
+ }
+ for (int i = 0; i < spaces; i++ ) {
+ indent += " ";
+ }
+ return indent;
+}
+
+QString Serializer::SerializerPrivate::sanitizeString( QString str )
+{
+ str.replace( QLatin1String( "\\" ), QLatin1String( "\\\\" ) );
+
+ // escape unicode chars
+ QString result;
+ const ushort* unicode = str.utf16();
+ unsigned int i = 0;
+
+ while ( unicode[ i ] ) {
+ if ( unicode[ i ] < 128 ) {
+ result.append( QChar( unicode[ i ] ) );
+ }
+ else {
+ QString hexCode = QString::number( unicode[ i ], 16 ).rightJustified( 4,
+ QLatin1Char('0') );
+
+ result.append( QLatin1String ("\\u") ).append( hexCode );
+ }
+ ++i;
+ }
+ str = result;
+
+ str.replace( QLatin1String( "\"" ), QLatin1String( "\\\"" ) );
+ str.replace( QLatin1String( "\b" ), QLatin1String( "\\b" ) );
+ str.replace( QLatin1String( "\f" ), QLatin1String( "\\f" ) );
+ str.replace( QLatin1String( "\n" ), QLatin1String( "\\n" ) );
+ str.replace( QLatin1String( "\r" ), QLatin1String( "\\r" ) );
+ str.replace( QLatin1String( "\t" ), QLatin1String( "\\t" ) );
+
+ return QString( QLatin1String( "\"%1\"" ) ).arg( str );
+}
+
+Serializer::Serializer()
+ : d( new SerializerPrivate )
+{
+}
+
+Serializer::~Serializer() {
+ delete d;
+}
+
+void Serializer::serialize( const QVariant& v, QIODevice* io, bool* ok)
+{
+ Q_ASSERT( io );
+ if (ok)
+ *ok = false;
+
+ if (!io->isOpen()) {
+ if (!io->open(QIODevice::WriteOnly)) {
+ qCritical ("Error opening device");
+ return;
+ }
+ }
+
+ if (!io->isWritable()) {
+ qCritical ("Device is not readable");
+ io->close();
+ return;
+ }
+
+ const QByteArray str = serialize( v );
+ if (io->write(str) == str.count()) {
+ if (ok)
+ *ok = true;
+ }
+}
+
+QByteArray Serializer::serialize( const QVariant &v)
+{
+ return d->serialize(v);
+}
+
+void QJson::Serializer::allowSpecialNumbers(bool allow) {
+ d->specialNumbersAllowed = allow;
+}
+
+bool QJson::Serializer::specialNumbersAllowed() const {
+ return d->specialNumbersAllowed;
+}
+
+void QJson::Serializer::setIndentMode(IndentMode mode) {
+ d->indentMode = mode;
+}
+
+void QJson::Serializer::setDoublePrecision(int precision) {
+ d->doublePrecision = precision;
+}
+
+IndentMode QJson::Serializer::indentMode() const {
+ return d->indentMode;
+}