QJson home page
QJson::Serializer Class Reference

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

#include <serializer.h>

Public Member Functions

void serialize (const QVariant &variant, QIODevice *out, bool *ok)
 
QByteArray serialize (const QVariant &variant)
 
QByteArray serialize (const QVariant &variant, bool *ok)
 
void allowSpecialNumbers (bool allow)
 
bool specialNumbersAllowed () const
 
void setIndentMode (IndentMode mode=QJson::IndentNone)
 
void setDoublePrecision (int precision)
 
IndentMode indentMode () const
 
QString errorMessage () const
 

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;
QByteArray json = serializer.serialize(people, &ok);
if (ok) {
qDebug() << json;
} else {
qCritical() << "Something went wrong:" << serializer.errorMessage();
}
Main class used to convert QVariant objects to JSON data.
Definition serializer.h:149
void serialize(const QVariant &variant, QIODevice *out, bool *ok)
QString errorMessage() const

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 149 of file serializer.h.

Member Function Documentation

◆ allowSpecialNumbers()

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

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

Definition at line 446 of file serializer.cpp.

◆ errorMessage()

QString QJson::Serializer::errorMessage ( ) const

Returns the error message

Definition at line 466 of file serializer.cpp.

◆ indentMode()

IndentMode QJson::Serializer::indentMode ( ) const

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

Definition at line 462 of file serializer.cpp.

◆ serialize() [1/3]

QByteArray Serializer::serialize ( const QVariant & variant)

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.
Deprecated
This method is going to be removed with the next major release of QJson.

Definition at line 425 of file serializer.cpp.

References serialize().

◆ serialize() [2/3]

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 432 of file serializer.cpp.

◆ serialize() [3/3]

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 398 of file serializer.cpp.

References serialize().

Referenced by serialize(), and serialize().

◆ setDoublePrecision()

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

set double precision used while converting Double

See also
QByteArray::number

Definition at line 458 of file serializer.cpp.

◆ setIndentMode()

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

set output indentation mode as defined in QJson::IndentMode

Definition at line 454 of file serializer.cpp.

◆ specialNumbersAllowed()

bool QJson::Serializer::specialNumbersAllowed ( ) const

Is Nan and/or Infinity allowed?

Definition at line 450 of file serializer.cpp.


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

SourceForge Logo hosts this site. Send comments to:
QJson Developers