gsmiko 50954f5bba * created tag, trunk, branch directories 13 years ago
..
AUTHORS 50954f5bba * created tag, trunk, branch directories 13 years ago
README 50954f5bba * created tag, trunk, branch directories 13 years ago
json.cpp 50954f5bba * created tag, trunk, branch directories 13 years ago
json.h 50954f5bba * created tag, trunk, branch directories 13 years ago

README

########################################################################
1. INTRODUCTION

The Json class is a simple class for parsing JSON data into a hierarchy
of QMaps, QLists, and QVariants. Now, we can write JSON data from Qt
objects such as QString, QMap, QList and others.


########################################################################
2. HOW TO USE

The parse is really easy to use. Let's say we have the following JSON
data:

------------------------------------------------------------------------
{
"encoding" : "UTF-8",
"plug-ins" : [
"python",
"c++",
"ruby"
],
"indent" : {
"length" : 3,
"use_space" : true
}
}
------------------------------------------------------------------------


We would first call the parse-method:

------------------------------------------------------------------------
bool ok;
//json is a QString containing the JSON data
QVariantMap result = Json::parse(json, ok).toMap();

if(!ok) {
qFatal("An error occurred during parsing");
exit(1);
}
------------------------------------------------------------------------


Assuming the parsing process completed without errors, we would then
go through the hierarchy:

------------------------------------------------------------------------
qDebug() << "encoding:" << result["encoding"].toString();
qDebug() << "plugins:";

foreach(QVariant plugin, result["plug-ins"].toList()) {
qDebug() << "\t-" << plugin.toString();
}

QVariantMap nestedMap = result["indent"].toMap();
qDebug() << "length:" << nestedMap["length"].toInt();
qDebug() << "use_space:" << nestedMap["use_space"].toBool();
------------------------------------------------------------------------


The previous code would print out the following:

------------------------------------------------------------------------
encoding: "UTF-8"
plugins:
- "python"
- "c++"
- "ruby"
length: 3
use_space: true
------------------------------------------------------------------------

To write JSON data from Qt object is as simple as parsing:

------------------------------------------------------------------------
QVariantMap map;
map["name"] = "Name";
map["age"] = 22;

QByteArray data = Json::serialize(map);
------------------------------------------------------------------------

The byte array 'data' contains valid JSON data:

------------------------------------------------------------------------
{
name: "Luis Gustavo",
age: 22,
}
------------------------------------------------------------------------

########################################################################
3. LICENSE

The class is licensed under PUBLIC DOMAIN. That means that you can do
anything with it. Modify, Sell, etc., ANYTHING!


########################################################################
4. CONTRIBUTING

The code is available to download at GitHub. Contribute if you dare!