123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #ifndef BASE_JSON_JSON_READER_H_
- #define BASE_JSON_JSON_READER_H_
- #include <memory>
- #include <string>
- #include "base/base_export.h"
- #include "base/json/json_common.h"
- #include "base/optional.h"
- #include "base/strings/string_piece.h"
- #include "base/values.h"
- namespace base {
- namespace internal {
- class JSONParser;
- }
- enum JSONParserOptions {
-
-
- JSON_PARSE_RFC = 0,
-
- JSON_ALLOW_TRAILING_COMMAS = 1 << 0,
-
-
-
-
- JSON_REPLACE_INVALID_CHARACTERS = 1 << 1,
- };
- class BASE_EXPORT JSONReader {
- public:
-
-
-
-
-
-
-
- enum JsonParseError {
- JSON_NO_ERROR = 0,
- JSON_INVALID_ESCAPE,
- JSON_SYNTAX_ERROR,
- JSON_UNEXPECTED_TOKEN,
- JSON_TRAILING_COMMA,
- JSON_TOO_MUCH_NESTING,
- JSON_UNEXPECTED_DATA_AFTER_ROOT,
- JSON_UNSUPPORTED_ENCODING,
- JSON_UNQUOTED_DICTIONARY_KEY,
- JSON_TOO_LARGE,
- JSON_UNREPRESENTABLE_NUMBER,
- JSON_PARSE_ERROR_COUNT
- };
- struct BASE_EXPORT ValueWithError {
- ValueWithError();
- ValueWithError(ValueWithError&& other);
- ValueWithError& operator=(ValueWithError&& other);
- ~ValueWithError();
- Optional<Value> value;
-
-
- JsonParseError error_code = JSON_NO_ERROR;
- std::string error_message;
- int error_line = 0;
- int error_column = 0;
- DISALLOW_COPY_AND_ASSIGN(ValueWithError);
- };
-
- static const char kInvalidEscape[];
- static const char kSyntaxError[];
- static const char kUnexpectedToken[];
- static const char kTrailingComma[];
- static const char kTooMuchNesting[];
- static const char kUnexpectedDataAfterRoot[];
- static const char kUnsupportedEncoding[];
- static const char kUnquotedDictionaryKey[];
- static const char kInputTooLarge[];
- static const char kUnrepresentableNumber[];
-
- JSONReader(int options = JSON_PARSE_RFC,
- size_t max_depth = internal::kAbsoluteMaxDepth);
- ~JSONReader();
-
-
- static Optional<Value> Read(StringPiece json,
- int options = JSON_PARSE_RFC,
- size_t max_depth = internal::kAbsoluteMaxDepth);
-
-
-
-
-
- static std::unique_ptr<Value> ReadDeprecated(
- StringPiece json,
- int options = JSON_PARSE_RFC,
- size_t max_depth = internal::kAbsoluteMaxDepth);
-
-
-
- static ValueWithError ReadAndReturnValueWithError(
- StringPiece json,
- int options = JSON_PARSE_RFC);
-
-
-
-
-
- static std::unique_ptr<Value> ReadAndReturnErrorDeprecated(
- StringPiece json,
- int options,
- int* error_code_out,
- std::string* error_msg_out,
- int* error_line_out = nullptr,
- int* error_column_out = nullptr);
-
-
- static std::string ErrorCodeToString(JsonParseError error_code);
-
- Optional<Value> ReadToValue(StringPiece json);
-
-
- std::unique_ptr<Value> ReadToValueDeprecated(StringPiece json);
-
-
- std::string GetErrorMessage() const;
- private:
- std::unique_ptr<internal::JSONParser> parser_;
- };
- }
- #endif
|