summaryrefslogtreecommitdiffstats
path: root/src/qtjsonschema/jsonobjecttypes_impl_p.h
blob: dfa8918db129da0cc3ca0596cc98e655249fcaf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef JSONOBJECTTYPES_IMPL_P_H
#define JSONOBJECTTYPES_IMPL_P_H

#include "jsonobjecttypes_p.h"
#include "schemamanager_p.h"

#include "jsonschema-global.h"
#include "schemaerror.h"

QT_BEGIN_NAMESPACE_JSONSTREAM

inline JsonObjectTypes::ValueList::ValueList(const QJsonArray &list) : QJsonArray(list)
{}

inline uint JsonObjectTypes::ValueList::count() const
{
    return QJsonArray::size();
}

inline JsonObjectTypes::ValueList::const_iterator JsonObjectTypes::ValueList::constBegin() const
{
    return const_iterator(0, this);
}

inline JsonObjectTypes::ValueList::const_iterator JsonObjectTypes::ValueList::constEnd() const
{
    return const_iterator(size(), this);
}

inline JsonObjectTypes::ValueList::const_iterator::const_iterator()
    : m_index(-1)
    , m_list(0)
{}

inline JsonObjectTypes::Value JsonObjectTypes::ValueList::const_iterator::operator *() const
{
    Q_ASSERT(isValid());
    return Value(m_index, *m_list);
}

inline bool JsonObjectTypes::ValueList::const_iterator::operator !=(const const_iterator &other) const
{
    return m_index != other.m_index || m_list != other.m_list;
}

inline JsonObjectTypes::ValueList::const_iterator& JsonObjectTypes::ValueList::const_iterator::operator ++()
{
    m_index++;
    return *this;
}

inline JsonObjectTypes::ValueList::const_iterator::const_iterator(int begin, const QJsonArray *list)
    : m_index(begin)
    , m_list(list)
{}

inline bool JsonObjectTypes::ValueList::const_iterator::isValid() const
{
    return m_index != -1 && m_index < m_list->size();
}

inline JsonObjectTypes::Value::Value(Key propertyName, const QJsonObject &map)
    : m_index(-1)
    , m_property(propertyName)
    , m_value(map)
    , m_type(propertyName.isEmpty() ? RootMap : Map)
{}

inline JsonObjectTypes::Value::Value(const int index, const QJsonArray &list)
    : m_index(index)
    , m_value(list)
    , m_type(List)
{}

inline int JsonObjectTypes::Value::toInt(bool *ok) const
{
    if (m_intCache.isValid()) {
        *ok = true;
        return m_intCache.value();
    }
    int result = 0;
    QJsonValue::Type type;
    switch (m_type) {
    case Map:
        type = typeMap();
        if (type == QJsonValue::Double) {
            QJsonValue v = map().value(m_property);
            double doubleResult = v.toDouble();
            int intResult = (int)doubleResult;
            if ((double)intResult == doubleResult) {
                *ok = true;
                result = intResult;
            } else {
                *ok = false;
            }
        } else {
            *ok = false;
        }
        m_intCache.set(*ok, result);
        return result;
    case List:
        type = typeList();
        if (type == QJsonValue::Double) {
            QJsonValue v = list().at(m_index);
            double doubleResult = v.toDouble();
            int intResult = (int)doubleResult;
            if ((double)intResult == doubleResult) {
                *ok = true;
                result = intResult;
            } else {
                *ok = false;
            }
        } else {
            *ok = false;
        }
        m_intCache.set(*ok, result);
        return result;
    case RootMap:
        break;
    default:
        Q_ASSERT(false);
    }
    *ok = false;
    return -1;
}

inline double JsonObjectTypes::Value::toDouble(bool *ok) const
{
    if (m_doubleCache.isValid()) {
        *ok = true;
        return m_doubleCache.value();
    }
    double result;
    QJsonValue::Type type;
    switch (m_type) {
    case Map:
        type = typeMap();
        *ok = type == QJsonValue::Double;
        result = map().value(m_property).toDouble();
        m_doubleCache.set(*ok, result);
        return result;
    case List:
        type = typeList();
        *ok = type == QJsonValue::Double;
        result = list().at(m_index).toDouble();
        m_doubleCache.set(*ok, result);
        return result;
    case RootMap:
        break;
    default:
        Q_ASSERT(false);
    }
    *ok = false;
    return -1;
}

inline JsonObjectTypes::ValueList JsonObjectTypes::Value::toList(bool *ok) const
{
    *ok = true;
    switch (m_type) {
    case Map:
        *ok = typeMap() == QJsonValue::Array;
        return map().value(m_property).toArray();
    case List:
        *ok = typeList() == QJsonValue::Array;
        return list().at(m_index).toArray();
    case RootMap:
        return m_value.toArray();
    default:
        Q_ASSERT(false);
    }
    *ok = false;
    return ValueList(QJsonArray());
}

