1 /** |
|
2 \mainpage |
|
3 \section _intro Introduction |
|
4 |
|
5 <a HREF="http://www.json.org/">JSON (JavaScript Object Notation)</a> |
|
6 is a lightweight data-interchange format. |
|
7 It can represents integer, real number, string, an ordered sequence of value, and |
|
8 a collection of name/value pairs. |
|
9 |
|
10 QJson is a qt-based library that maps JSON data to QVariant objects. |
|
11 |
|
12 JSON arrays will be mapped to QVariantList instances, while JSON's objects will |
|
13 be mapped to QVariantMap. |
|
14 |
|
15 \section _usage Usage |
|
16 Converting JSON's data to QVariant instance is really simple: |
|
17 \code |
|
18 // create a JSonDriver instance |
|
19 QJson::Parser parser; |
|
20 |
|
21 bool ok; |
|
22 |
|
23 // json is a QString containing the data to convert |
|
24 QVariant result = parser.parse (json, &ok); |
|
25 \endcode |
|
26 |
|
27 Suppose you're going to convert this JSON data: |
|
28 \verbatim |
|
29 { |
|
30 "encoding" : "UTF-8", |
|
31 "plug-ins" : [ |
|
32 "python", |
|
33 "c++", |
|
34 "ruby" |
|
35 ], |
|
36 "indent" : { "length" : 3, "use_space" : true } |
|
37 } |
|
38 \endverbatim |
|
39 |
|
40 The following code would convert the JSON data and parse it: |
|
41 \code |
|
42 QJson::Parser parser; |
|
43 bool ok; |
|
44 |
|
45 QVariantMap result = parser.parse (json, &ok).toMap(); |
|
46 if (!ok) { |
|
47 qFatal("An error occured during parsing"); |
|
48 exit (1); |
|
49 } |
|
50 |
|
51 qDebug() << "encoding:" << result["encoding"].toString(); |
|
52 qDebug() << "plugins:"; |
|
53 |
|
54 foreach (QVariant plugin, result["plug-ins"].toList()) { |
|
55 qDebug() << "\t-" << plugin.toString(); |
|
56 } |
|
57 |
|
58 QVariantMap nestedMap = result["indent"].toMap(); |
|
59 qDebug() << "length:" << nestedMap["length"].toInt(); |
|
60 qDebug() << "use_space:" << nestedMap["use_space"].toBool(); |
|
61 \endcode |
|
62 The output would be: |
|
63 \verbatim |
|
64 encoding: "UTF-8" |
|
65 plugins: |
|
66 - "python" |
|
67 - "c++" |
|
68 - "ruby" |
|
69 length: 3 |
|
70 use_space: true |
|
71 \endverbatim |
|
72 |
|
73 The QJson::QObjectHelper class permits to serialize QObject instances into JSON. QJson::QObjectHelper also allows to |
|
74 initialize a QObject using the values stored inside of a JSON object. |
|
75 |
|
76 \section _build Build instructions |
|
77 QJson build system is based on cmake. Download QJson sources, extract them, move inside the sources directory and then: |
|
78 \code |
|
79 mkdir build |
|
80 cd build |
|
81 cmake .. |
|
82 make |
|
83 sudo make install |
|
84 \endcode |
|
85 |
|
86 \section _download Get the code |
|
87 Actually QJson code is hosted on KDE subversion repository. You can download it using a svn client: |
|
88 \code |
|
89 svn co svn://anonsvn.kde.org/home/kde/trunk/playground/libs/qjson |
|
90 \endcode |
|
91 |
|
92 Otherwise you can download source tarballs <a HREF="https://sourceforge.net/project/showfiles.php?group_id=244195">here</a>. |
|
93 |
|
94 \author Flavio Castelli <flavio@castelli.name> |
|
95 */ |
|