1 /* This file is part of QJson
2  *
3  * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License version 2.1, as published by the Free Software Foundation.
8  *
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef QJSON_PARSER_H
22 #define QJSON_PARSER_H
23 
24 #include "qjson_export.h"
25 
26 QT_BEGIN_NAMESPACE
27 class QIODevice;
28 class QVariant;
29 QT_END_NAMESPACE
30 
31 /**
32  * Namespace used by QJson
33  */
34 namespace QJson {
35 
36   class ParserPrivate;
37 
38   /**
39    * @brief Main class used to convert JSON data to QVariant objects
40    */
41   class QJSON_EXPORT Parser
42   {
43     public:
44       Parser();
45       ~Parser();
46 
47       /**
48       * Read JSON string from the I/O Device and converts it to a QVariant object
49       * @param io Input output device
50       * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
51       * @returns a QVariant object generated from the JSON string
52       */
53       QVariant parse(QIODevice* io, bool* ok = 0);
54 
55       /**
56       * This is a method provided for convenience.
57       * @param jsonData data containing the JSON object representation
58       * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
59       * @returns a QVariant object generated from the JSON string
60       * @sa errorString
61       * @sa errorLine
62       */
63       QVariant parse(const QByteArray& jsonData, bool* ok = 0);
64 
65       /**
66       * This method returns the error message
67       * @returns a QString object containing the error message of the last parse operation
68       * @sa errorLine
69       */
70       QString errorString() const;
71 
72       /**
73       * This method returns line number where the error occurred
74       * @returns the line number where the error occurred
75       * @sa errorString
76       */
77       int errorLine() const;
78 
79       /**
80        * Sets whether special numbers (Infinity, -Infinity, NaN) are allowed as an extension to
81        * the standard
82        * @param  allowSpecialNumbers new value of whether special numbers are allowed
83        * @sa specialNumbersAllowed
84        */
85       void allowSpecialNumbers(bool allowSpecialNumbers);
86 
87       /**
88        * @returns whether special numbers (Infinity, -Infinity, NaN) are allowed
89        * @sa allowSpecialNumbers
90        */
91       bool specialNumbersAllowed() const;
92 
93     private:
94       Q_DISABLE_COPY(Parser)
95       ParserPrivate* const d;
96   };
97 }
98 
99 #endif // QJSON_PARSER_H
100