1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
* JSON Library, originally from http://jsoncpp.sourceforge.net/
5
* Copyright (C) 2011 Stewart Smith
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
#ifndef CPPTL_JSON_READER_H_INCLUDED
40
# define CPPTL_JSON_READER_H_INCLUDED
42
# include "features.h"
51
/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
58
typedef const Char *Location;
60
/** \brief Constructs a Reader allowing all features
65
/** \brief Constructs a Reader allowing the specified feature set
68
Reader( const Features &features );
70
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
71
* \param document UTF-8 encoded string containing the document to read.
72
* \param root [out] Contains the root value of the document if it was
73
* successfully parsed.
74
* \param collectComments \c true to collect comment and allow writing them back during
75
* serialization, \c false to discard comments.
76
* This parameter is ignored if Features::allowComments_
78
* \return \c true if the document was successfully parsed, \c false if an error occurred.
80
bool parse( const std::string &document,
82
bool collectComments = true );
84
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
85
* \param document UTF-8 encoded string containing the document to read.
86
* \param root [out] Contains the root value of the document if it was
87
* successfully parsed.
88
* \param collectComments \c true to collect comment and allow writing them back during
89
* serialization, \c false to discard comments.
90
* This parameter is ignored if Features::allowComments_
92
* \return \c true if the document was successfully parsed, \c false if an error occurred.
94
bool parse( const char *beginDoc, const char *endDoc,
96
bool collectComments = true );
98
/// \brief Parse from input stream.
99
/// \see Json::operator>>(std::istream&, Json::Value&).
100
bool parse( std::istream &is,
102
bool collectComments = true );
104
/** \brief Returns a user friendly string that list errors in the parsed document.
105
* \return Formatted error message with the list of errors with their location in
106
* the parsed document. An empty string is returned if no error occurred
109
std::string getFormatedErrorMessages() const;
114
tokenEndOfStream = 0,
125
tokenMemberSeparator,
142
std::string message_;
146
typedef std::deque<ErrorInfo> Errors;
148
bool expectToken( TokenType type, Token &token, const char *message );
149
bool readToken( Token &token );
151
bool match( Location pattern,
154
bool readCStyleComment();
155
bool readCppStyleComment();
159
bool readObject( Token &token );
160
bool readArray( Token &token );
161
bool decodeNumber( Token &token );
162
bool decodeString( Token &token );
163
bool decodeString( Token &token, std::string &decoded );
164
bool decodeDouble( Token &token );
165
bool decodeUnicodeCodePoint( Token &token,
168
unsigned int &unicode );
169
bool decodeUnicodeEscapeSequence( Token &token,
172
unsigned int &unicode );
173
bool addError( const std::string &message,
175
Location extra = 0 );
176
bool recoverFromError( TokenType skipUntilToken );
177
bool addErrorAndRecover( const std::string &message,
179
TokenType skipUntilToken );
180
void skipUntilSpace();
181
Value ¤tValue();
183
void getLocationLineAndColumn( Location location,
186
std::string getLocationLineAndColumn( Location location ) const;
187
void addComment( Location begin,
189
CommentPlacement placement );
190
void skipCommentTokens( Token &token );
192
typedef std::stack<Value *> Nodes;
195
std::string document_;
199
Location lastValueEnd_;
201
std::string commentsBefore_;
203
bool collectComments_;
206
/** \brief Read from 'sin' into 'root'.
208
Always keep comments from the input JSON.
210
This can be used to read a file into a particular sub-object.
214
cin >> root["dir"]["file"];
222
// The input stream JSON would be nested here.
227
\throw std::exception on parse error.
228
\see Json::operator<<()
230
std::istream& operator>>( std::istream&, Value& );
234
#endif // CPPTL_JSON_READER_H_INCLUDED