Skip to content

Commit

Permalink
to_basic_string
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 17, 2023
1 parent 39e9a74 commit cc3953d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 45 deletions.
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonpath/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3053,7 +3053,7 @@ namespace detail {
{
auto callback = [&result](const path_node_type& pathp, reference)
{
result.emplace_back(to_jsonpath(pathp));
result.emplace_back(to_basic_string(pathp));
};
evaluate(resources, root, path, instance, callback, options);
}
Expand Down
43 changes: 42 additions & 1 deletion include/jsoncons_ext/jsonpath/json_location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>
#include <vector>
#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_utilities.hpp>

namespace jsoncons {
namespace jsonpath {
Expand Down Expand Up @@ -89,7 +90,7 @@ namespace jsonpath {
}
};

template <class CharT, class Allocator = std::allocator<char>>
template <class CharT, class Allocator = std::allocator<CharT>>
class basic_json_location
{
public:
Expand Down Expand Up @@ -330,11 +331,51 @@ namespace jsonpath {
return found ? p_current : nullptr;
}

template <class CharT, class Allocator = std::allocator<CharT>>
std::basic_string<CharT, std::char_traits<CharT>, Allocator> to_basic_string(const basic_json_location<CharT,Allocator>& location,
const Allocator& alloc = Allocator())
{
std::basic_string<CharT, std::char_traits<CharT>, Allocator> buffer(alloc);

buffer.push_back('$');
for (const auto& element : location)
{
if (element.has_name())
{
buffer.push_back('[');
buffer.push_back('\'');
jsoncons::jsonpath::escape_string(element.name().data(), element.name().size(), buffer);
buffer.push_back('\'');
buffer.push_back(']');
}
else
{
buffer.push_back('[');
jsoncons::detail::from_integer(element.index(), buffer);
buffer.push_back(']');
}
}

return buffer;
}

using json_location = basic_json_location<char>;
using wjson_location = basic_json_location<wchar_t>;
using path_element = basic_path_element<char,std::allocator<char>>;
using wpath_element = basic_path_element<wchar_t,std::allocator<char>>;

inline
std::string to_string(const json_location& location)
{
return to_basic_string(location);
}

inline
std::wstring to_wstring(const wjson_location& location)
{
return to_basic_string(location);
}

} // namespace jsonpath
} // namespace jsoncons

Expand Down
4 changes: 2 additions & 2 deletions include/jsoncons_ext/jsonpath/json_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace jsonpath {

auto f = [&callback](const path_node_type& path, reference val)
{
callback(to_jsonpath(path), val);
callback(to_basic_string(path), val);
};
expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, f);
}
Expand All @@ -157,7 +157,7 @@ namespace jsonpath {

auto f = [&callback](const path_node_type& path, reference val)
{
callback(to_jsonpath(path), val);
callback(to_basic_string(path), val);
};
expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, f);
}
Expand Down
6 changes: 3 additions & 3 deletions include/jsoncons_ext/jsonpath/jsonpath_expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace jsonpath {
value_type result(json_array_arg, semantic_tag::none, alloc_);
auto callback = [&result](const path_node_type& p, reference)
{
result.emplace_back(to_jsonpath(p));
result.emplace_back(to_basic_string(p));
};
expr_.evaluate(resources, instance, path_node_type(), instance, callback, options);
return result;
Expand Down Expand Up @@ -133,7 +133,7 @@ namespace jsonpath {
};

template <class Json>
auto make_jsonpath_expr(const typename Json::string_view_type& path,
auto compile_jsonpath(const typename Json::string_view_type& path,
const jsoncons::jsonpath::custom_functions<typename jsonpath_traits<Json>::value_type>& funcs = jsoncons::jsonpath::custom_functions<typename jsonpath_traits<Json>::value_type>())
{
using jsonpath_traits_type = jsoncons::jsonpath::jsonpath_traits<Json>;
Expand All @@ -150,7 +150,7 @@ namespace jsonpath {
}

template <class Json, class TempAllocator>
auto make_jsonpath_expr(const allocator_set<typename Json::allocator_type,TempAllocator>& alloc_set,
auto compile_jsonpath(const allocator_set<typename Json::allocator_type,TempAllocator>& alloc_set,
const typename Json::string_view_type& path,
const jsoncons::jsonpath::custom_functions<typename jsonpath_traits<Json>::value_type>& funcs, std::error_code& ec)
{
Expand Down
4 changes: 2 additions & 2 deletions include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2543,7 +2543,7 @@ namespace detail {
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};
auto f = [&callback](const path_node_type& path, reference val)
{
callback(to_jsonpath(path), val);
callback(to_basic_string(path), val);
};
expr_.evaluate(resources, instance, path_node_type{}, instance, f, options);
}
Expand All @@ -2557,7 +2557,7 @@ namespace detail {
Json result(json_array_arg, semantic_tag::none, alloc_);
auto callback = [&result](const path_node_type& path, reference)
{
result.emplace_back(to_jsonpath(path));
result.emplace_back(to_basic_string(path));
};
expr_.evaluate(resources, instance, path_node_type(), instance, callback, options);
return result;
Expand Down
43 changes: 14 additions & 29 deletions include/jsoncons_ext/jsonpath/path_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ namespace jsonpath {
}

template <class CharT, class Allocator=std::allocator<CharT>>
std::basic_string<CharT,std::char_traits<CharT>,Allocator> to_jsonpath(const basic_path_node<CharT>& path, const Allocator& alloc=Allocator())
std::basic_string<CharT,std::char_traits<CharT>,Allocator> to_basic_string(const basic_path_node<CharT>& path, const Allocator& alloc=Allocator())
{
std::basic_string<CharT,std::char_traits<CharT>,Allocator> buffer(alloc);

Expand Down Expand Up @@ -319,34 +319,6 @@ namespace jsonpath {
return buffer;
}

template <class CharT, class Allocator = std::allocator<CharT>>
std::basic_string<CharT, std::char_traits<CharT>, Allocator> to_jsonpath(const basic_json_location<CharT,Allocator>& location,
const Allocator& alloc = Allocator())
{
std::basic_string<CharT, std::char_traits<CharT>, Allocator> buffer(alloc);

buffer.push_back('$');
for (const auto& element : location)
{
if (element.has_name())
{
buffer.push_back('[');
buffer.push_back('\'');
jsoncons::jsonpath::escape_string(element.name().data(), element.name().size(), buffer);
buffer.push_back('\'');
buffer.push_back(']');
}
else
{
buffer.push_back('[');
jsoncons::detail::from_integer(element.index(), buffer);
buffer.push_back(']');
}
}

return buffer;
}

template <class CharT, class Allocator=std::allocator<char>>
basic_json_location<CharT,Allocator> to_json_location(const basic_path_node<CharT>& path, const Allocator& alloc=Allocator())
{
Expand Down Expand Up @@ -382,9 +354,22 @@ namespace jsonpath {
return location;
}


using path_node = basic_path_node<char>;
using wpath_node = basic_path_node<wchar_t>;

inline
std::string to_string(const path_node& path)
{
return to_basic_string(path);
}

inline
std::wstring to_wstring(const wpath_node& path)
{
return to_basic_string(path);
}

} // namespace jsonpath
} // namespace jsoncons

Expand Down
4 changes: 2 additions & 2 deletions test/jsonpath/src/jsonpath_make_expression_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ TEST_CASE("jsonpath make_expression test")

const json doc = json::parse(input);

auto expr = jsoncons::jsonpath::make_jsonpath_expr<const json>("$.books[*]");
auto expr = jsoncons::jsonpath::compile_jsonpath<const json>("$.books[*]");

auto callback = [&](const jsonpath::path_node& /*location*/, const json& book)
{
Expand All @@ -80,7 +80,7 @@ TEST_CASE("jsonpath make_expression test")

json doc = json::parse(input);

auto expr = jsoncons::jsonpath::make_jsonpath_expr<json>("$.books[*]");
auto expr = jsoncons::jsonpath::compile_jsonpath<json>("$.books[*]");

auto callback1 = [&](const jsonpath::path_node& /*location*/, const json& book)
{
Expand Down
4 changes: 2 additions & 2 deletions test/jsonpath/src/jsonpath_select_paths_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ TEST_CASE("jsonpath.jsonpath select_paths test")

SECTION("test 1")
{
auto expr = jsonpath::make_jsonpath_expr<json>("$..book[?(@.category == 'fiction')].title");
auto expr = jsonpath::compile_jsonpath<json>("$..book[?(@.category == 'fiction')].title");
auto result = expr.select_paths(doc);

for (const auto& loc : result)
{
std::string s = jsonpath::to_jsonpath(loc);
std::string s = jsonpath::to_string(loc);
std::cout << s << std::endl;
}

Expand Down
6 changes: 3 additions & 3 deletions test/jsonpath/src/path_node_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST_CASE("test json_location equals")
jsonpath::path_node b4(&b3,0);

CHECK((a4 == b4));
CHECK((jsonpath::to_jsonpath(a4) == std::string("$['foo']['bar'][0]")));
CHECK((jsonpath::to_string(a4) == std::string("$['foo']['bar'][0]")));
}

TEST_CASE("test json_location with solidus to_string")
Expand All @@ -34,7 +34,7 @@ TEST_CASE("test json_location with solidus to_string")
jsonpath::path_node a3(&a2,"bar");
jsonpath::path_node a4(&a3,0);

CHECK(jsonpath::to_jsonpath(a4) == std::string(R"($['foo\'s']['bar'][0])"));
CHECK(jsonpath::to_string(a4) == std::string(R"($['foo\'s']['bar'][0])"));
}

TEST_CASE("test path_node less")
Expand Down Expand Up @@ -135,7 +135,7 @@ TEST_CASE("test to_json_location")
std::string jsonpath_string = "$['foo']['bar'][7]";

CHECK((jsonpath::to_json_location(a4) == location));
CHECK((jsonpath::to_jsonpath(location) == jsonpath_string));
CHECK((jsonpath::to_string(location) == jsonpath_string));
}
}

0 comments on commit cc3953d

Please sign in to comment.