inline QString JsonObjectTypes::Value::toString(bool *ok) const
{
    switch (m_type) {
    case Map:
        *ok = typeMap() == QJsonValue::String;
        return map().value(m_property).toString();
    case List:
        *ok = typeList() == QJsonValue::String;
        return list().at(m_index).toString();
    case RootMap:
        *ok = true;
        return QString(); // useful for debugging
    default:
        Q_ASSERT(false);
    }
    *ok = false;
    return QString();
}

inline bool JsonObjectTypes::Value::toBoolean(bool *ok) const
{
    switch (m_type) {
    case Map:
        *ok = typeMap() == QJsonValue::Bool;
        return map().value(m_property).toBool();
    case List:
        *ok = typeList() == QJsonValue::Bool;
        return list().at(m_index).toBool();
    case RootMap:
    default:
        Q_ASSERT(false);
    }
    *ok = false;
    return false;
}

inline void JsonObjectTypes::Value::toNull(bool *ok) const
{
    switch (m_type) {
    case Map:
        *ok = typeMap() == QJsonValue::Null;
    case List:
        *ok = typeList() == QJsonValue::Null;
    case RootMap:
    default:
        Q_ASSERT(false);
    }
    *ok = false;
}

inline JsonObjectTypes::Object JsonObjectTypes::Value::toObject(bool *ok) const
{
    switch (m_type) {
    case Map:
        *ok = typeMap() == QJsonValue::Object;
        return map().value(m_property).toObject();
    case List:
        *ok = typeList() == QJsonValue::Object;
        return list().at(m_index).toObject();
    case RootMap:
        *ok =  true;
        return static_cast<Object>(m_value.toObject());
    default:
        Q_ASSERT(false);
    }
    *ok = false;
    return Object(QJsonObject());
}

inline bool JsonObjectTypes::Value::compare(const Value & val) const
{
    QJsonValue v0(m_type == Map ? map().value(m_property) : (m_type == List ? list().at(m_index) : QJsonValue()));
    QJsonValue v1(val.m_type == Map ? val.map().value(val.m_property) : (val.m_type == List ? val.list().at(val.m_index) : QJsonValue()));
    return v0 == v1;
}

inline QJsonValue JsonObjectTypes::Value::value() const
{
    switch (m_type) {
    case Map:
        return map().value(m_property);
    case List:
        return list().at(m_index);
    case RootMap:
        return m_value;
    default:
        Q_ASSERT(false);
    }
    return QJsonValue();
}

inline const QJsonObject JsonObjectTypes::Value::map() const
{
    Q_ASSERT(m_type == Map);
    Q_ASSERT(!m_property.isEmpty());
    return m_value.toObject();
}

inline const QJsonArray JsonObjectTypes::Value::list() const
{
    Q_ASSERT(m_type == List);
    Q_ASSERT(m_index >= 0);
    return m_value.toArray();
}

inline QJsonValue::Type JsonObjectTypes::Value::typeMap() const
{
    if (m_jsonTypeCache.isValid())
        return m_jsonTypeCache.value();
    QJsonValue::Type result = map().value(m_property).type();
    m_jsonTypeCache.set(true, result);
    return result;
}

inline QJsonValue::Type JsonObjectTypes::Value::typeList() const
{
    if (m_jsonTypeCache.isValid())
        return m_jsonTypeCache.value();
    QJsonValue::Type result = list().at(m_index).type();
    m_jsonTypeCache.set(true, result);
    return result;
}

inline JsonObjectTypes::Object::Object()
{}

inline JsonObjectTypes::Object::Object(const QJsonObject &object)
    : QJsonObject(object)
{}

inline JsonObjectTypes::Value JsonObjectTypes::Object::property(const JsonObjectTypes::Key& name) const
{
    return Value(name, *this);
}

inline QList<JsonObjectTypes::Key> JsonObjectTypes::Object::propertyNames() const { return keys(); }

inline JsonObjectTypes::Service::Service(SchemaManagerBase *schemas)
    : m_schemas(schemas)
{}

inline QJsonObject JsonObjectTypes::Service::error() const
{
    return m_errorMap;
}

inline void JsonObjectTypes::Service::setError(const QString &message)
{
    m_errorMap.insert(SchemaError::kCodeStr, SchemaError::FailedSchemaValidation);
    m_errorMap.insert(SchemaError::kMessageStr, message);
}

inline SchemaValidation::Schema<JsonObjectTypes> JsonObjectTypes::Service::loadSchema(const QString &schemaName)
{
    return reinterpret_cast< SchemaManager<QJsonObject, JsonObjectTypes> *>(m_schemas)->schema(schemaName, this);
}

QT_END_NAMESPACE_JSONSTREAM

#endif // JSONOBJECTTYPES_IMPL_P_H