diff --git a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp index 38f1cb0697..fc4432a88d 100644 --- a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +++ b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp @@ -190,11 +190,17 @@ namespace jsonpath { return jsoncons::jsonpath::jsonpath_expression(jsoncons::combine_allocators(), std::move(static_resources), std::move(expr)); } + template + jsonpath_expression make_expression(const typename Json::string_view_type& expr, std::error_code& ec) + { + return make_expression(jsoncons::combine_allocators(), expr, custom_functions(), ec); + } + template jsonpath_expression make_expression(const allocator_set& alloc_set, const typename Json::string_view_type& expr, std::error_code& ec) { - return make_expression(alloc_set, expr, custom_functions(), ec); + return make_expression(alloc_set, expr, custom_functions(), ec); } template @@ -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>(funcs, + auto resources = jsoncons::make_unique>(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(alloc_set, std::move(static_resources), std::move(expr)); + return jsonpath_expression(alloc_set, std::move(resources), std::move(expr)); } template diff --git a/test/jsonpath/src/jsonpath_expression_tests.cpp b/test/jsonpath/src/jsonpath_expression_tests.cpp index fb5ca4ed8d..c8a934482f 100644 --- a/test/jsonpath/src/jsonpath_expression_tests.cpp +++ b/test/jsonpath/src/jsonpath_expression_tests.cpp @@ -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("$.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") @@ -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() == "The Comedians"); - - std::cout << pretty_print(doc) << "\n"; + CHECK(doc == expected); } }