Skip to content

Commit 7dde688

Browse files
committed
ignore bom at the beginning of the UTF-8 text
add a testcase about bom.
1 parent 3beb37e commit 7dde688

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/lib_json/json_reader.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <memory>
1919
#include <set>
2020
#include <sstream>
21+
#include <string.h>
2122
#include <utility>
2223

2324
#include <cstdio>
@@ -939,6 +940,7 @@ class OurReader {
939940

940941
bool readToken(Token& token);
941942
void skipSpaces();
943+
void skipBom(Location& begin);
942944
bool match(const Char* pattern, int patternLength);
943945
bool readComment();
944946
bool readCStyleComment(bool* containsNewLineResult);
@@ -1010,6 +1012,7 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
10101012
collectComments = false;
10111013
}
10121014

1015+
skipBom(beginDoc);
10131016
begin_ = beginDoc;
10141017
end_ = endDoc;
10151018
collectComments_ = collectComments;
@@ -1021,7 +1024,6 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
10211024
while (!nodes_.empty())
10221025
nodes_.pop();
10231026
nodes_.push(&root);
1024-
10251027
bool successful = readValue();
10261028
nodes_.pop();
10271029
Token token;
@@ -1268,6 +1270,12 @@ void OurReader::skipSpaces() {
12681270
}
12691271
}
12701272

1273+
void OurReader::skipBom(Location& begin) {
1274+
if (strncmp(begin, "\xEF\xBB\xBF", 3) == 0) {
1275+
begin += 3;
1276+
}
1277+
}
1278+
12711279
bool OurReader::match(const Char* pattern, int patternLength) {
12721280
if (end_ - current_ < patternLength)
12731281
return false;

src/test_lib_json/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3578,6 +3578,20 @@ JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {
35783578
}
35793579
}
35803580

3581+
struct BomTest : JsonTest::TestCase {};
3582+
3583+
JSONTEST_FIXTURE_LOCAL(BomTest, withBom) {
3584+
const std::string with_bom = u8"\xEF\xBB\xBF{}";
3585+
Json::Value root;
3586+
JSONCPP_STRING errs;
3587+
std::istringstream iss(with_bom);
3588+
bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs);
3589+
JSONTEST_ASSERT(ok);
3590+
if (!ok) {
3591+
std::cout << "errs: " << errs << std::endl;
3592+
}
3593+
}
3594+
35813595
struct IteratorTest : JsonTest::TestCase {};
35823596

35833597
JSONTEST_FIXTURE_LOCAL(IteratorTest, convert) {

0 commit comments

Comments
 (0)