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.
49
/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
56
typedef const Char *Location;
58
/** \brief Constructs a Reader allowing all features
63
/** \brief Constructs a Reader allowing the specified feature set
66
Reader( const Features &features );
68
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
69
* \param document UTF-8 encoded string containing the document to read.
70
* \param root [out] Contains the root value of the document if it was
71
* successfully parsed.
72
* \param collectComments \c true to collect comment and allow writing them back during
73
* serialization, \c false to discard comments.
74
* This parameter is ignored if Features::allowComments_
76
* \return \c true if the document was successfully parsed, \c false if an error occurred.
78
bool parse( const std::string &document,
80
bool collectComments = true );
82
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
83
* \param document UTF-8 encoded string containing the document to read.
84
* \param root [out] Contains the root value of the document if it was
85
* successfully parsed.
86
* \param collectComments \c true to collect comment and allow writing them back during
87
* serialization, \c false to discard comments.
88
* This parameter is ignored if Features::allowComments_
90
* \return \c true if the document was successfully parsed, \c false if an error occurred.
92
bool parse( const char *beginDoc, const char *endDoc,
94
bool collectComments = true );
96
/// \brief Parse from input stream.
97
/// \see Json::operator>>(std::istream&, Json::Value&).
98
bool parse( std::istream &is,
100
bool collectComments = true );
102
/** \brief Returns a user friendly string that list errors in the parsed document.
103
* \return Formatted error message with the list of errors with their location in
104
* the parsed document. An empty string is returned if no error occurred
107
std::string getFormatedErrorMessages() const;
112
tokenEndOfStream = 0,
123
tokenMemberSeparator,
140
std::string message_;
144
typedef std::deque<ErrorInfo> Errors;
146
bool expectToken( TokenType type, Token &token, const char *message );
147
bool readToken( Token &token );
149
bool match( Location pattern,
152
bool readCStyleComment();
153
bool readCppStyleComment();
157
bool readObject( Token &token );
158
bool readArray( Token &token );
159
bool decodeNumber( Token &token );
160
bool decodeString( Token &token );
161
bool decodeString( Token &token, std::string &decoded );
162
bool decodeDouble( Token &token );
163
bool decodeUnicodeCodePoint( Token &token,
166
unsigned int &unicode );
167
bool decodeUnicodeEscapeSequence( Token &token,
170
unsigned int &unicode );
171
bool addError( const std::string &message,
173
Location extra = 0 );
174
bool recoverFromError( TokenType skipUntilToken );
175
bool addErrorAndRecover( const std::string &message,
177
TokenType skipUntilToken );
178
void skipUntilSpace();
179
Value ¤tValue();
181
void getLocationLineAndColumn( Location location,
184
std::string getLocationLineAndColumn( Location location ) const;
185
void addComment( Location begin,
187
CommentPlacement placement );
188
void skipCommentTokens( Token &token );
190
typedef std::stack<Value *> Nodes;
193
std::string document_;
197
Location lastValueEnd_;
199
std::string commentsBefore_;
201
bool collectComments_;
204
/** \brief Read from 'sin' into 'root'.
206
Always keep comments from the input JSON.
208
This can be used to read a file into a particular sub-object.
212
cin >> root["dir"]["file"];
220
// The input stream JSON would be nested here.
225
\throw std::exception on parse error.
226
\see Json::operator<<()
228
std::istream& operator>>( std::istream&, Value& );