allowBom -> skipBom (#1162)

This commit is contained in:
Chen 2020-04-28 17:30:08 +08:00 committed by GitHub
parent 83946a28db
commit 2cb16b35dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 13 deletions

View File

@ -871,7 +871,7 @@ public:
bool failIfExtra_; bool failIfExtra_;
bool rejectDupKeys_; bool rejectDupKeys_;
bool allowSpecialFloats_; bool allowSpecialFloats_;
bool allowBom_; bool skipBom_;
size_t stackLimit_; size_t stackLimit_;
}; // OurFeatures }; // OurFeatures
@ -940,7 +940,7 @@ private:
bool readToken(Token& token); bool readToken(Token& token);
void skipSpaces(); void skipSpaces();
void skipBom(bool allowBom); void skipBom(bool skipBom);
bool match(const Char* pattern, int patternLength); bool match(const Char* pattern, int patternLength);
bool readComment(); bool readComment();
bool readCStyleComment(bool* containsNewLineResult); bool readCStyleComment(bool* containsNewLineResult);
@ -1025,7 +1025,7 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
nodes_.push(&root); nodes_.push(&root);
// skip byte order mark if it exists at the beginning of the UTF-8 text. // skip byte order mark if it exists at the beginning of the UTF-8 text.
skipBom(features_.allowBom_); skipBom(features_.skipBom_);
bool successful = readValue(); bool successful = readValue();
nodes_.pop(); nodes_.pop();
Token token; Token token;
@ -1272,10 +1272,9 @@ void OurReader::skipSpaces() {
} }
} }
void OurReader::skipBom(bool allowBom) { void OurReader::skipBom(bool skipBom) {
// If BOM is not allowed, then skip it. // The default behavior is to skip BOM.
// The default value is: false if (skipBom) {
if (!allowBom) {
if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) { if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) {
begin_ += 3; begin_ += 3;
current_ = begin_; current_ = begin_;
@ -1900,7 +1899,7 @@ CharReader* CharReaderBuilder::newCharReader() const {
features.failIfExtra_ = settings_["failIfExtra"].asBool(); features.failIfExtra_ = settings_["failIfExtra"].asBool();
features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool(); features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool();
features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool(); features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool();
features.allowBom_ = settings_["allowBom"].asBool(); features.skipBom_ = settings_["skipBom"].asBool();
return new OurCharReader(collectComments, features); return new OurCharReader(collectComments, features);
} }
static void getValidReaderKeys(std::set<String>* valid_keys) { static void getValidReaderKeys(std::set<String>* valid_keys) {
@ -1916,7 +1915,7 @@ static void getValidReaderKeys(std::set<String>* valid_keys) {
valid_keys->insert("failIfExtra"); valid_keys->insert("failIfExtra");
valid_keys->insert("rejectDupKeys"); valid_keys->insert("rejectDupKeys");
valid_keys->insert("allowSpecialFloats"); valid_keys->insert("allowSpecialFloats");
valid_keys->insert("allowBom"); valid_keys->insert("skipBom");
} }
bool CharReaderBuilder::validate(Json::Value* invalid) const { bool CharReaderBuilder::validate(Json::Value* invalid) const {
Json::Value my_invalid; Json::Value my_invalid;
@ -1951,7 +1950,7 @@ void CharReaderBuilder::strictMode(Json::Value* settings) {
(*settings)["failIfExtra"] = true; (*settings)["failIfExtra"] = true;
(*settings)["rejectDupKeys"] = true; (*settings)["rejectDupKeys"] = true;
(*settings)["allowSpecialFloats"] = false; (*settings)["allowSpecialFloats"] = false;
(*settings)["allowBom"] = false; (*settings)["skipBom"] = true;
//! [CharReaderBuilderStrictMode] //! [CharReaderBuilderStrictMode]
} }
// static // static
@ -1968,7 +1967,7 @@ void CharReaderBuilder::setDefaults(Json::Value* settings) {
(*settings)["failIfExtra"] = false; (*settings)["failIfExtra"] = false;
(*settings)["rejectDupKeys"] = false; (*settings)["rejectDupKeys"] = false;
(*settings)["allowSpecialFloats"] = false; (*settings)["allowSpecialFloats"] = false;
(*settings)["allowBom"] = false; (*settings)["skipBom"] = true;
//! [CharReaderBuilderDefaults] //! [CharReaderBuilderDefaults]
} }

View File

@ -3590,13 +3590,13 @@ JSONTEST_FIXTURE_LOCAL(BomTest, skipBom) {
JSONTEST_ASSERT(errs.empty()); JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_STRING_EQUAL(root["key"].asString(), "value"); JSONTEST_ASSERT_STRING_EQUAL(root["key"].asString(), "value");
} }
JSONTEST_FIXTURE_LOCAL(BomTest, allowBom) { JSONTEST_FIXTURE_LOCAL(BomTest, notSkipBom) {
const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}"; const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}";
Json::Value root; Json::Value root;
JSONCPP_STRING errs; JSONCPP_STRING errs;
std::istringstream iss(with_bom); std::istringstream iss(with_bom);
Json::CharReaderBuilder b; Json::CharReaderBuilder b;
b.settings_["allowBom"] = true; b.settings_["skipBom"] = false;
bool ok = parseFromStream(b, iss, &root, &errs); bool ok = parseFromStream(b, iss, &root, &errs);
// Detect the BOM, and failed on it. // Detect the BOM, and failed on it.
JSONTEST_ASSERT(!ok); JSONTEST_ASSERT(!ok);