JsonCpp project page | Classes | Namespace | JsonCpp home page

reader.h
Go to the documentation of this file.
// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
// 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
#ifndef JSON_READER_H_INCLUDED
#define JSON_READER_H_INCLUDED
#if !defined(JSON_IS_AMALGAMATION)
#include "json_features.h"
#include "value.h"
#endif // if !defined(JSON_IS_AMALGAMATION)
#include <deque>
#include <iosfwd>
#include <istream>
#include <stack>
#include <string>
// Disable warning C4251: <data member>: <type> needs to have dll-interface to
// be used by...
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
#pragma warning(push)
#pragma warning(disable : 4251)
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
#pragma pack(push)
#pragma pack()
namespace Json {
class JSON_API Reader {
public:
using Char = char;
using Location = const Char*;
struct StructuredError {
ptrdiff_t offset_start;
ptrdiff_t offset_limit;
String message;
};
Reader();
Reader(const Features& features);
bool parse(const std::string& document, Value& root,
bool collectComments = true);
bool parse(const char* beginDoc, const char* endDoc, Value& root,
bool collectComments = true);
bool parse(IStream& is, Value& root, bool collectComments = true);
JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
String getFormatedErrorMessages() const;
String getFormattedErrorMessages() const;
std::vector<StructuredError> getStructuredErrors() const;
bool pushError(const Value& value, const String& message);
bool pushError(const Value& value, const String& message, const Value& extra);
bool good() const;
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_;
String message_;
Location extra_;
};
using Errors = std::deque<ErrorInfo>;
bool readToken(Token& token);
bool readTokenSkippingComments(Token& token);
void skipSpaces();
bool match(const Char* pattern, int patternLength);
bool readComment();
bool readCStyleComment();
bool readCppStyleComment();
bool readString();
void readNumber();
bool readValue();
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, String& decoded);
bool decodeDouble(Token& token);
bool decodeDouble(Token& token, Value& decoded);
bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
unsigned int& unicode);
bool decodeUnicodeEscapeSequence(Token& token, Location& current,
Location end, unsigned int& unicode);
bool addError(const String& message, Token& token, Location extra = nullptr);
bool recoverFromError(TokenType skipUntilToken);
bool addErrorAndRecover(const String& message, Token& token,
TokenType skipUntilToken);
void skipUntilSpace();
Value& currentValue();
Char getNextChar();
void getLocationLineAndColumn(Location location, int& line,
int& column) const;
String getLocationLineAndColumn(Location location) const;
void addComment(Location begin, Location end, CommentPlacement placement);
static bool containsNewLine(Location begin, Location end);
static String normalizeEOL(Location begin, Location end);
using Nodes = std::stack<Value*>;
Nodes nodes_;
Errors errors_;
String document_;
Location begin_{};
Location end_{};
Location current_{};
Location lastValueEnd_{};
Value* lastValue_{};
String commentsBefore_;
Features features_;
bool collectComments_{};
}; // Reader
class JSON_API CharReader {
public:
struct JSON_API StructuredError {
ptrdiff_t offset_start;
ptrdiff_t offset_limit;
String message;
};
virtual ~CharReader() = default;
virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
String* errs);
std::vector<StructuredError> getStructuredErrors() const;
class JSON_API Factory {
public:
virtual ~Factory() = default;
virtual CharReader* newCharReader() const = 0;
}; // Factory
protected:
class Impl {
public:
virtual ~Impl() = default;
virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
String* errs) = 0;
virtual std::vector<StructuredError> getStructuredErrors() const = 0;
};
explicit CharReader(std::unique_ptr<Impl> impl) : _impl(std::move(impl)) {}
private:
std::unique_ptr<Impl> _impl;
}; // CharReader
class JSON_API CharReaderBuilder : public CharReader::Factory {
public:
// Note: We use a Json::Value so that we can add data-members to this class
// without a major version bump.
Json::Value settings_;
CharReaderBuilder();
~CharReaderBuilder() override;
CharReader* newCharReader() const override;
bool validate(Json::Value* invalid) const;
Value& operator[](const String& key);
static void setDefaults(Json::Value* settings);
static void strictMode(Json::Value* settings);
static void ecma404Mode(Json::Value* settings);
};
bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
String* errs);
JSON_API IStream& operator>>(IStream&, Value&);
} // namespace Json
#pragma pack(pop)
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
#pragma warning(pop)
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
#endif // JSON_READER_H_INCLUDED
Json::CharReader::Factory
Definition reader.h:281
Json::CharReader::Factory::~Factory
virtual ~Factory()=default
Json::CharReader::Factory::newCharReader
virtual CharReader * newCharReader() const =0
Allocate a CharReader via operator new().
Json::CharReader::Impl
Definition reader.h:291
Json::CharReader::Impl::getStructuredErrors
virtual std::vector< StructuredError > getStructuredErrors() const =0
Json::CharReader::Impl::parse
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, String *errs)=0
Json::CharReader::Impl::~Impl
virtual ~Impl()=default
Json::CharReaderBuilder
Build a CharReader implementation.
Definition reader.h:317
Json::CharReaderBuilder::setDefaults
static void setDefaults(Json::Value *settings)
Called by ctor, but you can use this to reset settings_.
Definition json_reader.cpp:1960
Json::CharReaderBuilder::ecma404Mode
static void ecma404Mode(Json::Value *settings)
ECMA-404 mode.
Definition json_reader.cpp:1977
Json::CharReaderBuilder::operator[]
Value & operator[](const String &key)
A simple way to update a specific setting.
Definition json_reader.cpp:1940
Json::CharReaderBuilder::strictMode
static void strictMode(Json::Value *settings)
Same as old Features::strictMode().
Definition json_reader.cpp:1944
Json::CharReaderBuilder::validate
bool validate(Json::Value *invalid) const
Configuration of this builder.
Definition json_reader.cpp:1913
Json::CharReader
Interface for reading JSON from a char array.
Definition reader.h:248
Json::CharReader::CharReader
CharReader(std::unique_ptr< Impl > impl)
Definition reader.h:299
Json::CharReader::~CharReader
virtual ~CharReader()=default
Json::CharReader::getStructuredErrors
std::vector< StructuredError > getStructuredErrors() const
Returns a vector of structured errors encountered while parsing.
Definition json_reader.cpp:1994
Json::CharReader::parse
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, String *errs)
Read a Value from a JSON document.
Definition json_reader.cpp:1998
Json::Features
Configuration passed to reader and writer.
Definition json_features.h:22
Json::Reader::Char
char Char
Definition reader.h:39
Json::Reader::Reader
Reader()
Constructs a Reader allowing all features for parsing.
Definition json_reader.cpp:78
Json::Reader::pushError
bool pushError(const Value &value, const String &message)
Add a semantic error message.
Definition json_reader.cpp:808
Json::Reader::good
bool good() const
Return whether there are any errors.
Definition json_reader.cpp:842
Json::Reader::Location
const Char * Location
Definition reader.h:40
Json::Reader::getStructuredErrors
std::vector< StructuredError > getStructuredErrors() const
Returns a vector of structured errors encountered while parsing.
Definition json_reader.cpp:796
Json::Reader::parse
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
Definition json_reader.cpp:82
Json::Reader::getFormattedErrorMessages
String getFormattedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
Definition json_reader.cpp:783
Json::Value
Represents a JSON value.
Definition value.h:207
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition config.h:50
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition config.h:89
json_features.h
Json
JSON (JavaScript Object Notation).
Definition allocator.h:16
Json::IStream
std::istream IStream
Definition config.h:142
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:135
Json::operator>>
IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
Definition json_reader.cpp:2018
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
Definition json_reader.cpp:2006
Json::CharReader::StructuredError
Definition reader.h:250
Json::CharReader::StructuredError::offset_limit
ptrdiff_t offset_limit
Definition reader.h:252
Json::CharReader::StructuredError::offset_start
ptrdiff_t offset_start
Definition reader.h:251
Json::CharReader::StructuredError::message
String message
Definition reader.h:253
Json::Reader::StructuredError
An error tagged with where in the JSON text it was encountered.
Definition reader.h:47
Json::Reader::StructuredError::message
String message
Definition reader.h:50
Json::Reader::StructuredError::offset_start
ptrdiff_t offset_start
Definition reader.h:48
Json::Reader::StructuredError::offset_limit
ptrdiff_t offset_limit
Definition reader.h:49
value.h