source: trunk/tools/runonphone/symbianutils/json.h@ 1147

Last change on this file since 1147 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef SYMBIANUTILS_JSON_H
43#define SYMBIANUTILS_JSON_H
44
45#include "symbianutils_global.h"
46
47#include <QtCore/QByteArray>
48#include <QtCore/QStringList>
49#include <QtCore/QVector>
50
51namespace tcftrk {
52
53class SYMBIANUTILS_EXPORT JsonValue
54{
55public:
56 JsonValue() : m_type(Invalid) {}
57 explicit JsonValue(const QByteArray &str) { fromString(str); }
58
59 QByteArray m_name;
60 QByteArray m_data;
61 QList<JsonValue> m_children;
62
63 enum Type {
64 Invalid,
65 String,
66 Number,
67 Boolean,
68 Object,
69 NullObject,
70 Array,
71 };
72
73 Type m_type;
74
75 inline Type type() const { return m_type; }
76 inline QByteArray name() const { return m_name; }
77 inline bool hasName(const char *name) const { return m_name == name; }
78
79 inline bool isValid() const { return m_type != Invalid; }
80 inline bool isNumber() const { return m_type == Number; }
81 inline bool isString() const { return m_type == String; }
82 inline bool isObject() const { return m_type == Object; }
83 inline bool isArray() const { return m_type == Array; }
84
85
86 inline QByteArray data() const { return m_data; }
87 inline const QList<JsonValue> &children() const { return m_children; }
88 inline int childCount() const { return m_children.size(); }
89
90 const JsonValue &childAt(int index) const { return m_children[index]; }
91 JsonValue &childAt(int index) { return m_children[index]; }
92 JsonValue findChild(const char *name) const;
93
94 QByteArray toString(bool multiline = false, int indent = 0) const;
95 void fromString(const QByteArray &str);
96 void setStreamOutput(const QByteArray &name, const QByteArray &content);
97
98private:
99 static QByteArray parseCString(const char *&from, const char *to);
100 static QByteArray parseNumber(const char *&from, const char *to);
101 static QByteArray escapeCString(const QByteArray &ba);
102 static QString escapeCString(const QString &ba);
103 void parsePair(const char *&from, const char *to);
104 void parseValue(const char *&from, const char *to);
105 void parseObject(const char *&from, const char *to);
106 void parseArray(const char *&from, const char *to);
107
108 void dumpChildren(QByteArray *str, bool multiline, int indent) const;
109};
110
111/* Thin wrapper around QByteArray for formatting JSON input. Use as in:
112 * JsonInputStream(byteArray) << '{' << "bla" << ':' << "blup" << '}';
113 * Note that strings get double quotes and JSON-escaping, characters should be
114 * used for the array/hash delimiters.
115 * */
116class SYMBIANUTILS_EXPORT JsonInputStream {
117public:
118 explicit JsonInputStream(QByteArray &a) : m_target(a) {}
119
120 JsonInputStream &operator<<(char c) { m_target.append(c); return *this; }
121 JsonInputStream &operator<<(const char *c) { appendCString(c); return *this; }
122 JsonInputStream &operator<<(const QByteArray &a) { appendCString(a.constData()); return *this; }
123 JsonInputStream &operator<<(const QString &c) { appendString(c); return *this; }
124
125 // Format as array
126 JsonInputStream &operator<<(const QStringList &c);
127
128 // Format as array
129 JsonInputStream &operator<<(const QVector<QByteArray> &ba);
130
131 JsonInputStream &operator<<(bool b);
132
133 JsonInputStream &operator<<(int i)
134 { m_target.append(QByteArray::number(i)); return *this; }
135 JsonInputStream &operator<<(unsigned i)
136 { m_target.append(QByteArray::number(i)); return *this; }
137 JsonInputStream &operator<<(quint64 i)
138 { m_target.append(QByteArray::number(i)); return *this; }
139
140private:
141 void appendString(const QString &);
142 void appendCString(const char *c);
143
144 QByteArray &m_target;
145};
146
147} // namespace tcftrk
148
149#endif // SYMBIANUTILS_JSON_H
Note: See TracBrowser for help on using the repository browser.