OSDN Git Service

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