1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QJSON_P_H
42#define QJSON_P_H
43
44//
45// W A R N I N G
46// -------------
47//
48// This file is not part of the Qt API. It exists purely as an
49// implementation detail. This header file may change from version to
50// version without notice, or even be removed.
51//
52// We mean it.
53//
54
55#include <qjsonvalue.h>
56#include <qcborvalue.h>
57#include <private/qcborvalue_p.h>
58
59QT_BEGIN_NAMESPACE
60
61namespace QJsonPrivate {
62
63template<typename Element, typename ElementsIterator>
64struct ObjectIterator
65{
66 using pointer = Element *;
67
68 struct value_type;
69 struct reference {
70 reference(Element &ref) : m_key(&ref) {}
71
72 reference() = delete;
73 ~reference() = default;
74
75 reference(const reference &other) = default;
76 reference(reference &&other) = default;
77
78 reference &operator=(const value_type &value);
79 reference &operator=(const reference &other)
80 {
81 if (m_key != other.m_key) {
82 key() = other.key();
83 value() = other.value();
84 }
85 return *this;
86 }
87
88 reference &operator=(reference &&other)
89 {
90 key() = other.key();
91 value() = other.value();
92 return *this;
93 }
94
95 Element &key() { return *m_key; }
96 Element &value() { return *(m_key + 1); }
97
98 const Element &key() const { return *m_key; }
99 const Element &value() const { return *(m_key + 1); }
100
101
102 private:
103 Element *m_key;
104 };
105
106 struct value_type {
107 value_type(reference ref) : m_key(ref.key()), m_value(ref.value()) {}
108
109 Element key() const { return m_key; }
110 Element value() const { return m_value; }
111 private:
112 Element m_key;
113 Element m_value;
114 };
115
116 using difference_type = typename QVector<Element>::difference_type;
117 using iterator_category = std::random_access_iterator_tag;
118
119 ObjectIterator() = default;
120 ObjectIterator(ElementsIterator it) : it(it) {}
121 ElementsIterator elementsIterator() { return it; }
122
123 ObjectIterator operator++(int) { ObjectIterator ret(it); it += 2; return ret; }
124 ObjectIterator &operator++() { it += 2; return *this; }
125 ObjectIterator &operator+=(difference_type n) { it += 2 * n; return *this; }
126
127 ObjectIterator operator--(int) { ObjectIterator ret(it); it -= 2; return ret; }
128 ObjectIterator &operator--() { it -= 2; return *this; }
129 ObjectIterator &operator-=(difference_type n) { it -= 2 * n; return *this; }
130
131 reference operator*() const { return *it; }
132 reference operator[](int n) const { return it[n * 2]; }
133
134 bool operator<(ObjectIterator other) const { return it < other.it; }
135 bool operator>(ObjectIterator other) const { return it > other.it; }
136 bool operator<=(ObjectIterator other) const { return it <= other.it; }
137 bool operator>=(ObjectIterator other) const { return it >= other.it; }
138
139private:
140 ElementsIterator it;
141};
142
143template<typename Element, typename ElementsIterator>
144inline ObjectIterator<Element, ElementsIterator> operator+(
145 ObjectIterator<Element, ElementsIterator> a,
146 typename ObjectIterator<Element, ElementsIterator>::difference_type n)
147{
148 return {a.elementsIterator() + 2 * n};
149}
150template<typename Element, typename ElementsIterator>
151inline ObjectIterator<Element, ElementsIterator> operator+(
152 int n, ObjectIterator<Element, ElementsIterator> a)
153{
154 return {a.elementsIterator() + 2 * n};
155}
156template<typename Element, typename ElementsIterator>
157inline ObjectIterator<Element, ElementsIterator> operator-(
158 ObjectIterator<Element, ElementsIterator> a,
159 typename ObjectIterator<Element, ElementsIterator>::difference_type n)
160{
161 return {a.elementsIterator() - 2 * n};
162}
163template<typename Element, typename ElementsIterator>
164inline int operator-(
165 ObjectIterator<Element, ElementsIterator> a,
166 ObjectIterator<Element, ElementsIterator> b)
167{
168 return (a.elementsIterator() - b.elementsIterator()) / 2;
169}
170template<typename Element, typename ElementsIterator>
171inline bool operator!=(
172 ObjectIterator<Element, ElementsIterator> a,
173 ObjectIterator<Element, ElementsIterator> b)
174{
175 return a.elementsIterator() != b.elementsIterator();
176}
177template<typename Element, typename ElementsIterator>
178inline bool operator==(
179 ObjectIterator<Element, ElementsIterator> a,
180 ObjectIterator<Element, ElementsIterator> b)
181{
182 return a.elementsIterator() == b.elementsIterator();
183}
184
185using KeyIterator = ObjectIterator<QtCbor::Element, QVector<QtCbor::Element>::iterator>;
186using ConstKeyIterator = ObjectIterator<const QtCbor::Element, QVector<QtCbor::Element>::const_iterator>;
187
188template<>
189inline KeyIterator::reference &KeyIterator::reference::operator=(const KeyIterator::value_type &value)
190{
191 *m_key = value.key();
192 *(m_key + 1) = value.value();
193 return *this;
194}
195
196inline void swap(KeyIterator::reference a, KeyIterator::reference b)
197{
198 KeyIterator::value_type t = a;
199 a = b;
200 b = t;
201}
202
203class Value
204{
205public:
206 static QCborContainerPrivate *container(const QCborValue &v) { return v.container; }
207
208 static QJsonValue fromTrustedCbor(const QCborValue &v)
209 {
210 QJsonValue result;
211 result.d = v.container;
212 result.n = v.n;
213 result.t = v.t;
214 return result;
215 }
216};
217
218class Variant
219{
220public:
221 static QJsonObject toJsonObject(const QVariantMap &map);
222 static QJsonArray toJsonArray(const QVariantList &list);
223};
224
225} // namespace QJsonPrivate
226
227QT_END_NAMESPACE
228
229#endif // QJSON_P_H
230

source code of qtbase/src/corelib/serialization/qjson_p.h