From 2cb16b35dcfe0703272c9bbe777ad09546846a04 Mon Sep 17 00:00:00 2001 From: Chen <50514813+dota17@users.noreply.github.com> Date: Tue, 28 Apr 2020 17:30:08 +0800 Subject: [PATCH] allowBom -> skipBom (#1162) --- src/lib_json/json_reader.cpp | 21 ++++++++++----------- src/test_lib_json/main.cpp | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 341162b..02a7d54 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -871,7 +871,7 @@ public: bool failIfExtra_; bool rejectDupKeys_; bool allowSpecialFloats_; - bool allowBom_; + bool skipBom_; size_t stackLimit_; }; // OurFeatures @@ -940,7 +940,7 @@ private: bool readToken(Token& token); void skipSpaces(); - void skipBom(bool allowBom); + void skipBom(bool skipBom); bool match(const Char* pattern, int patternLength); bool readComment(); bool readCStyleComment(bool* containsNewLineResult); @@ -1025,7 +1025,7 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root, nodes_.push(&root); // skip byte order mark if it exists at the beginning of the UTF-8 text. - skipBom(features_.allowBom_); + skipBom(features_.skipBom_); bool successful = readValue(); nodes_.pop(); Token token; @@ -1272,10 +1272,9 @@ void OurReader::skipSpaces() { } } -void OurReader::skipBom(bool allowBom) { - // If BOM is not allowed, then skip it. - // The default value is: false - if (!allowBom) { +void OurReader::skipBom(bool skipBom) { + // The default behavior is to skip BOM. + if (skipBom) { if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) { begin_ += 3; current_ = begin_; @@ -1900,7 +1899,7 @@ CharReader* CharReaderBuilder::newCharReader() const { features.failIfExtra_ = settings_["failIfExtra"].asBool(); features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool(); features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool(); - features.allowBom_ = settings_["allowBom"].asBool(); + features.skipBom_ = settings_["skipBom"].asBool(); return new OurCharReader(collectComments, features); } static void getValidReaderKeys(std::set* valid_keys) { @@ -1916,7 +1915,7 @@ static void getValidReaderKeys(std::set* valid_keys) { valid_keys->insert("failIfExtra"); valid_keys->insert("rejectDupKeys"); valid_keys->insert("allowSpecialFloats"); - valid_keys->insert("allowBom"); + valid_keys->insert("skipBom"); } bool CharReaderBuilder::validate(Json::Value* invalid) const { Json::Value my_invalid; @@ -1951,7 +1950,7 @@ void CharReaderBuilder::strictMode(Json::Value* settings) { (*settings)["failIfExtra"] = true; (*settings)["rejectDupKeys"] = true; (*settings)["allowSpecialFloats"] = false; - (*settings)["allowBom"] = false; + (*settings)["skipBom"] = true; //! [CharReaderBuilderStrictMode] } // static @@ -1968,7 +1967,7 @@ void CharReaderBuilder::setDefaults(Json::Value* settings) { (*settings)["failIfExtra"] = false; (*settings)["rejectDupKeys"] = false; (*settings)["allowSpecialFloats"] = false; - (*settings)["allowBom"] = false; + (*settings)["skipBom"] = true; //! [CharReaderBuilderDefaults] } diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index ede39ee..e6fe43d 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3590,13 +3590,13 @@ JSONTEST_FIXTURE_LOCAL(BomTest, skipBom) { JSONTEST_ASSERT(errs.empty()); 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\"}"; Json::Value root; JSONCPP_STRING errs; std::istringstream iss(with_bom); Json::CharReaderBuilder b; - b.settings_["allowBom"] = true; + b.settings_["skipBom"] = false; bool ok = parseFromStream(b, iss, &root, &errs); // Detect the BOM, and failed on it. JSONTEST_ASSERT(!ok);