Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add makeCharReader function to deprecated newCharReader #1424

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/jsoncpp.dox
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ CharReaders and StreamWriters are not thread-safe, but they are re-usable.
\code
Json::CharReaderBuilder rbuilder;
cfg >> rbuilder.settings_;
std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader());
std::unique_ptr<Json::CharReader> const reader = rbuilder.newCharReader();
reader->parse(start, stop, &value1, &errs);
// ...
reader->parse(start, stop, &value2, &errs);
Expand Down
2 changes: 1 addition & 1 deletion example/readFromString/readFromString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main() {
reader.parse(rawJson, root);
} else {
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
const std::unique_ptr<Json::CharReader> reader = builder.makeCharReader();
if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
&err)) {
std::cout << "error" << std::endl;
Expand Down
3 changes: 2 additions & 1 deletion include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class JSON_API CharReader {
* \throw std::exception if something goes wrong (e.g. invalid settings)
*/
virtual CharReader* newCharReader() const = 0;
virtual std::unique_ptr<CharReader> makeCharReader() const = 0;
AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
}; // Factory
}; // CharReader

Expand Down Expand Up @@ -338,7 +339,7 @@ class JSON_API CharReaderBuilder : public CharReader::Factory {
~CharReaderBuilder() override;

CharReader* newCharReader() const override;

AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
std::unique_ptr<CharReader> makeCharReader() const override;
/** \return true if 'settings' are legal and consistent;
* otherwise, indicate bad settings via 'invalid'.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/jsontestrunner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static int parseAndSaveValueTree(const Json::String& input,
features.allowDroppedNullPlaceholders_;
builder.settings_["allowNumericKeys"] = features.allowNumericKeys_;

std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
std::unique_ptr<Json::CharReader> reader = builder.makeCharReader();
AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
Json::String errors;
const bool parsingSuccessful =
reader->parse(input.data(), input.data() + input.size(), root, &errors);
Expand Down
13 changes: 10 additions & 3 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ bool Reader::decodeDouble(Token& token, Value& decoded) {
value = -std::numeric_limits<double>::infinity();
else if (!std::isinf(value))
return addError(
"'" + String(token.start_, token.end_) + "' is not a number.", token);
"'" + String(token.start_, token.end_) + "' is not a number.", token);
AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
}
decoded = value;
return true;
Expand Down Expand Up @@ -1660,7 +1660,7 @@ bool OurReader::decodeDouble(Token& token, Value& decoded) {
value = -std::numeric_limits<double>::infinity();
else if (!std::isinf(value))
return addError(
"'" + String(token.start_, token.end_) + "' is not a number.", token);
"'" + String(token.start_, token.end_) + "' is not a number.", token);
AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
}
decoded = value;
return true;
Expand Down Expand Up @@ -1891,6 +1891,13 @@ class OurCharReader : public CharReader {

CharReaderBuilder::CharReaderBuilder() { setDefaults(&settings_); }
CharReaderBuilder::~CharReaderBuilder() = default;
std::unique_ptr<CharReader> CharReaderBuilder::makeCharReader() const {
return std::unique_ptr<CharReader>(newCharReader());
}

AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
/*
* \deprecated Use makeCharReader.
AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
*/
CharReader* CharReaderBuilder::newCharReader() const {
bool collectComments = settings_["collectComments"].asBool();
OurFeatures features = OurFeatures::all();
Expand Down Expand Up @@ -1987,7 +1994,7 @@ bool parseFromStream(CharReader::Factory const& fact, IStream& sin, Value* root,
char const* begin = doc.data();
char const* end = begin + doc.size();
// Note that we do not actually need a null-terminator.
CharReaderPtr const reader(fact.newCharReader());
CharReaderPtr const reader = fact.makeCharReader();
return reader->parse(begin, end, root, errs);
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib_json/json_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ String valueToString(double value, bool useSpecialFloats,
if (!isfinite(value)) {
static const char* const reps[2][3] = {{"NaN", "-Infinity", "Infinity"},
{"null", "-1e+9999", "1e+9999"}};
return reps[useSpecialFloats ? 0 : 1]
[isnan(value) ? 0 : (value < 0) ? 1 : 2];
return reps[useSpecialFloats ? 0 : 1][isnan(value) ? 0
AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
: (value < 0) ? 1
: 2];
}

String buffer(size_t(36), '\0');
Expand Down
2 changes: 1 addition & 1 deletion src/test_lib_json/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
builder.settings_["collectComments"] = hash_settings & (1 << 9);
builder.settings_["allowTrailingCommas_"] = hash_settings & (1 << 10);

std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
std::unique_ptr<Json::CharReader> reader = builder.makeCharReader();

Json::Value root;
const auto data_str = reinterpret_cast<const char*>(data);
Expand Down
60 changes: 30 additions & 30 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2994,7 +2994,7 @@ struct CharReaderTest : JsonTest::TestCase {};

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrors) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
Json::Value root;
char const doc[] = R"({ "property" : "value" })";
Expand All @@ -3005,7 +3005,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrors) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrorsTestingOffsets) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
Json::Value root;
char const doc[] = "{ \"property\" : [\"value\", \"value2\"], \"obj\" : "
Expand All @@ -3018,7 +3018,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrorsTestingOffsets) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseNumber) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
Json::Value root;
{
Expand All @@ -3034,7 +3034,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseNumber) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseString) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::Value root;
Json::String errs;
{
Expand Down Expand Up @@ -3090,7 +3090,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseString) {
}
{
b.settings_["allowSingleQuotes"] = true;
CharReaderPtr charreader(b.newCharReader());
CharReaderPtr charreader = b.makeCharReader();
char const doc[] = R"({'a': 'x\ty', "b":'x\\y'})";
bool ok = charreader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
Expand All @@ -3103,7 +3103,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseString) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseComment) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::Value root;
Json::String errs;
{
Expand Down Expand Up @@ -3134,7 +3134,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseComment) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseObjectWithErrors) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::Value root;
Json::String errs;
{
Expand All @@ -3157,7 +3157,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseObjectWithErrors) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseArrayWithErrors) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::Value root;
Json::String errs;
{
Expand All @@ -3180,7 +3180,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseArrayWithErrors) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithOneError) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
Json::Value root;
char const doc[] = R"({ "property" :: "value" })";
Expand All @@ -3193,7 +3193,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithOneError) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseChineseWithOneError) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
Json::Value root;
char const doc[] = "{ \"pr佐藤erty\" :: \"value\" }";
Expand All @@ -3206,7 +3206,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseChineseWithOneError) {

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithDetailError) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
Json::Value root;
char const doc[] = R"({ "property" : "v\alue" })";
Expand All @@ -3223,7 +3223,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) {
char const doc[] = R"({ "property" : "value" })";
{
b.settings_["stackLimit"] = 2;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
Expand All @@ -3232,7 +3232,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) {
}
{
b.settings_["stackLimit"] = 1;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
JSONTEST_ASSERT_THROWS(
reader->parse(doc, doc + std::strlen(doc), &root, &errs));
Expand All @@ -3256,7 +3256,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderStrictModeTest, dupKeys) {
R"({ "property" : "value", "key" : "val1", "key" : "val2" })";
{
b.strictMode(&b.settings_);
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(!ok);
Expand All @@ -3275,7 +3275,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue164) {
char const doc[] = R"( "property" : "value" })";
{
b.settings_["failIfExtra"] = false;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
Expand All @@ -3284,7 +3284,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue164) {
}
{
b.settings_["failIfExtra"] = true;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(!ok);
Expand All @@ -3295,7 +3295,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue164) {
}
{
b.strictMode(&b.settings_);
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(!ok);
Expand All @@ -3307,7 +3307,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue164) {
{
b.strictMode(&b.settings_);
b.settings_["failIfExtra"] = false;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(!ok);
Expand All @@ -3325,7 +3325,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, issue107) {
Json::Value root;
char const doc[] = "1:2:3";
b.settings_["failIfExtra"] = true;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(!ok);
Expand All @@ -3340,7 +3340,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterObject) {
{
char const doc[] = "{ \"property\" : \"value\" } //trailing\n//comment\n";
b.settings_["failIfExtra"] = true;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
Expand All @@ -3353,7 +3353,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterArray) {
Json::Value root;
char const doc[] = "[ \"property\" , \"value\" ] //trailing\n//comment\n";
b.settings_["failIfExtra"] = true;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
Expand All @@ -3365,7 +3365,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterBool) {
Json::Value root;
char const doc[] = " true /*trailing\ncomment*/";
b.settings_["failIfExtra"] = true;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::String errs;
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
Expand All @@ -3376,7 +3376,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, commentAfterBool) {
JSONTEST_FIXTURE_LOCAL(CharReaderFailIfExtraTest, parseComment) {
Json::CharReaderBuilder b;
b.settings_["failIfExtra"] = true;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::Value root;
Json::String errs;
{
Expand Down Expand Up @@ -3454,7 +3454,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderAllowDropNullTest, issue178) {
for (const auto& spec : specs) {
Json::CharReaderBuilder b;
b.settings_["allowDroppedNullPlaceholders"] = true;
std::unique_ptr<Json::CharReader> reader(b.newCharReader());
std::unique_ptr<Json::CharReader> reader = b.makeCharReader();

Json::Value root;
Json::String errs;
Expand All @@ -3475,7 +3475,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderAllowNumericKeysTest, allowNumericKeys) {
b.settings_["allowNumericKeys"] = true;
Json::Value root;
Json::String errs;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
char const doc[] = "{15:true,-16:true,12.01:true}";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
JSONTEST_ASSERT(ok);
Expand All @@ -3493,7 +3493,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderAllowSingleQuotesTest, issue182) {
b.settings_["allowSingleQuotes"] = true;
Json::Value root;
Json::String errs;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
{
char const doc[] = "{'a':true,\"b\":true}";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
Expand Down Expand Up @@ -3521,7 +3521,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderAllowZeroesTest, issue176) {
b.settings_["allowSingleQuotes"] = true;
Json::Value root;
Json::String errs;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
{
char const doc[] = "{'a':true,\"b\":true}";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
Expand All @@ -3546,7 +3546,7 @@ struct CharReaderAllowSpecialFloatsTest : JsonTest::TestCase {};

JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest, specialFloat) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
Json::Value root;
Json::String errs;
{
Expand Down Expand Up @@ -3574,7 +3574,7 @@ JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest, issue209) {
b.settings_["allowSpecialFloats"] = true;
Json::Value root;
Json::String errs;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
{
char const doc[] = R"({"a":NaN,"b":Infinity,"c":-Infinity,"d":+Infinity})";
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
Expand Down Expand Up @@ -3655,7 +3655,7 @@ JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, readerParseEscapeSequence) {

JSONTEST_FIXTURE_LOCAL(EscapeSequenceTest, charReaderParseEscapeSequence) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
CharReaderPtr reader = b.makeCharReader();
AssafPassalCG marked this conversation as resolved.
Show resolved Hide resolved
Json::Value root;
Json::String errs;
char const doc[] = "[\"\\\"\",\"\\/\",\"\\\\\",\"\\b\","
Expand Down