@@ -871,6 +871,7 @@ class OurFeatures {
871
871
bool failIfExtra_;
872
872
bool rejectDupKeys_;
873
873
bool allowSpecialFloats_;
874
+ bool allowBom_;
874
875
size_t stackLimit_;
875
876
}; // OurFeatures
876
877
@@ -939,6 +940,7 @@ class OurReader {
939
940
940
941
bool readToken (Token& token);
941
942
void skipSpaces ();
943
+ void skipBom (bool allowBom);
942
944
bool match (const Char* pattern, int patternLength);
943
945
bool readComment ();
944
946
bool readCStyleComment (bool * containsNewLineResult);
@@ -1022,6 +1024,8 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
1022
1024
nodes_.pop ();
1023
1025
nodes_.push (&root);
1024
1026
1027
+ // skip byte order mark if it exists at the beginning of the UTF-8 text.
1028
+ skipBom (features_.allowBom_ );
1025
1029
bool successful = readValue ();
1026
1030
nodes_.pop ();
1027
1031
Token token;
@@ -1268,6 +1272,17 @@ void OurReader::skipSpaces() {
1268
1272
}
1269
1273
}
1270
1274
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
+ }
1283
+ }
1284
+ }
1285
+
1271
1286
bool OurReader::match (const Char* pattern, int patternLength) {
1272
1287
if (end_ - current_ < patternLength)
1273
1288
return false ;
@@ -1885,6 +1900,7 @@ CharReader* CharReaderBuilder::newCharReader() const {
1885
1900
features.failIfExtra_ = settings_[" failIfExtra" ].asBool ();
1886
1901
features.rejectDupKeys_ = settings_[" rejectDupKeys" ].asBool ();
1887
1902
features.allowSpecialFloats_ = settings_[" allowSpecialFloats" ].asBool ();
1903
+ features.allowBom_ = settings_[" allowBom" ].asBool ();
1888
1904
return new OurCharReader (collectComments, features);
1889
1905
}
1890
1906
static void getValidReaderKeys (std::set<String>* valid_keys) {
@@ -1900,6 +1916,7 @@ static void getValidReaderKeys(std::set<String>* valid_keys) {
1900
1916
valid_keys->insert (" failIfExtra" );
1901
1917
valid_keys->insert (" rejectDupKeys" );
1902
1918
valid_keys->insert (" allowSpecialFloats" );
1919
+ valid_keys->insert (" allowBom" );
1903
1920
}
1904
1921
bool CharReaderBuilder::validate (Json::Value* invalid) const {
1905
1922
Json::Value my_invalid;
@@ -1934,6 +1951,7 @@ void CharReaderBuilder::strictMode(Json::Value* settings) {
1934
1951
(*settings)[" failIfExtra" ] = true ;
1935
1952
(*settings)[" rejectDupKeys" ] = true ;
1936
1953
(*settings)[" allowSpecialFloats" ] = false ;
1954
+ (*settings)[" allowBom" ] = false ;
1937
1955
// ! [CharReaderBuilderStrictMode]
1938
1956
}
1939
1957
// static
@@ -1950,6 +1968,7 @@ void CharReaderBuilder::setDefaults(Json::Value* settings) {
1950
1968
(*settings)[" failIfExtra" ] = false ;
1951
1969
(*settings)[" rejectDupKeys" ] = false ;
1952
1970
(*settings)[" allowSpecialFloats" ] = false ;
1971
+ (*settings)[" allowBom" ] = false ;
1953
1972
// ! [CharReaderBuilderDefaults]
1954
1973
}
1955
1974
0 commit comments