Cute Chess  0.1
jsonparser.h
1 /*
2  Copyright (c) 2010 Ilari Pihlajisto
3 
4  Permission is hereby granted, free of charge, to any person
5  obtaining a copy of this software and associated documentation
6  files (the "Software"), to deal in the Software without
7  restriction, including without limitation the rights to use,
8  copy, modify, merge, publish, distribute, sublicense, and/or sell
9  copies of the Software, and to permit persons to whom the
10  Software is furnished to do so, subject to the following
11  conditions:
12 
13  The above copyright notice and this permission notice shall be
14  included in all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 #ifndef JSONPARSER_H
27 #define JSONPARSER_H
28 
29 #include <QVariant>
30 #include <QCoreApplication>
31 
32 class QTextStream;
33 
34 
44 class LIB_EXPORT JsonParser
45 {
46  Q_DECLARE_TR_FUNCTIONS(JsonParser)
47 
48  public:
50  JsonParser(QTextStream& stream);
57  QVariant parse();
58 
60  bool hasError() const;
62  QString errorString() const;
64  qint64 errorLineNumber() const;
65 
66  private:
67  enum Token
68  {
69  JsonError,
70  JsonGeneric,
71  JsonNone,
72  JsonComma,
73  JsonColon,
74  JsonBeginObject,
75  JsonEndObject,
76  JsonBeginArray,
77  JsonEndArray,
78  JsonTrue,
79  JsonFalse,
80  JsonNull,
81  JsonNumber,
82  JsonString
83  };
84 
85  static QString tokenString(Token type,
86  const QString& str = QString());
87 
88  Token parseToken();
89  QVariant parseValue(Token* tokenType = 0);
90  QVariant parseObject();
91  QVariant parseArray();
92  QString parseString();
93  void setError(const QString& message);
94  void clearError();
95 
96  bool m_error;
97  qint64 m_currentLine;
98  qint64 m_errorLine;
99  QString m_errorString;
100  QString m_lastToken;
101  QChar m_buffer;
102  QTextStream& m_stream;
103 };
104 
105 #endif // JSONPARSER_H
A JSON (JavaScript Object Notation) parser.
Definition: jsonparser.h:44