json.h
Go to the documentation of this file.
1 /* Copyright 2011 Eeli Reilin. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this list of conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 * EVENT SHALL EELI REILIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
19 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
22 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation
25 * are those of the authors and should not be interpreted as representing
26 * official policies, either expressed or implied, of Eeli Reilin.
27 */
28 
33 #ifndef JSON_H
34 #define JSON_H
35 
36 #include <QVariant>
37 #include <QString>
38 
39 namespace QtJson
40 {
41 
45 enum JsonToken
46 {
47  JsonTokenNone = 0,
48  JsonTokenCurlyOpen = 1,
49  JsonTokenCurlyClose = 2,
50  JsonTokenSquaredOpen = 3,
51  JsonTokenSquaredClose = 4,
52  JsonTokenColon = 5,
53  JsonTokenComma = 6,
54  JsonTokenString = 7,
55  JsonTokenNumber = 8,
56  JsonTokenTrue = 9,
57  JsonTokenFalse = 10,
58  JsonTokenNull = 11
59 };
60 
67 class Json
68 {
69  public:
75  static QVariant parse(const QString &json);
76 
83  static QVariant parse(const QString &json, bool &success);
84 
91  static QByteArray serialize(const QVariant &data);
92 
101  static QByteArray serialize(const QVariant &data, bool &success);
102 
103  private:
113  static QVariant parseValue(const QString &json, int &index,
114  bool &success);
115 
125  static QVariant parseObject(const QString &json, int &index,
126  bool &success);
127 
137  static QVariant parseArray(const QString &json, int &index,
138  bool &success);
139 
149  static QVariant parseString(const QString &json, int &index,
150  bool &success);
151 
160  static QVariant parseNumber(const QString &json, int &index);
161 
170  static int lastIndexOfNumber(const QString &json, int index);
171 
178  static void eatWhitespace(const QString &json, int &index);
179 
188  static int lookAhead(const QString &json, int index);
189 
198  static int nextToken(const QString &json, int &index);
199 };
200 
201 
202 } //end namespace
203 
204 #endif //JSON_H
static QVariant parse(const QString &json)
Definition: json.cpp:69
A JSON data parser.
Definition: json.h:67
static QByteArray serialize(const QVariant &data)
Definition: json.cpp:102
Definition: json.cpp:36