Skip to content

CharReader: Add StructuredError #1281

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions include/json/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ class JSON_API Reader {
*/
class JSON_API CharReader {
public:
class JSON_API StructuredError {
public:
ptrdiff_t offset_start;
ptrdiff_t offset_limit;
String message;
};

virtual ~CharReader() = default;
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
* document. The document must be a UTF-8 encoded string containing the
Expand All @@ -263,6 +270,12 @@ class JSON_API CharReader {
virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
String* errs) = 0;

/** \brief Get a list of structured error messages from parsing the document.
*
* \return list of error messages.
*/
virtual std::vector<StructuredError> getStructuredErrors() const = 0;

class JSON_API Factory {
public:
virtual ~Factory() = default;
Expand Down
18 changes: 9 additions & 9 deletions src/lib_json/json_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,17 +890,12 @@ class OurReader {
public:
using Char = char;
using Location = const Char*;
struct StructuredError {
ptrdiff_t offset_start;
ptrdiff_t offset_limit;
String message;
};

explicit OurReader(OurFeatures const& features);
bool parse(const char* beginDoc, const char* endDoc, Value& root,
bool collectComments = true);
String getFormattedErrorMessages() const;
std::vector<StructuredError> getStructuredErrors() const;
std::vector<CharReader::StructuredError> getStructuredErrors() const;

private:
OurReader(OurReader const&); // no impl
Expand Down Expand Up @@ -1860,10 +1855,11 @@ String OurReader::getFormattedErrorMessages() const {
return formattedMessage;
}

std::vector<OurReader::StructuredError> OurReader::getStructuredErrors() const {
std::vector<OurReader::StructuredError> allErrors;
std::vector<CharReader::StructuredError>
OurReader::getStructuredErrors() const {
std::vector<CharReader::StructuredError> allErrors;
for (const auto& error : errors_) {
OurReader::StructuredError structured;
CharReader::StructuredError structured;
structured.offset_start = error.token_.start_ - begin_;
structured.offset_limit = error.token_.end_ - begin_;
structured.message = error.message_;
Expand All @@ -1887,6 +1883,10 @@ class OurCharReader : public CharReader {
}
return ok;
}

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

CharReaderBuilder::CharReaderBuilder() { setDefaults(&settings_); }
Expand Down
37 changes: 37 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3903,6 +3903,43 @@ JSONTEST_FIXTURE_LOCAL(FuzzTest, fuzzDoesntCrash) {
example.size()));
}

struct ParseWithStructuredErrorsTest : JsonTest::TestCase {
void
testErrors(const std::string& doc, bool success,
const std::vector<Json::CharReader::StructuredError>& errors) {
Json::CharReaderBuilder b;
CharReaderPtr reader(b.newCharReader());
Json::Value root;
JSONTEST_ASSERT_EQUAL(
success,
reader->parse(doc.data(), doc.data() + doc.length(), &root, nullptr));
auto actualErrors = reader->getStructuredErrors();
JSONTEST_ASSERT_EQUAL(errors.size(), actualErrors.size());
for (std::size_t i = 0; i < errors.size() && i < actualErrors.size(); i++) {
JSONTEST_ASSERT_EQUAL(errors[i].offset_start,
actualErrors[i].offset_start);
JSONTEST_ASSERT_EQUAL(errors[i].offset_limit,
actualErrors[i].offset_limit);
JSONTEST_ASSERT_STRING_EQUAL(errors[i].message, actualErrors[i].message);
}
}
};

JSONTEST_FIXTURE_LOCAL(ParseWithStructuredErrorsTest, success) {
testErrors("{}", true, {});
}

JSONTEST_FIXTURE_LOCAL(ParseWithStructuredErrorsTest, singleError) {
testErrors("{ 1 : 2 }", false,
{
{
/*offset_start=*/2,
/*offset_limit=*/3,
/*message=*/"Missing '}' or object member name",
},
});
}

int main(int argc, const char* argv[]) {
JsonTest::Runner runner;

Expand Down