diff --git a/doc/jsoncpp.dox b/doc/jsoncpp.dox index f340591fd..381fb9185 100644 --- a/doc/jsoncpp.dox +++ b/doc/jsoncpp.dox @@ -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 const reader(rbuilder.newCharReader()); +std::unique_ptr const reader = rbuilder.makeCharReader(); reader->parse(start, stop, &value1, &errs); // ... reader->parse(start, stop, &value2, &errs); diff --git a/include/json/reader.h b/include/json/reader.h index d745378fc..9df30511f 100644 --- a/include/json/reader.h +++ b/include/json/reader.h @@ -282,6 +282,9 @@ class JSON_API CharReader { * \throw std::exception if something goes wrong (e.g. invalid settings) */ virtual CharReader* newCharReader() const = 0; + + /** Wrap newCharReader's result in a std::unique_ptr. Throws if newCharReader throws. */ + std::unique_ptr makeCharReader() const; }; // Factory protected: @@ -362,6 +365,9 @@ class JSON_API CharReaderBuilder : public CharReader::Factory { CharReaderBuilder(); ~CharReaderBuilder() override; + /* + * \deprecated Use makeCharReader. + */ CharReader* newCharReader() const override; /** \return true if 'settings' are legal and consistent; diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 10c97aecb..717549305 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -1842,6 +1842,10 @@ OurReader::getStructuredErrors() const { return allErrors; } +std::unique_ptr CharReader::Factory::makeCharReader() const { + return std::unique_ptr(newCharReader()); +} + class OurCharReader : public CharReader { public: diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 5a0ce01ce..5c98ad42c 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3009,6 +3009,19 @@ JSONTEST_FIXTURE_LOCAL(ReaderTest, allowNumericKeysTest) { checkParse(R"({ 123 : "abc" })"); } +struct UniqueReaderTest : JsonTest::TestCase {}; + +JSONTEST_FIXTURE_LOCAL(UniqueReaderTest, parseWithNoErrors) { + Json::CharReaderBuilder b; + CharReaderPtr reader = b.makeCharReader(); + Json::String errs; + Json::Value root; + char const doc[] = R"({ "property" : "value" })"; + bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs); + JSONTEST_ASSERT(ok); + JSONTEST_ASSERT(errs.empty()); +} + struct CharReaderTest : JsonTest::TestCase {}; JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithNoErrors) {