Skip to content

Commit

Permalink
refactory skipBom()
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed Apr 28, 2020
1 parent a5414b5 commit 7092f8d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/lib_json/json_reader.cpp
Expand Up @@ -871,6 +871,7 @@ class OurFeatures {
bool failIfExtra_;
bool rejectDupKeys_;
bool allowSpecialFloats_;
bool allowBom_;
size_t stackLimit_;
}; // OurFeatures

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

bool readToken(Token& token);
void skipSpaces();
void skipBom();
void skipBom(bool allowBom);
bool match(const Char* pattern, int patternLength);
bool readComment();
bool readCStyleComment(bool* containsNewLineResult);
Expand Down Expand Up @@ -1024,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();
skipBom(features_.allowBom_);
bool successful = readValue();
nodes_.pop();
Token token;
Expand Down Expand Up @@ -1271,10 +1272,14 @@ void OurReader::skipSpaces() {
}
}

void OurReader::skipBom() {
if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) {
begin_ += 3;
current_ = begin_;
void OurReader::skipBom(bool allowBom) {
// If BOM is not allowed, then skip it.
// The default value is: false
if (!allowBom) {
if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) {
begin_ += 3;
current_ = begin_;
}
}
}

Expand Down Expand Up @@ -1895,6 +1900,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();
return new OurCharReader(collectComments, features);
}
static void getValidReaderKeys(std::set<String>* valid_keys) {
Expand All @@ -1910,6 +1916,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");
}
bool CharReaderBuilder::validate(Json::Value* invalid) const {
Json::Value my_invalid;
Expand Down Expand Up @@ -1944,6 +1951,7 @@ void CharReaderBuilder::strictMode(Json::Value* settings) {
(*settings)["failIfExtra"] = true;
(*settings)["rejectDupKeys"] = true;
(*settings)["allowSpecialFloats"] = false;
(*settings)["allowBom"] = false;
//! [CharReaderBuilderStrictMode]
}
// static
Expand All @@ -1960,6 +1968,7 @@ void CharReaderBuilder::setDefaults(Json::Value* settings) {
(*settings)["failIfExtra"] = false;
(*settings)["rejectDupKeys"] = false;
(*settings)["allowSpecialFloats"] = false;
(*settings)["allowBom"] = false;
//! [CharReaderBuilderDefaults]
}

Expand Down
15 changes: 14 additions & 1 deletion src/test_lib_json/main.cpp
Expand Up @@ -3580,16 +3580,29 @@ JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {

struct BomTest : JsonTest::TestCase {};

JSONTEST_FIXTURE_LOCAL(BomTest, withBom) {
JSONTEST_FIXTURE_LOCAL(BomTest, skipBom) {
const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}";
Json::Value root;
JSONCPP_STRING errs;
std::istringstream iss(with_bom);
bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs);
// The default behavior is to skip the BOM, so we can parse it normally.
JSONTEST_ASSERT(ok);
JSONTEST_ASSERT(errs.empty());
JSONTEST_ASSERT_STRING_EQUAL(root["key"].asString(), "value");
}
JSONTEST_FIXTURE_LOCAL(BomTest, allowBom) {
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;
bool ok = parseFromStream(b, iss, &root, &errs);
// Detect the BOM, and failed on it.
JSONTEST_ASSERT(!ok);
JSONTEST_ASSERT(!errs.empty());
}

struct IteratorTest : JsonTest::TestCase {};

Expand Down

0 comments on commit 7092f8d

Please sign in to comment.