2010-04-20 23:35:19 +02:00
|
|
|
// Copyright 2007-2010 Baptiste Lepilleur
|
|
|
|
// Distributed under MIT license, or public domain if desired and
|
|
|
|
// recognized in your jurisdiction.
|
|
|
|
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
|
|
|
|
2007-06-13 17:51:04 +02:00
|
|
|
#ifndef CPPTL_JSON_READER_H_INCLUDED
|
2014-07-01 00:48:54 +02:00
|
|
|
#define CPPTL_JSON_READER_H_INCLUDED
|
2007-06-13 17:51:04 +02:00
|
|
|
|
2011-05-02 23:09:30 +02:00
|
|
|
#if !defined(JSON_IS_AMALGAMATION)
|
2014-07-01 00:48:54 +02:00
|
|
|
#include "features.h"
|
|
|
|
#include "value.h"
|
2011-05-02 23:09:30 +02:00
|
|
|
#endif // if !defined(JSON_IS_AMALGAMATION)
|
2014-07-01 00:48:54 +02:00
|
|
|
#include <deque>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <stack>
|
|
|
|
#include <string>
|
2015-01-29 21:29:40 +01:00
|
|
|
#include <istream>
|
2007-06-13 17:51:04 +02:00
|
|
|
|
2014-07-01 00:48:54 +02:00
|
|
|
// Disable warning C4251: <data member>: <type> needs to have dll-interface to
|
|
|
|
// be used by...
|
2013-05-08 22:21:11 +02:00
|
|
|
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
2014-07-01 00:48:54 +02:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4251)
|
2013-05-08 22:21:11 +02:00
|
|
|
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
|
|
|
|
2007-06-13 17:51:04 +02:00
|
|
|
namespace Json {
|
|
|
|
|
2014-07-01 00:48:54 +02:00
|
|
|
/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a
|
|
|
|
*Value.
|
|
|
|
*
|
2015-02-09 18:15:39 +01:00
|
|
|
* \deprecated Use CharReader and CharReaderBuilder.
|
2014-07-01 00:48:54 +02:00
|
|
|
*/
|
|
|
|
class JSON_API Reader {
|
|
|
|
public:
|
|
|
|
typedef char Char;
|
2014-09-15 02:15:29 +02:00
|
|
|
typedef const Char* Location;
|
2014-07-01 00:48:54 +02:00
|
|
|
|
|
|
|
/** \brief An error tagged with where in the JSON text it was encountered.
|
|
|
|
*
|
|
|
|
* The offsets give the [start, limit) range of bytes within the text. Note
|
|
|
|
* that this is bytes, not codepoints.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct StructuredError {
|
|
|
|
size_t offset_start;
|
|
|
|
size_t offset_limit;
|
|
|
|
std::string message;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Constructs a Reader allowing all features
|
|
|
|
* for parsing.
|
|
|
|
*/
|
|
|
|
Reader();
|
|
|
|
|
|
|
|
/** \brief Constructs a Reader allowing the specified feature set
|
|
|
|
* for parsing.
|
|
|
|
*/
|
2014-09-15 02:15:29 +02:00
|
|
|
Reader(const Features& features);
|
2014-07-01 00:48:54 +02:00
|
|
|
|
|
|
|
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
|
|
|
|
* document.
|
|
|
|
* \param document UTF-8 encoded string containing the document to read.
|
|
|
|
* \param root [out] Contains the root value of the document if it was
|
|
|
|
* successfully parsed.
|
|
|
|
* \param collectComments \c true to collect comment and allow writing them
|
|
|
|
* back during
|
|
|
|
* serialization, \c false to discard comments.
|
|
|
|
* This parameter is ignored if
|
|
|
|
* Features::allowComments_
|
|
|
|
* is \c false.
|
|
|
|
* \return \c true if the document was successfully parsed, \c false if an
|
|
|
|
* error occurred.
|
2007-06-13 17:51:04 +02:00
|
|
|
*/
|
2014-07-01 00:48:54 +02:00
|
|
|
bool
|
2014-09-15 02:15:29 +02:00
|
|
|
parse(const std::string& document, Value& root, bool collectComments = true);
|
2014-07-01 00:48:54 +02:00
|
|
|
|
|
|
|
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
|
|
|
|
document.
|
|
|
|
* \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the
|
|
|
|
document to read.
|
|
|
|
* \param endDoc Pointer on the end of the UTF-8 encoded string of the
|
|
|
|
document to read.
|
2015-01-29 21:29:40 +01:00
|
|
|
* Must be >= beginDoc.
|
2014-07-01 00:48:54 +02:00
|
|
|
* \param root [out] Contains the root value of the document if it was
|
|
|
|
* successfully parsed.
|
|
|
|
* \param collectComments \c true to collect comment and allow writing them
|
|
|
|
back during
|
|
|
|
* serialization, \c false to discard comments.
|
|
|
|
* This parameter is ignored if
|
|
|
|
Features::allowComments_
|
|
|
|
* is \c false.
|
|
|
|
* \return \c true if the document was successfully parsed, \c false if an
|
|
|
|
error occurred.
|
|
|
|
*/
|
2014-09-15 02:15:29 +02:00
|
|
|
bool parse(const char* beginDoc,
|
|
|
|
const char* endDoc,
|
|
|
|
Value& root,
|
2014-07-01 00:48:54 +02:00
|
|
|
bool collectComments = true);
|
|
|
|
|
|
|
|
/// \brief Parse from input stream.
|
|
|
|
/// \see Json::operator>>(std::istream&, Json::Value&).
|
2014-09-15 02:15:29 +02:00
|
|
|
bool parse(std::istream& is, Value& root, bool collectComments = true);
|
2014-07-01 00:48:54 +02:00
|
|
|
|
|
|
|
/** \brief Returns a user friendly string that list errors in the parsed
|
|
|
|
* document.
|
|
|
|
* \return Formatted error message with the list of errors with their location
|
|
|
|
* in
|
|
|
|
* the parsed document. An empty string is returned if no error
|
|
|
|
* occurred
|
|
|
|
* during parsing.
|
|
|
|
* \deprecated Use getFormattedErrorMessages() instead (typo fix).
|
|
|
|
*/
|
|
|
|
JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
|
|
|
|
std::string getFormatedErrorMessages() const;
|
|
|
|
|
|
|
|
/** \brief Returns a user friendly string that list errors in the parsed
|
|
|
|
* document.
|
|
|
|
* \return Formatted error message with the list of errors with their location
|
|
|
|
* in
|
|
|
|
* the parsed document. An empty string is returned if no error
|
|
|
|
* occurred
|
|
|
|
* during parsing.
|
|
|
|
*/
|
|
|
|
std::string getFormattedErrorMessages() const;
|
|
|
|
|
|
|
|
/** \brief Returns a vector of structured erros encounted while parsing.
|
|
|
|
* \return A (possibly empty) vector of StructuredError objects. Currently
|
|
|
|
* only one error can be returned, but the caller should tolerate
|
|
|
|
* multiple
|
|
|
|
* errors. This can occur if the parser recovers from a non-fatal
|
|
|
|
* parse error and then encounters additional errors.
|
|
|
|
*/
|
|
|
|
std::vector<StructuredError> getStructuredErrors() const;
|
|
|
|
|
2014-10-23 09:03:43 +02:00
|
|
|
/** \brief Add a semantic error message.
|
|
|
|
* \param value JSON Value location associated with the error
|
|
|
|
* \param message The error message.
|
|
|
|
* \return \c true if the error was successfully added, \c false if the
|
|
|
|
* Value offset exceeds the document size.
|
|
|
|
*/
|
|
|
|
bool pushError(const Value& value, const std::string& message);
|
|
|
|
|
|
|
|
/** \brief Add a semantic error message with extra context.
|
|
|
|
* \param value JSON Value location associated with the error
|
|
|
|
* \param message The error message.
|
|
|
|
* \param extra Additional JSON Value location to contextualize the error
|
|
|
|
* \return \c true if the error was successfully added, \c false if either
|
|
|
|
* Value offset exceeds the document size.
|
|
|
|
*/
|
|
|
|
bool pushError(const Value& value, const std::string& message, const Value& extra);
|
|
|
|
|
|
|
|
/** \brief Return whether there are any errors.
|
|
|
|
* \return \c true if there are no errors to report \c false if
|
|
|
|
* errors have occurred.
|
|
|
|
*/
|
|
|
|
bool good() const;
|
|
|
|
|
2014-07-01 00:48:54 +02:00
|
|
|
private:
|
|
|
|
enum TokenType {
|
|
|
|
tokenEndOfStream = 0,
|
|
|
|
tokenObjectBegin,
|
|
|
|
tokenObjectEnd,
|
|
|
|
tokenArrayBegin,
|
|
|
|
tokenArrayEnd,
|
|
|
|
tokenString,
|
|
|
|
tokenNumber,
|
|
|
|
tokenTrue,
|
|
|
|
tokenFalse,
|
|
|
|
tokenNull,
|
|
|
|
tokenArraySeparator,
|
|
|
|
tokenMemberSeparator,
|
|
|
|
tokenComment,
|
|
|
|
tokenError
|
|
|
|
};
|
|
|
|
|
|
|
|
class Token {
|
|
|
|
public:
|
|
|
|
TokenType type_;
|
|
|
|
Location start_;
|
|
|
|
Location end_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ErrorInfo {
|
|
|
|
public:
|
|
|
|
Token token_;
|
|
|
|
std::string message_;
|
|
|
|
Location extra_;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::deque<ErrorInfo> Errors;
|
|
|
|
|
2014-09-15 02:15:29 +02:00
|
|
|
bool readToken(Token& token);
|
2014-07-01 00:48:54 +02:00
|
|
|
void skipSpaces();
|
|
|
|
bool match(Location pattern, int patternLength);
|
|
|
|
bool readComment();
|
|
|
|
bool readCStyleComment();
|
|
|
|
bool readCppStyleComment();
|
|
|
|
bool readString();
|
|
|
|
void readNumber();
|
|
|
|
bool readValue();
|
2014-09-15 02:15:29 +02:00
|
|
|
bool readObject(Token& token);
|
|
|
|
bool readArray(Token& token);
|
|
|
|
bool decodeNumber(Token& token);
|
|
|
|
bool decodeNumber(Token& token, Value& decoded);
|
|
|
|
bool decodeString(Token& token);
|
|
|
|
bool decodeString(Token& token, std::string& decoded);
|
|
|
|
bool decodeDouble(Token& token);
|
|
|
|
bool decodeDouble(Token& token, Value& decoded);
|
|
|
|
bool decodeUnicodeCodePoint(Token& token,
|
|
|
|
Location& current,
|
2014-07-01 00:48:54 +02:00
|
|
|
Location end,
|
2014-09-15 02:15:29 +02:00
|
|
|
unsigned int& unicode);
|
|
|
|
bool decodeUnicodeEscapeSequence(Token& token,
|
|
|
|
Location& current,
|
2014-07-01 00:48:54 +02:00
|
|
|
Location end,
|
2014-09-15 02:15:29 +02:00
|
|
|
unsigned int& unicode);
|
|
|
|
bool addError(const std::string& message, Token& token, Location extra = 0);
|
2014-07-01 00:48:54 +02:00
|
|
|
bool recoverFromError(TokenType skipUntilToken);
|
2014-09-15 02:15:29 +02:00
|
|
|
bool addErrorAndRecover(const std::string& message,
|
|
|
|
Token& token,
|
2014-07-01 00:48:54 +02:00
|
|
|
TokenType skipUntilToken);
|
|
|
|
void skipUntilSpace();
|
2014-09-15 02:15:29 +02:00
|
|
|
Value& currentValue();
|
2014-07-01 00:48:54 +02:00
|
|
|
Char getNextChar();
|
|
|
|
void
|
2014-09-15 02:15:29 +02:00
|
|
|
getLocationLineAndColumn(Location location, int& line, int& column) const;
|
2014-07-01 00:48:54 +02:00
|
|
|
std::string getLocationLineAndColumn(Location location) const;
|
|
|
|
void addComment(Location begin, Location end, CommentPlacement placement);
|
2014-09-15 02:15:29 +02:00
|
|
|
void skipCommentTokens(Token& token);
|
2014-07-01 00:48:54 +02:00
|
|
|
|
2014-09-15 02:15:29 +02:00
|
|
|
typedef std::stack<Value*> Nodes;
|
2014-07-01 00:48:54 +02:00
|
|
|
Nodes nodes_;
|
|
|
|
Errors errors_;
|
|
|
|
std::string document_;
|
|
|
|
Location begin_;
|
|
|
|
Location end_;
|
|
|
|
Location current_;
|
|
|
|
Location lastValueEnd_;
|
2014-09-15 02:15:29 +02:00
|
|
|
Value* lastValue_;
|
2014-07-01 00:48:54 +02:00
|
|
|
std::string commentsBefore_;
|
|
|
|
Features features_;
|
|
|
|
bool collectComments_;
|
2015-01-29 21:29:40 +01:00
|
|
|
}; // Reader
|
|
|
|
|
|
|
|
/** Interface for reading JSON from a char array.
|
|
|
|
*/
|
|
|
|
class JSON_API CharReader {
|
|
|
|
public:
|
|
|
|
virtual ~CharReader() {}
|
|
|
|
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
|
|
|
|
document.
|
|
|
|
* The document must be a UTF-8 encoded string containing the document to read.
|
|
|
|
*
|
|
|
|
* \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the
|
|
|
|
document to read.
|
|
|
|
* \param endDoc Pointer on the end of the UTF-8 encoded string of the
|
|
|
|
document to read.
|
|
|
|
* Must be >= beginDoc.
|
|
|
|
* \param root [out] Contains the root value of the document if it was
|
|
|
|
* successfully parsed.
|
|
|
|
* \param errs [out] Formatted error messages (if not NULL)
|
|
|
|
* a user friendly string that lists errors in the parsed
|
|
|
|
* document.
|
|
|
|
* \return \c true if the document was successfully parsed, \c false if an
|
|
|
|
error occurred.
|
|
|
|
*/
|
|
|
|
virtual bool parse(
|
|
|
|
char const* beginDoc, char const* endDoc,
|
|
|
|
Value* root, std::string* errs) = 0;
|
|
|
|
|
|
|
|
class Factory {
|
|
|
|
public:
|
|
|
|
/// \brief Allocate a CharReader via operator new().
|
|
|
|
virtual CharReader* newCharReader() const = 0;
|
|
|
|
}; // Factory
|
|
|
|
}; // CharReader
|
|
|
|
|
2015-02-09 08:29:43 +01:00
|
|
|
/** \brief Build a CharReader implementation.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
\code
|
|
|
|
using namespace Json;
|
|
|
|
CharReaderBuilder builder;
|
2015-02-09 18:15:39 +01:00
|
|
|
builder.collectComments_ = false;
|
2015-02-09 08:29:43 +01:00
|
|
|
Value value;
|
|
|
|
std::string errs;
|
2015-02-09 18:15:39 +01:00
|
|
|
bool ok = parseFromStream(builder, std::cin, &value, &errs);
|
2015-02-09 08:29:43 +01:00
|
|
|
\endcode
|
|
|
|
*/
|
2015-01-29 21:29:40 +01:00
|
|
|
class CharReaderBuilder : public CharReader::Factory {
|
2015-02-09 08:29:43 +01:00
|
|
|
public:
|
2015-02-09 18:15:39 +01:00
|
|
|
/** default: true
|
|
|
|
*
|
|
|
|
* It is possible to "allow" comments but still not "collect" them.
|
|
|
|
*/
|
2015-01-29 21:29:40 +01:00
|
|
|
bool collectComments_;
|
2015-02-09 18:15:39 +01:00
|
|
|
/** default: all()
|
|
|
|
*
|
|
|
|
* For historical reasons, Features is a separate structure.
|
|
|
|
*/
|
2015-01-29 21:29:40 +01:00
|
|
|
Features features_;
|
|
|
|
|
2015-02-09 08:29:43 +01:00
|
|
|
CharReaderBuilder();
|
|
|
|
virtual ~CharReaderBuilder();
|
2015-01-29 21:29:40 +01:00
|
|
|
|
|
|
|
virtual CharReader* newCharReader() const;
|
2014-07-01 00:48:54 +02:00
|
|
|
};
|
|
|
|
|
2015-01-29 21:29:40 +01:00
|
|
|
/** Consume entire stream and use its begin/end.
|
|
|
|
* Someday we might have a real StreamReader, but for now this
|
|
|
|
* is convenient.
|
|
|
|
*/
|
|
|
|
bool parseFromStream(
|
|
|
|
CharReader::Factory const&,
|
|
|
|
std::istream&,
|
|
|
|
Value* root, std::string* errs);
|
|
|
|
|
2014-07-01 00:48:54 +02:00
|
|
|
/** \brief Read from 'sin' into 'root'.
|
|
|
|
|
|
|
|
Always keep comments from the input JSON.
|
|
|
|
|
|
|
|
This can be used to read a file into a particular sub-object.
|
|
|
|
For example:
|
|
|
|
\code
|
|
|
|
Json::Value root;
|
|
|
|
cin >> root["dir"]["file"];
|
|
|
|
cout << root;
|
|
|
|
\endcode
|
|
|
|
Result:
|
|
|
|
\verbatim
|
|
|
|
{
|
|
|
|
"dir": {
|
|
|
|
"file": {
|
|
|
|
// The input stream JSON would be nested here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
\endverbatim
|
|
|
|
\throw std::exception on parse error.
|
|
|
|
\see Json::operator<<()
|
|
|
|
*/
|
2014-09-15 02:15:29 +02:00
|
|
|
JSON_API std::istream& operator>>(std::istream&, Value&);
|
2007-06-13 17:51:04 +02:00
|
|
|
|
|
|
|
} // namespace Json
|
|
|
|
|
2013-05-08 22:21:11 +02:00
|
|
|
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
2014-07-01 00:48:54 +02:00
|
|
|
#pragma warning(pop)
|
2013-05-08 22:21:11 +02:00
|
|
|
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
|
|
|
|
|
2007-06-13 17:51:04 +02:00
|
|
|
#endif // CPPTL_JSON_READER_H_INCLUDED
|