Struct ParseResult

Synopsis

#include <include/rapidjson/error/error.h>

struct ParseResult

Description

Result of parsing (wraps ParseErrorCode)

Document doc;
ParseResult ok = doc.Parse("[42]");
if (!ok) {
    fprintf(stderr, "JSON parse error: %s (%u)",
            GetParseError_En(ok.Code()), ok.Offset());
    exit(EXIT_FAILURE);
}
See
GenericReader::Parse, GenericDocument::Parse

Mentioned in

Methods

ParseResult overloadDefault constructor, no error.
ParseResult overloadConstructor to set an error.
ClearReset error code.
CodeGet the error code.
IsErrorWhether the result is an error.
OffsetGet the error offset, if IsError(), 0 otherwise.
operator BooleanTypeExplicit conversion to bool, returns true, iff !IsError().
operator!= overload
operator== overload
SetUpdate error code and offset.

Source

Lines 106-141 in include/rapidjson/error/error.h.

struct ParseResult {
    //!! Unspecified boolean type
    typedef bool (ParseResult::*BooleanType)() const;
public:
    //! Default constructor, no error.
    ParseResult() : code_(kParseErrorNone), offset_(0) {}
    //! Constructor to set an error.
    ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}

    //! Get the error code.
    ParseErrorCode Code() const { return code_; }
    //! Get the error offset, if \ref IsError(), 0 otherwise.
    size_t Offset() const { return offset_; }

    //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError().
    operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; }
    //! Whether the result is an error.
    bool IsError() const { return code_ != kParseErrorNone; }

    bool operator==(const ParseResult& that) const { return code_ == that.code_; }
    bool operator==(ParseErrorCode code) const { return code_ == code; }
    friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }

    bool operator!=(const ParseResult& that) const { return !(*this == that); }
    bool operator!=(ParseErrorCode code) const { return !(*this == code); }
    friend bool operator!=(ParseErrorCode code, const ParseResult & err) { return err != code; }

    //! Reset error code.
    void Clear() { Set(kParseErrorNone); }
    //! Update error code and offset.
    void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }

private:
    ParseErrorCode code_;
    size_t offset_;
};





Add Discussion as Guest

Log in