1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
31 **************************************************************************/
33 #ifndef SYMBIANUTILS_JSON_H
34 #define SYMBIANUTILS_JSON_H
36 #include "symbianutils_global.h"
38 #include <QtCore/QByteArray>
39 #include <QtCore/QStringList>
40 #include <QtCore/QVector>
44 class SYMBIANUTILS_EXPORT JsonValue
47 JsonValue() : m_type(Invalid) {}
48 explicit JsonValue(const QByteArray &str) { fromString(str); }
52 QList<JsonValue> m_children;
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; }
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; }
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(); }
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;
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);
89 QVariant toVariant() const;
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);
101 void dumpChildren(QByteArray *str, bool multiline, int indent) const;
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.
109 class SYMBIANUTILS_EXPORT JsonInputStream {
111 explicit JsonInputStream(QByteArray &a) : m_target(a) {}
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; }
119 JsonInputStream &operator<<(const QStringList &c);
122 JsonInputStream &operator<<(const QVector<QByteArray> &ba);
124 JsonInputStream &operator<<(bool b);
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; }
134 void appendString(const QString &);
135 void appendCString(const char *c);
137 QByteArray &m_target;
142 #endif // SYMBIANUTILS_JSON_H