qmjsonvalue.h
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 #ifndef QMJSONVALUE_H
23 #define QMJSONVALUE_H
24 
25 // ============================================================================
26 // Libraries
27 // ============================================================================
28 
29 #include <qmjsontype.h>
30 
31 // ============================================================================
32 // Class Prototypes
33 // ============================================================================
34 
35 class QMJsonArray;
36 class QMJsonObject;
37 
38 // ============================================================================
39 // Typedefs
40 // ============================================================================
41 
42 typedef QMPointer<QMJsonValue> (*FromComplexJsonFunc)(const QMPointer<QMJsonObject> &obj);
43 
44 // ============================================================================
45 // Class Definition
46 // ============================================================================
47 
48 class QM_JSON_EXPORT QMJsonValue : public QObject
49 {
50  Q_OBJECT
51 
52  template<class T> friend class QMJsonType;
53  friend QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMJsonValue &value);
54 
55 public:
56 
57  QMJsonValue();
58  explicit QMJsonValue(bool value);
59  explicit QMJsonValue(double value);
60  explicit QMJsonValue(const QString &value);
61  explicit QMJsonValue(const QMPointer<QMJsonValue> &value);
62  explicit QMJsonValue(const QMPointer<QMJsonArray> &value);
63  explicit QMJsonValue(const QMPointer<QMJsonObject> &value);
64  explicit QMJsonValue(QMJsonValue *value);
65  explicit QMJsonValue(QMJsonArray *value);
66  explicit QMJsonValue(QMJsonObject *value);
67  template<class T> explicit QMJsonValue(const T &value);
68 
69  explicit QMJsonValue(const char *value);
70  explicit QMJsonValue(float value);
71  explicit QMJsonValue(char value);
72  explicit QMJsonValue(int8_t value);
73  explicit QMJsonValue(uint8_t value);
74  explicit QMJsonValue(int16_t value);
75  explicit QMJsonValue(uint16_t value);
76  explicit QMJsonValue(int32_t value);
77  explicit QMJsonValue(uint32_t value);
78  explicit QMJsonValue(int64_t value);
79  explicit QMJsonValue(uint64_t value);
80 
81 #if UINTPTR_MAX == 0xffffffff
82 
83  explicit QMJsonValue(long value);
84  explicit QMJsonValue(unsigned long value);
85 
86 #elif UINTPTR_MAX == 0xffffffffffffffff
87 
88  explicit QMJsonValue(long long value);
89  explicit QMJsonValue(unsigned long long value);
90 
91 #endif
92 
93  virtual ~QMJsonValue();
94 
95  virtual bool isNull(void) const;
96  virtual bool isBool(void) const;
97  virtual bool isDouble(void) const;
98  virtual bool isString(void) const;
99  virtual bool isArray(void) const;
100  virtual bool isObject(void) const;
101  template<class T> bool is(void) const;
102 
103  virtual bool toBool(void) const;
104  virtual double toDouble(void) const;
105  virtual QString toString(void) const;
106  virtual const QMPointer<QMJsonArray> &toArray(void) const;
107  virtual const QMPointer<QMJsonObject> &toObject(void) const;
108 
109  virtual bool toBool(bool defaultValue) const;
110  virtual double toDouble(double defaultValue) const;
111  virtual const QString &toString(const QString &defaultValue) const;
112  virtual const QMPointer<QMJsonArray> &toArray(const QMPointer<QMJsonArray> &defaultValue) const;
113  virtual const QMPointer<QMJsonObject> &toObject(const QMPointer<QMJsonObject> &defaultValue) const;
114  template<class T> const T &to(const T &defaultValue) const;
115 
116  virtual bool fromBool(bool value);
117  virtual bool fromDouble(double value);
118  virtual bool fromString(const QString &value);
119  virtual bool fromArray(const QMPointer<QMJsonArray> &value);
120  virtual bool fromObject(const QMPointer<QMJsonObject> &value);
121  virtual bool from(const QMPointer<QMJsonValue> &value);
122  template <class T> bool from(const T &value);
123 
124  virtual const QMPointer<QMJsonValue> &value(int32_t index) const;
125  virtual const QMPointer<QMJsonValue> &value(const QString &key) const;
126 
127  virtual QVariant toVariant(void);
128  static QMPointer<QMJsonValue> fromVariant(const QVariant &value);
129 
130  virtual QMJsonValueType type(void) const;
131  virtual const QString &typeString(void) const;
132 
133  virtual QString toJson(QMJsonFormat format = QMJsonFormat_Pretty, QMJsonSort sort = QMJsonSort_None) const;
134  virtual bool toJsonFile(const QString &filename, QMJsonFormat format = QMJsonFormat_Pretty, QMJsonSort sort = QMJsonSort_None) const;
135  static QMPointer<QMJsonValue> fromJson(const QString &json);
136  static QMPointer<QMJsonValue> fromJsonFile(const QString &filename);
137 
138  static void registerFromComplexJson(const QString &qmjsontype, FromComplexJsonFunc func);
139 
140 signals:
141 
142  void valueChanged(void);
143 
144 private:
145 
146  static void skipSpaces(const QString &json, int32_t &index);
147  static void verifyIndex(const QString &json, int32_t &index);
148  static QMPointer<QMJsonValue> fromJson(const QString &json, int32_t &index);
149  static QMPointer<QMJsonValue> fromComplexJson(const QMPointer<QMJsonValue> &value);
150  static void throwError(const QString &json, int32_t index, QString error);
151 
152  template <class T> bool set(const T &value);
153 
154 private:
155 
156  Q_DISABLE_COPY(QMJsonValue);
157 
158  QMJsonValueType mType;
159  QMPointer<QMJsonTypeBase> mValue;
160 
161  static QMap<QString, FromComplexJsonFunc> mFromFuncs;
162 };
163 
164 // ============================================================================
165 // Template Specializations
166 // ============================================================================
167 
168 QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMJsonValue &value);
169 QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMPointer<QMJsonValue> &value);
170 
171 // ============================================================================
172 // Class Implementation
173 // ============================================================================
174 
175 template<class T>
177 {
178  auto type = QSharedPointer<QMJsonType<T> >(new QMJsonType<T>(value));
179 
180  mType = QMJsonValueType_Custom;
181  mValue = qSharedPointerDynamicCast<QMJsonTypeBase>(type);
182 }
183 
184 template<class T>
185 bool QMJsonValue::is(void) const
186 {
187  return qSharedPointerDynamicCast<QMJsonType<T> >(mValue) != NULL;
188 }
189 
190 template<class T>
191 const T &QMJsonValue::to(const T &defaultValue) const
192 {
193  auto type = qSharedPointerDynamicCast<QMJsonType<T> >(mValue);
194 
195  if(type == NULL)
196  return defaultValue;
197 
198  return type->get();
199 }
200 
201 template<class T>
202 bool QMJsonValue::from(const T &value)
203 {
204  switch(mType)
205  {
207  return false;
208 
212  return this->set<T>(value);
213 
216  return false;
217 
219  return this->set<T>(value);
220  };
221 
222  return false;
223 }
224 
225 template<class T>
226 bool QMJsonValue::set(const T &value)
227 {
228  auto type = qSharedPointerDynamicCast<QMJsonType<T> >(mValue);
229 
230  if(type == NULL)
231  return false;
232 
233  type->set(value);
234 
235  emit valueChanged();
236  return true;
237 }
238 
239 #endif // QMJSONVALUE_H
240 
virtual QString toJson(int32_t tab, QMJsonSort sort)
Definition: qmjsontype.h:189
#define QM_JSON_EXPORT
Definition: qmjsontype.h:36
QMJsonFormat
Definition: qmjsontype.h:62
virtual void set(const T &value)
Definition: qmjsontype.h:171
static QMPointer< QMJsonValue > fromJson(const QString &json, int32_t &index)
Definition: qmjsontype.h:198
bool is(void) const
Definition: qmjsonvalue.h:185
void valueChanged(void)
virtual const QMPointer< QMJsonValue > & value(int32_t index) const
virtual QMJsonValueType type(void) const
virtual bool from(const QMPointer< QMJsonValue > &value)
QMJsonValueType
Definition: qmjsontype.h:45
QMJsonSort
Definition: qmjsontype.h:68
const T & to(const T &defaultValue) const
Definition: qmjsonvalue.h:191
QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMJsonValue &value)
static QMPointer< QMJsonValue > fromComplexJson(const QMPointer< QMJsonObject > &obj)
QMPointer< QMJsonValue >(* FromComplexJsonFunc)(const QMPointer< QMJsonObject > &obj)
Definition: qmjsonvalue.h:42