Skip to content

Commit

Permalink
Fixed issue with $..book[?(@ISBN)]
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 19, 2015
1 parent ff6c341 commit 5c97d43
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 40 deletions.
4 changes: 4 additions & 0 deletions src/jsoncons_ext/jsonpath/jsonpath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class jsonpath_evaluator
bool recursive_descent_;
std::vector<cjson_ptr> nodes_;
std::vector<std::shared_ptr<basic_json<Char,Alloc>>> temp_;
unsigned long column_;
unsigned long line_;

void end_nodes()
{
Expand Down Expand Up @@ -138,6 +140,8 @@ class jsonpath_evaluator
}
void evaluate(const basic_json<Char, Alloc>& root, const Char* path, size_t path_length)
{
line_ = 1;
column_ = 1;
state_ = states::start;
buffer_.clear();
start_ = 0;
Expand Down
4 changes: 0 additions & 4 deletions src/jsoncons_ext/jsonpath/jsonpath_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,6 @@ class path_expression : public expression<Char,Alloc>
if (nodes_.size() > 0)
{
result = true;
for (size_t i = 0; result && i < nodes_.size(); ++i)
{
result = expression<Char,Alloc>::evaluate_single_node(*nodes_[i]);
}
}
return result;
}
Expand Down
109 changes: 73 additions & 36 deletions test_suite/src/jsonpath_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,22 @@ BOOST_AUTO_TEST_CASE(test_jsonpath_store_book_star)

BOOST_AUTO_TEST_CASE(test_store_dotdot_price)
{
jsonpath_fixture fixture;
json root = json::parse_string(jsonpath_fixture::store_text());

json result = json_query(root,"$.store..price");
std::cout << pretty_print(result) << std::endl;

json expected = json::array();
expected.add(fixture.bicycle()["price"]);
json book_list = fixture.book();
for (size_t i = 0; i < book_list.size(); ++i)
{
expected.add(book_list[i]["price"]);
}

//std::cout << pretty_print(result) << std::endl;

BOOST_CHECK_EQUAL(expected,result);
}

BOOST_AUTO_TEST_CASE(test_jsonpath_recursive_descent)
Expand Down Expand Up @@ -150,35 +162,76 @@ BOOST_AUTO_TEST_CASE(test_jsonpath_recursive_descent)
BOOST_CHECK(result6[1] == root["store"]["book"][3]);

json result7 = json_query(root,"$..book[2:]");
std::cout << pretty_print(result7) << std::endl;
//std::cout << pretty_print(result7) << std::endl;
BOOST_CHECK(result7.size() == 2);
BOOST_CHECK(result7[0] == root["store"]["book"][2]);
BOOST_CHECK(result7[1] == root["store"]["book"][3]);
}

BOOST_AUTO_TEST_CASE(test_jsonpath_filter1)
{
jsonpath_fixture fixture;

json root = json::parse_string(jsonpath_fixture::store_text());

json result = json_query(root,"$..book[?(@.price<10)]");
std::cout << pretty_print(result) << std::endl;
//std::cout << pretty_print(result) << std::endl;
json books = fixture.book();
json expected = json::array();
for (size_t i = 0; i < books.size(); ++i)
{
double price = books[i]["price"].as<double>();
if (price < 10)
{
expected.add(books[i]);
}
}
BOOST_CHECK_EQUAL(expected,result);
}

BOOST_AUTO_TEST_CASE(test_jsonpath_filter2)
{
jsonpath_fixture fixture;

json root = json::parse_string(jsonpath_fixture::store_text());

json result = json_query(root,"$..book[?(10 > @.price)]");
std::cout << pretty_print(result) << std::endl;

//std::cout << pretty_print(result) << std::endl;
json books = fixture.book();
json expected = json::array();
for (size_t i = 0; i < books.size(); ++i)
{
double price = books[i]["price"].as<double>();
if (10 > price)
{
expected.add(books[i]);
}
}
BOOST_CHECK_EQUAL(expected,result);
}


BOOST_AUTO_TEST_CASE(test_jsonpath_filter_category_eq_reference)
{
jsonpath_fixture fixture;

json root = json::parse_string(jsonpath_fixture::store_text());

json result = json_query(root,"$..book[?(@.category == 'reference')]");
std::cout << pretty_print(result) << std::endl;

//std::cout << pretty_print(result) << std::endl;
json books = fixture.book();
json expected = json::array();
for (size_t i = 0; i < books.size(); ++i)
{
double price = books[i]["price"].as<double>();
if (books[i]["category"].as<std::string>() == "reference")
{
expected.add(books[i]);
}
}
BOOST_CHECK_EQUAL(expected,result);
}

BOOST_AUTO_TEST_CASE(test_jsonpath_filter3)
Expand Down Expand Up @@ -221,28 +274,12 @@ BOOST_AUTO_TEST_CASE(test_jsonpath_book_isbn)
if (has_isbn)
{
json result = json_query(books[i],"@.isbn");
std::cout << pretty_print(result) << std::endl;
json expected = json::array();
expected.add(books[i]["isbn"]);
BOOST_CHECK_EQUAL(expected, result);
//std::cout << pretty_print(result) << std::endl;
}
}


//json result = json_query(root,"$..book[?(@.isbn)]");

//json books = fixture.book();

//json expected = json::array();
//for (size_t i = 0; i < books.size(); ++i)
//{
// double price = books[i]["price"].as<double>();
// if (price > 8 && price < 12)
// {
// expected.add(books[i]);
// }
//}
//std::cout << pretty_print(result) << std::endl;
//std::cout << pretty_print(expected) << std::endl;

//BOOST_CHECK_EQUAL(expected,result);
}

BOOST_AUTO_TEST_CASE(test_jsonpath_filter4)
Expand All @@ -255,19 +292,18 @@ BOOST_AUTO_TEST_CASE(test_jsonpath_filter4)

json books = fixture.book();

//json expected = json::array();
//for (size_t i = 0; i < books.size(); ++i)
//{
// double price = books[i]["price"].as<double>();
// if (price > 8 && price < 12)
// {
// expected.add(books[i]);
// }
//}
std::cout << pretty_print(result) << std::endl;
json expected = json::array();
for (size_t i = 0; i < books.size(); ++i)
{
if (books[i].has_member("isbn"))
{
expected.add(books[i]);
}
}
//std::cout << pretty_print(result) << std::endl;
//std::cout << pretty_print(expected) << std::endl;

//BOOST_CHECK_EQUAL(expected,result);
BOOST_CHECK_EQUAL(expected,result);
}

BOOST_AUTO_TEST_CASE(test_jsonpath_array_length)
Expand Down Expand Up @@ -480,3 +516,4 @@ BOOST_AUTO_TEST_CASE(test_jsonpath_everything_in_store)




0 comments on commit 5c97d43

Please sign in to comment.