qmjsonobject.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 QMJSONOBJECT_H
23 #define QMJSONOBJECT_H
24 
25 // ============================================================================
26 // Libraries
27 // ============================================================================
28 
29 #include <qmjsonvalue.h>
30 
31 // ============================================================================
32 // Class Definition
33 // ============================================================================
34 
35 class QM_JSON_EXPORT QMJsonObject : public QObject
36 {
37  Q_OBJECT
38 
39 public:
40 
41  QMJsonObject();
42  explicit QMJsonObject(const QHash<QString, QMPointer<QMJsonValue> > &hash);
43  virtual ~QMJsonObject();
44 
45  virtual void reserve(int32_t alloc);
46  virtual int32_t capacity(void) const;
47  virtual void squeeze(void);
48 
49  virtual void clear(void);
50 
51  virtual int32_t count(void) const;
52  virtual int32_t size(void) const;
53 
54  virtual bool isEmpty(void) const;
55  virtual bool empty(void) const;
56 
57  virtual bool contains(const QString &key) const;
58 
59  virtual void insert(const QString &key, const QMPointer<QMJsonValue> &value, QMJsonReplacementPolicy policy = QMJsonReplacementPolicy_Replace);
60  template<class T> void insert(const QString &key, const T &value, QMJsonReplacementPolicy policy = QMJsonReplacementPolicy_Replace);
61 
62  virtual void unite(const QMPointer<QMJsonObject> &object, QMJsonReplacementPolicy replacementPolicy = QMJsonReplacementPolicy_Replace, QMJsonArrayUnitePolicy unitePolicy = QMJsonArrayUnitePolicy_Append);
63 
64  virtual void remove(const QString &key);
65  virtual QMPointer<QMJsonValue> take(const QString &key);
66  virtual QMPointer<QMJsonValue> take(const QString &key, const QMPointer<QMJsonValue> &defaultValue);
67  template<class T> QMPointer<QMJsonValue> take(const QString &key, const T &defaultValue);
68 
69  virtual const QString key(const QMPointer<QMJsonValue> &value) const;
70  virtual const QString key(const QMPointer<QMJsonValue> &value, const QString &defaultValue) const;
71 
72  virtual QHash<QString, QMPointer<QMJsonValue> >::iterator begin(void);
73  virtual QHash<QString, QMPointer<QMJsonValue> >::iterator end(void);
74  virtual QHash<QString, QMPointer<QMJsonValue> >::const_iterator cbegin(void) const;
75  virtual QHash<QString, QMPointer<QMJsonValue> >::const_iterator cend(void) const;
76 
77  virtual const QHash<QString, QMPointer<QMJsonValue> >::iterator erase(const QHash<QString, QMPointer<QMJsonValue> >::iterator &iter);
78  virtual QHash<QString, QMPointer<QMJsonValue> >::iterator find(const QString &key);
79  virtual const QHash<QString, QMPointer<QMJsonValue> >::const_iterator cfind(const QString &key) const;
80 
81  virtual const QMPointer<QMJsonValue> &value(const QString &key) const;
82  virtual const QMPointer<QMJsonValue> &value(const QString &key, const QMPointer<QMJsonValue> &defaultValue) const;
83  template<class T> QMPointer<QMJsonValue> value(const QString &key, const T &defaultValue) const;
84 
85  virtual QList<QString> keys(void) const;
86  virtual QList<QMPointer<QMJsonValue> > values(void) const;
87  virtual QHash<QString, QMPointer<QMJsonValue> > hash(void) const;
88 
89  virtual bool isNull(const QString &key) const;
90  virtual bool isBool(const QString &key) const;
91  virtual bool isDouble(const QString &key) const;
92  virtual bool isString(const QString &key) const;
93  virtual bool isArray(const QString &key) const;
94  virtual bool isObject(const QString &key) const;
95  template<class T> bool is(const QString &key) const;
96 
97  virtual bool toBool(const QString &key) const;
98  virtual double toDouble(const QString &key) const;
99  virtual QString toString(const QString &key) const;
100  virtual const QMPointer<QMJsonArray> &toArray(const QString &key) const;
101  virtual const QMPointer<QMJsonObject> &toObject(const QString &key) const;
102 
103  virtual bool toBool(const QString &key, bool defaultValue) const;
104  virtual double toDouble(const QString &key, double defaultValue) const;
105  virtual const QString &toString(const QString &key, const QString &defaultValue) const;
106  virtual const QMPointer<QMJsonArray> &toArray(const QString &key, const QMPointer<QMJsonArray> &defaultValue) const;
107  virtual const QMPointer<QMJsonObject> &toObject(const QString &key, const QMPointer<QMJsonObject> &defaultValue) const;
108  template<class T> const T &to(const QString &key, const T &defaultValue) const;
109 
110  virtual bool fromBool(const QString &key, bool value);
111  virtual bool fromDouble(const QString &key, double value);
112  virtual bool fromString(const QString &key, const QString &value);
113  virtual bool fromArray(const QString &key, const QMPointer<QMJsonArray> &value);
114  virtual bool fromObject(const QString &key, const QMPointer<QMJsonObject> &value);
115  virtual bool from(const QString &key, const QMPointer<QMJsonValue> &value);
116  template <class T> bool from(const QString &key, const T &value);
117 
118 signals:
119 
120  void itemAdded(const QString &key, const QMPointer<QMJsonValue> &value);
121  void itemRemoved(const QString &key, const QMPointer<QMJsonValue> &value);
122 
123 private:
124 
125  Q_DISABLE_COPY(QMJsonObject);
126 
127  QHash<QString, QMPointer<QMJsonValue> > mHash;
128 };
129 
130 QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMJsonObject &object);
131 QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMPointer<QMJsonObject> &value);
132 
133 // ============================================================================
134 // Class Implementation
135 // ============================================================================
136 
137 template<class T>
138 void QMJsonObject::insert(const QString &key, const T &value, QMJsonReplacementPolicy policy)
139 {
140  this->insert(key, QMPointer<QMJsonValue>(new QMJsonValue(value)), policy);
141 }
142 
143 template<class T>
144 QMPointer<QMJsonValue> QMJsonObject::take(const QString &key, const T &defaultValue)
145 {
146  auto iter = mHash.find(key);
147 
148  if(iter == mHash.end())
149  return QMPointer<QMJsonValue>(new QMJsonValue(defaultValue));
150 
151  this->erase(iter);
152 
153  return iter.value();
154 }
155 
156 template<class T>
157 QMPointer<QMJsonValue> QMJsonObject::value(const QString &key, const T &defaultValue) const
158 {
159  auto iter = mHash.constFind(key);
160 
161  if(iter == mHash.constEnd())
162  return QMPointer<QMJsonValue>(new QMJsonValue(defaultValue));
163 
164  return iter.value();
165 }
166 
167 template<class T>
168 bool QMJsonObject::is(const QString &key) const
169 {
170  auto iter = mHash.constFind(key);
171 
172  if(iter == mHash.constEnd())
173  return false;
174 
175  return iter.value()->is<T>();
176 }
177 
178 template<class T>
179 const T &QMJsonObject::to(const QString &key, const T &defaultValue) const
180 {
181  return this->value(key)->to<T>(defaultValue);
182 }
183 
184 template<class T>
185 bool QMJsonObject::from(const QString &key, const T &value)
186 {
187  return this->value(key)->from<T>(value);
188 }
189 
190 #endif // QMJSONOBJECT_H
191 
virtual const QMPointer< QMJsonValue > & value(const QString &key) const
#define QM_JSON_EXPORT
Definition: qmjsontype.h:36
QMJsonReplacementPolicy
Definition: qmjsontype.h:56
QMJsonArrayUnitePolicy
Definition: qmjsontype.h:75
const T & to(const QString &key, const T &defaultValue) const
Definition: qmjsonobject.h:179
bool is(const QString &key) const
Definition: qmjsonobject.h:168
virtual void insert(const QString &key, const QMPointer< QMJsonValue > &value, QMJsonReplacementPolicy policy=QMJsonReplacementPolicy_Replace)
virtual QMPointer< QMJsonValue > take(const QString &key)
QDebug QM_JSON_EXPORT operator<<(QDebug dbg, const QMJsonObject &object)
virtual const QHash< QString, QMPointer< QMJsonValue > >::iterator erase(const QHash< QString, QMPointer< QMJsonValue > >::iterator &iter)
virtual bool from(const QString &key, const QMPointer< QMJsonValue > &value)