CharReader: Add StructuredError by martinduffy1 · Pull Request #1409 · open-source-parsers/jsoncpp (original) (raw)

Can be written more economically, but I think the translation can be made unnecessary.

std::vectorCharReader::StructuredError getStructuredErrors() const override {
std::vectorOurReader::StructuredError errors = reader_.getStructuredErrors();
std::vectorCharReader::StructuredError out_errors;
std::transform(errors.begin(), errors.end(), std::back_inserter(out_errors),
[](OurReader::StructuredError x) {
CharReader::StructuredError y;
y.offset_start = x.offset_start;
y.offset_limit = x.offset_limit;
y.message = x.message;
return y;
});
return out_errors;
}
std::vectorCharReader::StructuredError getStructuredErrors() const override {
std::vectorCharReader::StructuredError out;
auto in = reader_.getStructuredErrors();
out.reserve(in.size());
for (auto&& x : in)
out.push_back({x.offset_start, x.offset_limit, std::move(x.message)});
return out;
}

But I don't see the need for the extra translation step.

There doesn't need to be a separate type for OurReader::StructuredError now that there's a CharReader::StructuredError. This OurReader is a private detail of this .cpp file, and its only purpose is to serve as an implementation detail of OurCharReader. It can be made to produce CharReader::StructuredError structs directly.

A great thing about OurReader is that it's 100% an implementation detail of the .cpp file and we can change it without worrying about API breaks. I didn't realize this before, which wasted your time perhaps. Apologies.
I feel like the Our stuff makes the library pretty confusing to work on and we should just make clean break from the deprecated Reader by putting it in another file where it can't bother us anymore. :)