Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
allowBom -> skipBom (open-source-parsers#1162)
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 authored Apr 28, 2020
1 parent 83946a2 commit 2cb16b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 10 additions & 11 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ class OurFeatures {
bool failIfExtra_;
bool rejectDupKeys_;
bool allowSpecialFloats_;
bool allowBom_;
bool skipBom_;
size_t stackLimit_;
}; // OurFeatures

Expand Down Expand Up @@ -940,7 +940,7 @@ class OurReader {

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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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<String>* valid_keys) {
Expand All @@ -1916,7 +1915,7 @@ static void getValidReaderKeys(std::set<String>* 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;
Expand Down Expand Up @@ -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
Expand All @@ -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]
}

Expand Down
4 changes: 2 additions & 2 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2cb16b3

Please sign in to comment.