Adding a tests and a fix for empty strings in at_pointer (#1148)

* Adding a test.

* More tests.
This commit is contained in:
Daniel Lemire 2020-09-02 17:04:56 -04:00 committed by GitHub
parent 4d4ed92055
commit 7aea774b21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 3 deletions

View File

@ -63,7 +63,7 @@ inline size_t array::size() const noexcept {
}
inline simdjson_result<element> array::at_pointer(std::string_view json_pointer) const noexcept {
if(json_pointer[0] != '/') {
if(json_pointer.size() == 0) { // an empty string means that we return the current node
if(json_pointer.empty()) { // an empty string means that we return the current node
return element(this->tape); // copy the current node
} else { // otherwise there is an error
return INVALID_JSON_POINTER;

View File

@ -356,9 +356,10 @@ inline simdjson_result<element> element::at_pointer(std::string_view json_pointe
case internal::tape_type::START_ARRAY:
return array(tape).at_pointer(json_pointer);
default: {
if(json_pointer.empty()) { // an empty string means that we return the current node
if(!json_pointer.empty()) { // a non-empty string is invalid on an atom
return INVALID_JSON_POINTER;
}
// an empty string means that we return the current node
dom::element copy(*this);
return simdjson_result<element>(std::move(copy));
}

View File

@ -82,7 +82,7 @@ inline simdjson_result<element> object::operator[](const char *key) const noexce
}
inline simdjson_result<element> object::at_pointer(std::string_view json_pointer) const noexcept {
if(json_pointer[0] != '/') {
if(json_pointer.size() == 0) { // an empty string means that we return the current node
if(json_pointer.empty()) { // an empty string means that we return the current node
return element(this->tape); // copy the current node
} else { // otherwise there is an error
return INVALID_JSON_POINTER;

View File

@ -153,10 +153,38 @@ bool modern_support() {
#endif
return true;
}
bool issue1142() {
#if SIMDJSON_EXCEPTIONS
std::cout << "issue 1142" << std::endl;
auto example_json = R"([1,2,{"1":"bla"}])"_padded;
dom::parser parser;
dom::element example = parser.parse(example_json);
auto e0 = dom::array(example).at(0).at_pointer("");
ASSERT_EQUAL(std::string("1"), simdjson::minify(e0))
auto o = dom::array(example).at(2).at_pointer("");
ASSERT_EQUAL(std::string(R"({"1":"bla"})"), simdjson::minify(o))
std::string_view s0 = dom::array(example).at(2).at_pointer("/1").at_pointer("");
if(s0 != "bla") {
std::cerr << s0 << std::endl;
return false;
}
auto example_json2 = R"("just a string")"_padded;
dom::element example2 = parser.parse(example_json2).at_pointer("");
if(std::string_view(example2) != "just a string") {
std::cerr << std::string_view(example2) << std::endl;
return false;
}
#endif
return true;
}
int main() {
if (true
&& demo()
&& issue1142()
&& legacy_support()
&& modern_support()
&& json_pointer_success_test(TEST_RFC_JSON, "", R"({"foo":["bar","baz"],"":0,"a/b":1,"c%d":2,"e^f":3,"g|h":4,"i\\j":5,"k\"l":6," ":7,"m~n":8})")