QJson - a Qt based library for mapping JSON data to QVariant objects

QJson home page

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- Class List
- Class Members

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>`

## 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();
}

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

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

QString QJson::Serializer::errorMessage | ( |  | ) | const

Returns the error message

Definition at line 410 of file serializer.cpp.

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

IndentMode QJson::Serializer::indentMode | ( |  | ) | const

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

Definition at line 406 of file serializer.cpp.

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

  variant | The JSON document in its in-memory representation as generated by the parser.
  out | Input output device
  ok | if a conversion error occurs, *ok is set to false; otherwise *ok is set to true

Definition at line 342 of file serializer.cpp.

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

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

  variant | The 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 369 of file serializer.cpp.

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

  variant | The JSON document in its in-memory representation as generated by the parser.
  ok | if a conversion error occurs, *ok is set to false; otherwise *ok is set to true

Definition at line 376 of file serializer.cpp.

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

set double precision used while converting Double

See also
  QByteArray::number

Definition at line 402 of file serializer.cpp.

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

set output indentation mode as defined in QJson::IndentMode

Definition at line 398 of file serializer.cpp.

bool QJson::Serializer::specialNumbersAllowed | ( |  | ) | const

Is Nan and/or Infinity allowed?

Definition at line 394 of file serializer.cpp.

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

- /usr/obj/ports/qjson-0.8.1/qjson-0.8.1/src/serializer.h
- /usr/obj/ports/qjson-0.8.1/qjson-0.8.1/src/serializer.cpp

| hosts this site. |  | Send comments to: QJson Developers