Skip to content

Commit

Permalink
Merge 49d584f into 3beb37e
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed Mar 24, 2020
2 parents 3beb37e + 49d584f commit 4adb56d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <memory>
#include <set>
#include <sstream>
#include <string.h>
#include <utility>

#include <cstdio>
Expand Down Expand Up @@ -939,6 +940,7 @@ class OurReader {

bool readToken(Token& token);
void skipSpaces();
void skipBom();
bool match(const Char* pattern, int patternLength);
bool readComment();
bool readCStyleComment(bool* containsNewLineResult);
Expand Down Expand Up @@ -1021,7 +1023,7 @@ bool OurReader::parse(const char* beginDoc, const char* endDoc, Value& root,
while (!nodes_.empty())
nodes_.pop();
nodes_.push(&root);

skipBom();
bool successful = readValue();
nodes_.pop();
Token token;
Expand Down Expand Up @@ -1268,6 +1270,12 @@ void OurReader::skipSpaces() {
}
}

void OurReader::skipBom() {
if (strncmp(current_, "\xEF\xBB\xBF", 3) == 0) {
current_ += 3;
}
}

bool OurReader::match(const Char* pattern, int patternLength) {
if (end_ - current_ < patternLength)
return false;
Expand Down
15 changes: 15 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3578,6 +3578,21 @@ JSONTEST_FIXTURE_LOCAL(BuilderTest, settings) {
}
}

struct BomTest : JsonTest::TestCase {};

JSONTEST_FIXTURE_LOCAL(BomTest, withBom) {
const std::string with_bom = u8"\xEF\xBB\xBF{\"key\" : \"value\"}";
Json::Value root;
JSONCPP_STRING errs;
std::istringstream iss(with_bom);
bool ok = parseFromStream(Json::CharReaderBuilder(), iss, &root, &errs);
JSONTEST_ASSERT(ok);
if (!ok) {
std::cerr << "errs: " << errs << std::endl;
}
JSONTEST_ASSERT_STRING_EQUAL(root["key"].asString(), "value");
}

struct IteratorTest : JsonTest::TestCase {};

JSONTEST_FIXTURE_LOCAL(IteratorTest, convert) {
Expand Down

0 comments on commit 4adb56d

Please sign in to comment.