Skip to content

Commit 7092f8d

Browse files
committed
refactory skipBom()
1 parent a5414b5 commit 7092f8d

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/lib_json/json_reader.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ class OurFeatures {
871871
bool failIfExtra_;
872872
bool rejectDupKeys_;
873873
bool allowSpecialFloats_;
874+
bool allowBom_;
874875
size_t stackLimit_;
875876
}; // OurFeatures
876877

@@ -939,7 +940,7 @@ class OurReader {
939940

940941
bool readToken(Token& token);
941942
void skipSpaces();
942-
void skipBom();
943+
void skipBom(bool allowBom);
943944
bool match(const Char* pattern, int patternLength);
944945
bool readComment();
945946
bool readCStyleComment(bool* containsNewLineResult);
@@ -1024,7 +1025,7 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
10241025
nodes_.push(&root);
10251026

10261027
// skip byte order mark if it exists at the beginning of the UTF-8 text.
1027-
skipBom();
1028+
skipBom(features_.allowBom_);
10281029
bool successful = readValue();
10291030
nodes_.pop();
10301031
Token token;
@@ -1271,10 +1272,14 @@ void OurReader::skipSpaces() {
12711272
}
12721273
}
12731274

1274-
void OurReader::skipBom() {
1275-
if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) {
1276-
begin_ += 3;
1277-
current_ = begin_;
1275+
void OurReader::skipBom(bool allowBom) {
1276+
// If BOM is not allowed, then skip it.
1277+
// The default value is: false
1278+
if (!allowBom) {
1279+
if (strncmp(begin_, "\xEF\xBB\xBF", 3) == 0) {
1280+
begin_ += 3;
1281+
current_ = begin_;
1282+
}
12781283
}
12791284
}
12801285

@@ -1895,6 +1900,7 @@ CharReader* CharReaderBuilder::newCharReader() const {
18951900
features.failIfExtra_ = settings_["failIfExtra"].asBool();
18961901
features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool();
18971902
features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool();
1903+
features.allowBom_ = settings_["allowBom"].asBool();
18981904
return new OurCharReader(collectComments, features);
18991905
}
19001906
static void getValidReaderKeys(std::set<String>* valid_keys) {
@@ -1910,6 +1916,7 @@ static void getValidReaderKeys(std::set<String>* valid_keys) {
19101916
valid_keys->insert("failIfExtra");
19111917
valid_keys->insert("rejectDupKeys");
19121918
valid_keys->insert("allowSpecialFloats");
1919+
valid_keys->insert("allowBom");
19131920
}
19141921
bool CharReaderBuilder::validate(Json::Value* invalid) const {
19151922
Json::Value my_invalid;
@@ -1944,6 +1951,7 @@ void CharReaderBuilder::strictMode(Json::Value* settings) {
19441951
(*settings)["failIfExtra"] = true;
19451952
(*settings)["rejectDupKeys"] = true;
19461953
(*settings)["allowSpecialFloats"] = false;
1954+
(*settings)["allowBom"] = false;
19471955
//! [CharReaderBuilderStrictMode]
19481956
}
19491957
// static
@@ -1960,6 +1968,7 @@ void CharReaderBuilder::setDefaults(Json::Value* settings) {
19601968
(*settings)["failIfExtra"] = false;
19611969
(*settings)["rejectDupKeys"] = false;
19621970
(*settings)["allowSpecialFloats"] = false;
1971+
(*settings)["allowBom"] = false;
19631972
//! [CharReaderBuilderDefaults]
19641973
}
19651974

src/test_lib_json/main.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3580,16 +3580,29 @@ JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {
35803580

35813581
struct BomTest : JsonTest::TestCase {};
35823582

3583-
JSONTEST_FIXTURE_LOCAL(BomTest, withBom) {
3583+
JSONTEST_FIXTURE_LOCAL(BomTest, skipBom) {
35843584
const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}";
35853585
Json::Value root;
35863586
JSONCPP_STRING errs;
35873587
std::istringstream iss(with_bom);
35883588
bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs);
3589+
// The default behavior is to skip the BOM, so we can parse it normally.
35893590
JSONTEST_ASSERT(ok);
35903591
JSONTEST_ASSERT(errs.empty());
35913592
JSONTEST_ASSERT_STRING_EQUAL(root["key"].asString(), "value");
35923593
}
3594+
JSONTEST_FIXTURE_LOCAL(BomTest, allowBom) {
3595+
const std::string with_bom = "\xEF\xBB\xBF{\"key\" : \"value\"}";
3596+
Json::Value root;
3597+
JSONCPP_STRING errs;
3598+
std::istringstream iss(with_bom);
3599+
Json::CharReaderBuilder b;
3600+
b.settings_["allowBom"] = true;
3601+
bool ok = parseFromStream(b, iss, &root, &errs);
3602+
// Detect the BOM, and failed on it.
3603+
JSONTEST_ASSERT(!ok);
3604+
JSONTEST_ASSERT(!errs.empty());
3605+
}
35933606

35943607
struct IteratorTest : JsonTest::TestCase {};
35953608

0 commit comments

Comments
 (0)