nextcloud-desktop/src/3rdparty/qjson
2013-10-18 02:00:19 +02:00
..
AUTHORS Add QtJson to 3rdparty, use it in ownCloudInfo 2013-10-18 02:00:19 +02:00
json.cpp Add QtJson to 3rdparty, use it in ownCloudInfo 2013-10-18 02:00:19 +02:00
json.h Add QtJson to 3rdparty, use it in ownCloudInfo 2013-10-18 02:00:19 +02:00
LICENSE Add QtJson to 3rdparty, use it in ownCloudInfo 2013-10-18 02:00:19 +02:00
README Add QtJson to 3rdparty, use it in ownCloudInfo 2013-10-18 02:00:19 +02:00

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

The Json class is a simple class for parsing JSON data into a QVariant 
hierarchies. Now, we can also reverse the process and serialize
QVariant hierarchies into valid JSON data.


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

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

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

We would first call the parse-method:

------------------------------------------------------------------------
//Say that we're using the QtJson namespace
using namespace QtJson;
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,
}
------------------------------------------------------------------------


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

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