Skip to content

Commit

Permalink
Fixes #470
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 28, 2023
1 parent c266fdf commit d449d2c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
15 changes: 11 additions & 4 deletions include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,17 @@ namespace jsonpath {
return jsoncons::jsonpath::jsonpath_expression<Json>(jsoncons::combine_allocators(), std::move(static_resources), std::move(expr));
}

template <class Json>
jsonpath_expression<Json> make_expression(const typename Json::string_view_type& expr, std::error_code& ec)
{
return make_expression<Json>(jsoncons::combine_allocators(), expr, custom_functions<Json>(), ec);
}

template <class Json, class TempAllocator>
jsonpath_expression<Json> make_expression(const allocator_set<typename Json::allocator_type,TempAllocator>& alloc_set,
const typename Json::string_view_type& expr, std::error_code& ec)
{
return make_expression(alloc_set, expr, custom_functions<Json>(), ec);
return make_expression<Json>(alloc_set, expr, custom_functions<Json>(), ec);
}

template <class Json, class TempAllocator>
Expand Down Expand Up @@ -228,13 +234,14 @@ namespace jsonpath {
using value_type = typename jsonpath_traits_type::value_type;
using reference = typename jsonpath_traits_type::reference;
using evaluator_type = typename jsonpath_traits_type::evaluator_type;
using path_expression_type = typename jsonpath_traits_type::path_expression_type;

auto static_resources = jsoncons::make_unique<jsoncons::jsonpath::detail::static_resources<value_type,reference>>(funcs,
auto resources = jsoncons::make_unique<jsoncons::jsonpath::detail::static_resources<value_type,reference>>(funcs,
alloc_set.get_allocator());
evaluator_type evaluator{alloc_set.get_allocator()};
auto expr = evaluator.compile(*static_resources, path, ec);
path_expression_type expr = evaluator.compile(*resources, path, ec);

return jsoncons::jsonpath::jsonpath_expression<value_type>(alloc_set, std::move(static_resources), std::move(expr));
return jsonpath_expression<Json>(alloc_set, std::move(resources), std::move(expr));
}

template<class Json>
Expand Down
33 changes: 30 additions & 3 deletions test/jsonpath/src/jsonpath_expression_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ TEST_CASE("jsonpath make_expression::evaluate tests")
CHECK(count == 1);
CHECK(root_value == original);
}

SECTION("evaluate with std::error_code")
{
int count = 0;

const json root_value = json::parse(input);
const json original = root_value;

std::error_code ec;
auto expr = jsoncons::jsonpath::make_expression<json>("$.books[*]", ec);
CHECK_FALSE(ec);

auto op = [&](const std::string& /*location*/, const json& book)
{
if (book.at("category") == "memoir" && !book.contains("price"))
{
++count;
}
};

expr.evaluate(root_value, op);

CHECK(count == 1);
CHECK(root_value == original);
}
}

TEST_CASE("jsonpath_expression::select tests")
Expand Down Expand Up @@ -392,12 +417,14 @@ TEST_CASE("jsonpath_expression remove")
{
json doc = json::parse(input);

json expected = doc;
expected["books"].erase(expected["books"].array_range().begin()+3);
expected["books"].erase(expected["books"].array_range().begin(),expected["books"].array_range().begin()+2);

std::size_t n = jsoncons::jsonpath::remove(doc, "$.books[1,1,3,3,0,0]");

CHECK(n == 3);
REQUIRE(doc.at("books").size() == 1);
CHECK(doc.at("books")[0].at("title").as<std::string>() == "The Comedians");

std::cout << pretty_print(doc) << "\n";
CHECK(doc == expected);
}
}

0 comments on commit d449d2c

Please sign in to comment.