Ignore byte order mark in the head of UTF-8 text. (#1149)

* Ignore bom at the beginning of the UTF-8 text
This commit is contained in:
Chen
2020-04-28 15:16:05 +08:00
committed by GitHub
parent 91f1553f2c
commit 83946a28db
2 changed files with 45 additions and 0 deletions

View File

@@ -3577,6 +3577,32 @@ JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {
}
}
struct BomTest : JsonTest::TestCase {};
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 {};
JSONTEST_FIXTURE_LOCAL(IteratorTest, convert) {