TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
Classes | Public Member Functions | List of all members
QJson::Serializer Class Reference

Main class used to convert QVariant objects to JSON data. More...

#include <serializer.h>

Classes

class  SerializerPrivate
 Main class used to convert QVariant objects to JSON data private. More...
 

Public Member Functions

 Serializer ()
 
void serialize (const QVariant &variant, QIODevice *out, bool *ok)
 This method generates a textual JSON representation and outputs it to the passed in I/O Device. More...
 
QByteArray serialize (const QVariant &variant, bool *ok)
 This is a method provided for convenience. More...
 
void allowSpecialNumbers (bool allow)
 Allow or disallow writing of NaN and/or Infinity (as an extension to QJson) More...
 
bool specialNumbersAllowed () const
 Is Nan and/or Infinity allowed? More...
 
void setIndentMode (IndentMode mode=QJson::IndentNone)
 set output indentation mode as defined in QJson::IndentMode More...
 
void setDoublePrecision (int precision)
 set double precision used while converting Double More...
 
IndentMode indentMode () const
 Returns one of the indentation modes defined in QJson::IndentMode. More...
 
QString errorMessage () const
 Returns the error message. More...
 

Detailed Description

Main class used to convert QVariant objects to JSON data.

QVariant objects are converted to a string containing the JSON data.

Usage:

QVariantList people;
QVariantMap bob;
bob.insert("Name", "Bob");
bob.insert("Phonenumber", 123);
QVariantMap alice;
alice.insert("Name", "Alice");
alice.insert("Phonenumber", 321);
people << bob << alice;
QJson::Serializer serializer;
bool ok = false;
QByteArray json = serializer.serialize(people, &ok);
if (ok) {
qDebug() << json;
} else {
qCritical() << "Something went wrong:" << serializer.errorMessage();
}

The output will be:

"[ { "Name" : "Bob", "Phonenumber" : 123 },
{ "Name" : "Alice", "Phonenumber" : 321 } ]"

It's possible to tune the indentation level of the resulting string.

See also
setIndentMode

Definition at line 151 of file serializer.h.

Constructor & Destructor Documentation

Serializer::Serializer ( )

Definition at line 415 of file serializer.cpp.

References TTK_INIT_PRIVATE.

Member Function Documentation

void QJson::Serializer::allowSpecialNumbers ( bool  allow)

Allow or disallow writing of NaN and/or Infinity (as an extension to QJson)

Definition at line 464 of file serializer.cpp.

References TTK_D.

QString QJson::Serializer::errorMessage ( ) const

Returns the error message.

Definition at line 494 of file serializer.cpp.

References TTK_D.

Referenced by QJson::SerializerRunnable::run().

IndentMode QJson::Serializer::indentMode ( ) const

Returns one of the indentation modes defined in QJson::IndentMode.

Definition at line 488 of file serializer.cpp.

References TTK_D.

void Serializer::serialize ( const QVariant &  variant,
QIODevice *  out,
bool *  ok 
)

This method generates a textual JSON representation and outputs it to the passed in I/O Device.

Parameters
variantThe JSON document in its in-memory representation as generated by the parser.
outInput output device
okif a conversion error occurs, *ok is set to false; otherwise *ok is set to true

Definition at line 420 of file serializer.cpp.

References TTK_D.

Referenced by QJson::SerializerRunnable::run(), and MusicJSPFConfigManager::writeBuffer().

QByteArray Serializer::serialize ( const QVariant &  variant,
bool *  ok 
)

This is a method provided for convenience.

It turns the passed in in-memory representation of the JSON document into a textual one, which is returned. If the returned string is empty, the document was empty. If it was null, there was a parsing error.

Parameters
variantThe JSON document in its in-memory representation as generated by the parser.
okif a conversion error occurs, *ok is set to false; otherwise *ok is set to true

Definition at line 449 of file serializer.cpp.

References TTK_D.

void QJson::Serializer::setDoublePrecision ( int  precision)

set double precision used while converting Double

See also
QByteArray::number

Definition at line 482 of file serializer.cpp.

References TTK_D.

void QJson::Serializer::setIndentMode ( IndentMode  mode = QJson::IndentNone)

set output indentation mode as defined in QJson::IndentMode

Definition at line 476 of file serializer.cpp.

References TTK_D.

bool QJson::Serializer::specialNumbersAllowed ( ) const

Is Nan and/or Infinity allowed?

Definition at line 470 of file serializer.cpp.

References TTK_D.


The documentation for this class was generated from the following files: