main.cpp
Go to the documentation of this file.
1 //
2 // QtMark JSON Library
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Rodney Forbes <forbesr@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <QtGui>
23 
24 #include <qmjson.h>
25 #include <qmjsongui.h>
26 
27 int main(int argc, char const *argv[])
28 {
29  (void) argc;
30  (void) argv;
31 
32  //--------------------------------------------------------------------------
33  // Setup
34  //--------------------------------------------------------------------------
35 
40 
41  auto value1 = QMPointer<QMJsonValue>(new QMJsonValue(5.5));
42  auto value2 = QMPointer<QMJsonValue>(new QMJsonValue("Hello"));
43  auto value3 = QMPointer<QMJsonValue>(new QMJsonValue(true));
44  auto array = QMPointer<QMJsonArray>(new QMJsonArray());
45  auto object = QMPointer<QMJsonObject>(new QMJsonObject());
46  auto tree = QMPointer<QMJsonObject>(new QMJsonObject());
47  auto document = QMPointer<QMJsonValue>(new QMJsonValue(tree));
48 
49  array->append(value1);
50  array->append(value2);
51  array->append(value3);
52 
53  object->insert("key1", value1);
54  object->insert("key2", value2);
55  object->insert("key3", value3);
56 
57  tree->insert("array", array);
58  tree->insert("object", object);
59 
60  auto complexValue1 = QMPointer<QMJsonValue>(new QMJsonValue(QColor("red")));
61  auto complexValue2 = QMPointer<QMJsonValue>(new QMJsonValue(QPoint(2, 2)));
62  auto complexValue3 = QMPointer<QMJsonValue>(new QMJsonValue(QRect(5, 5, 3, 3)));
63  auto complexValue4 = QMPointer<QMJsonValue>(new QMJsonValue(QSize(10, 10)));
64 
65  //--------------------------------------------------------------------------
66  // Valid
67  //--------------------------------------------------------------------------
68 
69  qDebug() << "Valid:";
70 
71  qDebug() << " " << value1;
72  qDebug() << " " << value2;
73  qDebug() << " " << value3;
74 
75  qDebug() << " " << array;
76  qDebug() << " " << object;
77 
78  qDebug() << " " << array->toDouble(0);
79  qDebug() << " " << array->toString(1);
80  qDebug() << " " << array->toBool(2);
81 
82  qDebug() << " " << array->value(0);
83  qDebug() << " " << array->value(1);
84  qDebug() << " " << array->value(2);
85 
86  qDebug() << " " << object->toDouble("key1");
87  qDebug() << " " << object->toString("key2");
88  qDebug() << " " << object->toBool("key3");
89 
90  qDebug() << " " << object->value("key1");
91  qDebug() << " " << object->value("key2");
92  qDebug() << " " << object->value("key3");
93 
94  qDebug() << "";
95 
96  //--------------------------------------------------------------------------
97  // Invalid
98  //--------------------------------------------------------------------------
99 
100  qDebug() << "Invalid:";
101 
102  qDebug() << " " << array->toString(0);
103  qDebug() << " " << array->toBool(1);
104  qDebug() << " " << array->toDouble(2);
105 
106  qDebug() << " " << array->toDouble(3);
107  qDebug() << " " << array->toString(4);
108  qDebug() << " " << array->toBool(5);
109 
110  qDebug() << " " << array->value(6);
111  qDebug() << " " << array->value(7);
112  qDebug() << " " << array->value(8);
113 
114  qDebug() << " " << object->toString("key1");
115  qDebug() << " " << object->toBool("key2");
116  qDebug() << " " << object->toDouble("key3");
117 
118  qDebug() << " " << object->toDouble("key4");
119  qDebug() << " " << object->toString("key5");
120  qDebug() << " " << object->toBool("key6");
121 
122  qDebug() << " " << object->value("key7");
123  qDebug() << " " << object->value("key8");
124  qDebug() << " " << object->value("key9");
125 
126  qDebug() << "";
127 
128  //--------------------------------------------------------------------------
129  // From (Setting)
130  //--------------------------------------------------------------------------
131 
132  qDebug() << "From:";
133 
134  value1->fromDouble(10.3);
135  value2->fromString("World");
136  value3->fromBool(false);
137 
138  qDebug() << " " << value1;
139  qDebug() << " " << value2;
140  qDebug() << " " << value3;
141  qDebug() << "";
142 
143  //--------------------------------------------------------------------------
144  // Tree Example
145  //--------------------------------------------------------------------------
146 
147  qDebug() << "Tree:";
148  qDebug() << " " << tree;
149  qDebug() << "";
150 
151  //--------------------------------------------------------------------------
152  // Save
153  //--------------------------------------------------------------------------
154 
155  qDebug() << "Save:";
156  qDebug() << document->toJsonFile("example.json", QMJsonFormat_Pretty);
157  qDebug() << "";
158 
159  //--------------------------------------------------------------------------
160  // Load
161  //--------------------------------------------------------------------------
162 
163  qDebug() << "Load:";
164  qDebug() << QMJsonValue::fromJsonFile("example.json");
165  qDebug() << "";
166 
167  //--------------------------------------------------------------------------
168  // Complex Types
169  //--------------------------------------------------------------------------
170 
171 
172  qDebug() << "Complex Values:";
173  qDebug() << complexValue1;
174  qDebug() << complexValue2;
175  qDebug() << complexValue3;
176  qDebug() << complexValue4;
177  qDebug() << "";
178 
179  qDebug() << "Is:";
180  qDebug() << complexValue1->is<QColor>();
181  qDebug() << complexValue2->is<QPoint>();
182  qDebug() << complexValue3->is<QRect>();
183  qDebug() << complexValue4->is<QSize>();
184  qDebug() << "";
185 
186  qDebug() << "To:";
187  qDebug() << complexValue1->to<QColor>(QColor());
188  qDebug() << complexValue2->to<QPoint>(QPoint());
189  qDebug() << complexValue3->to<QRect>(QRect());
190  qDebug() << complexValue4->to<QSize>(QSize());
191  qDebug() << "";
192 
193  qDebug() << "From:";
194  qDebug() << complexValue1->from<QColor>(QColor("blue"));
195  qDebug() << complexValue2->from<QPoint>(QPoint(4, 8));
196  qDebug() << complexValue3->from<QRect>(QRect(15, 16, 23, 42));
197  qDebug() << complexValue4->from<QSize>(QSize(21, 12));
198  qDebug() << "";
199 
200  qDebug() << "Complex to JSON:";
201  qDebug() << qPrintable(complexValue1->toJson());
202  qDebug() << qPrintable(complexValue2->toJson());
203  qDebug() << qPrintable(complexValue3->toJson());
204  qDebug() << qPrintable(complexValue4->toJson());
205  qDebug() << "";
206 
207  qDebug() << "Complex from JSON:";
208  qDebug() << QMJsonValue::fromJson(complexValue1->toJson());
209  qDebug() << QMJsonValue::fromJson(complexValue2->toJson());
210  qDebug() << QMJsonValue::fromJson(complexValue3->toJson());
211  qDebug() << QMJsonValue::fromJson(complexValue4->toJson());
212  qDebug() << "";
213 
214  return 0;
215 }
static QMPointer< QMJsonValue > fromJson(const QString &json)
int main(int argc, char const *argv[])
Definition: main.cpp:27
static QMPointer< QMJsonValue > fromJsonFile(const QString &filename)
static void registerFromComplexJson(const QString &qmjsontype, FromComplexJsonFunc func)