1
/****************************************************************************************
2
* Copyright (c) 2008 Frederick Emmott <mail@fredemmott.co.uk> *
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 *
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. *
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
****************************************************************************************/
17
#ifndef JSONQT_JSON_TO_VARIANT_H
18
#define JSONQT_JSON_TO_VARIANT_H
20
#include "ParseException.h"
22
#include "JsonQtExport.h"
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.
34
* @author Fred Emmott <mail@fredemmott.co.uk>
38
/** Class for converting JSON strings to QVariant-based structures.
40
* @author Fred Emmott <mail@fredemmott.co.uk>
42
class JSONQT_EXPORT JsonToVariant
45
/** Main parsing function.
47
* @param json is a string containing JSON text.
48
* @returns A QVariant-representation of the JSON
50
* @throws ParseException if the string provided is not
51
* valid JSON (or at least this parser thinks it
54
static QVariant parse(const QString& json) throw (ParseException);
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.
60
static QList<QVariantMap> multiParse(const QString& json) throw(ParseException);
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();
73
QVariant parseNumber();
77
QString parseDigits();
80
// Parsers for types implied on JSON.org
82
QChar parseHexDigit();
87
/// The unparsed part of the input string.
88
inline QString remaining();
90
/** Consume the next character.
91
* Advances m_sym to the next character, and returns it.
92
* Optionally skips over whitespace.
94
* @param skipWhitespace controls if whitespace is
97
* @throws ParseException if the end of the string is
100
QChar consume(bool skipWhitespace = true)
101
throw(ParseException);
103
/** Consume the next character, and check for equality
104
* with the specified character.
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.
110
void consume(QChar wanted) throw(ParseException);
112
/// Convenience function for consume(QChar).
113
void consume(char wanted) throw(ParseException);
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
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.
125
void consume(QString wanted) throw(ParseException);
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.
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.
137
bool tryConsume(QChar wanted) throw();
139
/** Return the next symbol.
140
* Optionally skips whitespace.
142
* @param skipWhitespace sets the whitespace handling.
143
* @returns the next symbol.
144
* @throws ParseException if the end of the string is
147
QChar peekNext(bool skipWhitespace = true)
148
throw(ParseException);
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;