diff --git a/doc/basics.md b/doc/basics.md index 80022ca41..4266bab70 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -1110,6 +1110,8 @@ for (auto val : doc) { std::cout << doc.current_location() << std::endl; // Throws OUT_OF_BOUNDS ``` +Conversely, if `doc.current_location()` succeeds, then the document has more content. + Finally, the `current_location()` method may also be used even when no exceptions/errors are thrown. This can be helpful for users that want to know the current state of iteration during parsing. For example: diff --git a/tools/json2json.cpp b/tools/json2json.cpp index aa4aea023..949aeb8d9 100644 --- a/tools/json2json.cpp +++ b/tools/json2json.cpp @@ -64,6 +64,16 @@ int main(int argc, const char *argv[]) { error = parser.iterate(docdata).get(doc); if(error != simdjson::SUCCESS) { std::cout << error << std::endl; return EXIT_FAILURE; } std::cout << doc; + // check if there is more content + const char * endofstream; + if(doc.current_location().get(endofstream) == simdjson::SUCCESS) { + // there is more content ! + // let us find what it is: + size_t len = docdata.data() + docdata.size() - endofstream; + std::string_view content{endofstream, len}; + std::cerr << "\nThere is leftover content: '" << content << "'" << std::endl; + return EXIT_FAILURE; + } return EXIT_SUCCESS; } simdjson::dom::parser parser;