Skip to content

CharReader: Add StructuredError #1409

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

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
run clang format
  • Loading branch information
baylesj committed Sep 10, 2024
commit 2ff6cbcb97e32e596ad7510d87b6ccb451a9cdca
14 changes: 7 additions & 7 deletions include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class JSON_API CharReader {

/** \brief Returns a vector of structured errors encountered while parsing.
* Each parse call resets the stored list of errors.
*/
*/
std::vector<StructuredError> getStructuredErrors() const;

class JSON_API Factory {
Expand All @@ -287,17 +287,17 @@ class JSON_API CharReader {
protected:
class Impl {
public:
virtual ~Impl() = default;
virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
String* errs) = 0;
virtual std::vector<StructuredError> getStructuredErrors() const = 0;
virtual ~Impl() = default;
virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
String* errs) = 0;
virtual std::vector<StructuredError> getStructuredErrors() const = 0;
};

explicit CharReader(std::unique_ptr<Impl> impl) : _impl(std::move(impl)) { }
explicit CharReader(std::unique_ptr<Impl> impl) : _impl(std::move(impl)) {}

private:
std::unique_ptr<Impl> _impl;
}; // CharReader
}; // CharReader

/** \brief Build a CharReader implementation.
*
Expand Down
16 changes: 10 additions & 6 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,8 @@ String OurReader::getFormattedErrorMessages() const {
return formattedMessage;
}

std::vector<CharReader::StructuredError> OurReader::getStructuredErrors() const {
std::vector<CharReader::StructuredError>
OurReader::getStructuredErrors() const {
std::vector<CharReader::StructuredError> allErrors;
for (const auto& error : errors_) {
CharReader::StructuredError structured;
Expand All @@ -1847,7 +1848,8 @@ class OurCharReader : public CharReader {

public:
OurCharReader(bool collectComments, OurFeatures const& features)
: CharReader(std::unique_ptr<OurImpl>(new OurImpl(collectComments, features))) {}
: CharReader(
std::unique_ptr<OurImpl>(new OurImpl(collectComments, features))) {}

protected:
class OurImpl : public Impl {
Expand All @@ -1864,7 +1866,8 @@ class OurCharReader : public CharReader {
return ok;
}

std::vector<CharReader::StructuredError> getStructuredErrors() const override {
std::vector<CharReader::StructuredError>
getStructuredErrors() const override {
return reader_.getStructuredErrors();
}

Expand Down Expand Up @@ -1961,13 +1964,14 @@ void CharReaderBuilder::setDefaults(Json::Value* settings) {
//! [CharReaderBuilderDefaults]
}

std::vector<CharReader::StructuredError> CharReader::getStructuredErrors() const {
return _impl->getStructuredErrors();
std::vector<CharReader::StructuredError>
CharReader::getStructuredErrors() const {
return _impl->getStructuredErrors();
}

bool CharReader::parse(char const* beginDoc, char const* endDoc, Value* root,
String* errs) {
return _impl->parse(beginDoc, endDoc, root, errs);
return _impl->parse(beginDoc, endDoc, root, errs);
}

//////////////////////////////////
Expand Down
5 changes: 3 additions & 2 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3918,8 +3918,9 @@ JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) {
}

struct ParseWithStructuredErrorsTest : JsonTest::TestCase {
void testErrors(const std::string& doc, bool success,
const std::vector<Json::CharReader::StructuredError>& expectedErrors) {
void testErrors(
const std::string& doc, bool success,
const std::vector<Json::CharReader::StructuredError>& expectedErrors) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
Json::Value root;
Expand Down
Loading