~ubuntu-branches/ubuntu/maverick/amarok/maverick-backports

« back to all changes in this revision

Viewing changes to src/context/engines/songkick/JsonQt/lib/JsonToVariant.h

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2011-03-03 10:20:39 UTC
  • mfrom: (126.1.8 natty)
  • Revision ID: james.westby@ubuntu.com-20110303102039-a408rug513n4qbin
Tags: 2:2.4.0-0ubuntu4~maverick1
* Source backprt to maverick (LP: #728447)
  - Drop version requirement on libindicate-qt-dev build-dep

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************************
2
 
 * Copyright (c) 2008 Frederick Emmott <mail@fredemmott.co.uk>                          *
3
 
 *                                                                                      *
4
 
 * This program is free software; you can redistribute it and/or modify it under        *
5
 
 * the terms of the GNU General Public License as published by the Free Software        *
6
 
 * Foundation; either version 2 of the License, or (at your option) any later           *
7
 
 * version.                                                                             *
8
 
 *                                                                                      *
9
 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11
 
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12
 
 *                                                                                      *
13
 
 * You should have received a copy of the GNU General Public License along with         *
14
 
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15
 
 ****************************************************************************************/
16
 
 
17
 
#ifndef JSONQT_JSON_TO_VARIANT_H
18
 
#define JSONQT_JSON_TO_VARIANT_H
19
 
 
20
 
#include "ParseException.h"
21
 
 
22
 
#include "JsonQtExport.h"
23
 
 
24
 
#include <QList>
25
 
#include <QPair>
26
 
#include <QString>
27
 
#include <QVariant>
28
 
 
29
 
/** Qt-based JSON handling.
30
 
 * This is based on a recursive descent parser with 1-character lookahed,
31
 
 * and follows the structure of the grammar presented on json.org as closely
32
 
 * as possible, to avoid mistakes, and hopefully to make the code more readable.
33
 
 *
34
 
 * @author Fred Emmott <mail@fredemmott.co.uk>
35
 
 */
36
 
namespace JsonQt
37
 
{
38
 
        /** Class for converting JSON strings to QVariant-based structures.
39
 
         *
40
 
         * @author Fred Emmott <mail@fredemmott.co.uk>
41
 
         */
42
 
        class JSONQT_EXPORT JsonToVariant
43
 
        {
44
 
                public:
45
 
                        /** Main parsing function.
46
 
                         *
47
 
                         * @param json is a string containing JSON text.
48
 
                         * @returns A QVariant-representation of the JSON
49
 
                         *      structure.
50
 
                         * @throws ParseException if the string provided is not
51
 
                         *      valid JSON (or at least this parser thinks it
52
 
                         *      isn't ;) )
53
 
                         */
54
 
                        static QVariant parse(const QString& json) throw (ParseException);
55
 
 
56
 
                        /** Parse multiple objects in one string.
57
 
                         * This is useful when working on streams where
58
 
                         * one-chunk-per-json-object is not guaranteed.
59
 
                         */
60
 
                        static QList<QVariantMap> multiParse(const QString& json) throw(ParseException);
61
 
                private:
62
 
                        JsonToVariant();
63
 
                        // Parsers for types given on JSON.org
64
 
                        QVariantMap parseObject();
65
 
                        QVariantMap parseMembers();
66
 
                        QPair<QString, QVariant> parsePair();
67
 
                        QVariantList parseArray();
68
 
                        QVariantList parseElements();
69
 
                        QVariant parseValue();
70
 
                        QString parseString();
71
 
                        QString parseChars();
72
 
                        QChar parseChar();
73
 
                        QVariant parseNumber();
74
 
                        QString parseInt();
75
 
                        QString parseFrac();
76
 
                        QString parseExp();
77
 
                        QString parseDigits();
78
 
                        QString parseE();
79
 
 
80
 
                        // Parsers for types implied on JSON.org
81
 
                        QChar parseDigit();
82
 
                        QChar parseHexDigit();
83
 
                        bool parseBool();
84
 
                        QVariant parseNull();
85
 
 
86
 
                        // Internal functions
87
 
                        /// The unparsed part of the input string.
88
 
                        inline QString remaining();
89
 
 
90
 
                        /** Consume the next character.
91
 
                         * Advances m_sym to the next character, and returns it.
92
 
                         * Optionally skips over whitespace.
93
 
                         *
94
 
                         * @param skipWhitespace controls if whitespace is
95
 
                         *      ignored.
96
 
                         * @returns *m_sym
97
 
                         * @throws ParseException if the end of the string is
98
 
                         *      reached.
99
 
                         */
100
 
                        QChar consume(bool skipWhitespace = true)
101
 
                                throw(ParseException);
102
 
 
103
 
                        /** Consume the next character, and check for equality
104
 
                         * with the specified character.
105
 
                         *
106
 
                         * @param wanted is the character to compare to.
107
 
                         * @throws ParseException if the end of the string is
108
 
                         *      reached, or the characaters are not equal.
109
 
                         */
110
 
                        void consume(QChar wanted) throw(ParseException);
111
 
 
112
 
                        /// Convenience function for consume(QChar).
113
 
                        void consume(char wanted) throw(ParseException);
114
 
 
115
 
                        /** Attempt to consume the specified string.
116
 
                         * This attempts to consume length(wanted) characters,
117
 
                         * including whitespace, and checks that they are equal
118
 
                         * to the characters in the same position in the
119
 
                         * specified string.
120
 
                         *
121
 
                         * @param wanted is the string to attempt to consume.
122
 
                         * @throws ParseException if the end of the string is
123
 
                         *      reached, or the string comparisson fails.
124
 
                         */
125
 
                        void consume(QString wanted) throw(ParseException);
126
 
 
127
 
                        /** Try to consume a single character, without throw.
128
 
                         * This will try to read a single character, and
129
 
                         * compares to the specified character.
130
 
                         *
131
 
                         * @param wanted is the character to compare to.
132
 
                         * @returns true if the character specified was
133
 
                         *      successfully consumed.
134
 
                         * @returns false if the end of the string was reached,
135
 
                         *      or the characters were not equal.
136
 
                         */
137
 
                        bool tryConsume(QChar wanted) throw();
138
 
 
139
 
                        /** Return the next symbol.
140
 
                         * Optionally skips whitespace.
141
 
                         * 
142
 
                         * @param skipWhitespace sets the whitespace handling.
143
 
                         * @returns the next symbol.
144
 
                         * @throws ParseException if the end of the string is
145
 
                         *      reached.
146
 
                         */
147
 
                        QChar peekNext(bool skipWhitespace = true)
148
 
                                throw(ParseException);
149
 
 
150
 
                        // Variables
151
 
 
152
 
                        /// Iterator pointing at the current symbol.
153
 
                        QString::ConstIterator m_sym;
154
 
                        /// Iterator pointing at the next symbol.
155
 
                        QString::ConstIterator m_next;
156
 
                        /// Iterator pointing at the end of the string.
157
 
                        QString::ConstIterator m_end;
158
 
        };
159
 
}
160
 
 
161
 
#endif