Skip to content

Commit

Permalink
json_location
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 16, 2023
1 parent 81777b5 commit ccb5698
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
1 change: 0 additions & 1 deletion include/jsoncons/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <jsoncons/json_traits_macros.hpp>
#include <jsoncons/json_traits_macros_deprecated.hpp>
#include <jsoncons/staj_iterator.hpp>
#include <jsoncons/json_location.hpp>

#endif

Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

// See https://github.com/danielaparker/jsoncons for latest version

#ifndef JSONCONS_JSON_LOCATION_HPP
#define JSONCONS_JSON_LOCATION_HPP
#ifndef JSONCONS_JSONPATH_JSON_LOCATION_HPP
#define JSONCONS_JSONPATH_JSON_LOCATION_HPP

#include <string>
#include <vector>
#include <jsoncons/config/jsoncons_config.hpp>

namespace jsoncons {
namespace jsonpath {

template <class CharT,class Allocator>
class basic_path_element
Expand Down Expand Up @@ -278,7 +279,7 @@ namespace jsoncons {
}

template<class Json>
Json* json_select(Json& instance, const basic_json_location<typename Json::char_type>& location)
Json* json_get(Json& instance, const basic_json_location<typename Json::char_type>& location)
{
Json* p_current = std::addressof(instance);
bool found = false;
Expand Down Expand Up @@ -334,6 +335,7 @@ namespace jsoncons {
using path_element = basic_path_element<char,std::allocator<char>>;
using wpath_element = basic_path_element<wchar_t,std::allocator<char>>;

} // namespace jsonpath
} // namespace jsoncons

#endif
1 change: 1 addition & 0 deletions include/jsoncons_ext/jsonpath/jsonpath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#include <jsoncons_ext/jsonpath/json_query.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_expr.hpp>
#include <jsoncons_ext/jsonpath/flatten.hpp>
#include <jsoncons_ext/jsonpath/json_location.hpp>

#endif
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonpath/path_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <vector>
#include <functional>
#include <algorithm> // std::reverse
#include <jsoncons/json_location.hpp>
#include <jsoncons_ext/jsonpath/json_location.hpp>
#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/detail/write_number.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_error.hpp>
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ add_executable(unit_tests
jsonpatch/src/jsonpatch_tests.cpp
jsonpath/src/jsonpath_flatten_tests.cpp
jsonpath/src/path_node_tests.cpp
jsonpath/src/json_location_tests.cpp
jsonpath/src/jsonpath_custom_function_tests.cpp
jsonpath/src/jsonpath_json_query_tests.cpp
jsonpath/src/jsonpath_make_expression_tests.cpp
Expand Down Expand Up @@ -142,7 +143,6 @@ add_executable(unit_tests
corelib/src/json_less_tests.cpp
corelib/src/json_line_split_tests.cpp
corelib/src/json_literal_operator_tests.cpp
corelib/src/json_location_tests.cpp
corelib/src/json_object_tests.cpp
corelib/src/ojson_object_tests.cpp
corelib/src/json_options_tests.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under Boost license

#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/json_location.hpp>
#include <catch/catch.hpp>
#include <iostream>

Expand All @@ -12,7 +13,7 @@ TEST_CASE("json_location tests")

SECTION("test 1")
{
json_location loc;
jsonpath::json_location loc;
loc.append("foo").append(1);

CHECK(loc.size() == 2);
Expand Down Expand Up @@ -54,14 +55,14 @@ TEST_CASE("json_location remove tests")

SECTION("store book 1")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(1);


CHECK(doc["store"]["book"].size() == 3);
CHECK(doc["store"]["book"][1]["author"].as<std::string>() == "Evelyn Waugh");

std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);

CHECK(count == 1);
CHECK(doc["store"]["book"].size() == 2);
Expand All @@ -70,14 +71,14 @@ TEST_CASE("json_location remove tests")

SECTION("store book 2")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(2);


CHECK(doc["store"]["book"].size() == 3);
CHECK(doc["store"]["book"][2]["author"].as<std::string>() == "Herman Melville");

std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);

CHECK(count == 1);
CHECK(doc["store"]["book"].size() == 2);
Expand All @@ -88,35 +89,35 @@ TEST_CASE("json_location remove tests")
{
json orig = doc;

json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(3);

CHECK(doc["store"]["book"].size() == 3);
CHECK(doc["store"]["book"][2]["author"].as<std::string>() == "Herman Melville");

std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);

CHECK(count == 0);
CHECK(doc == orig);
}

SECTION("store")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store");

std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);
CHECK(count == 1);
CHECK(doc.size() == 0);
}

SECTION("store book")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book");

CHECK(doc["store"]["book"].size() == 3);
std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);
CHECK(count == 1);
CHECK(doc["store"]["book"].size() == 0);
}
Expand All @@ -125,25 +126,25 @@ TEST_CASE("json_location remove tests")
{
json orig = doc;

json_location loc;
jsonpath::json_location loc;
loc.append("store").append("lost&found");

CHECK(doc["store"].size() == 1);
std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);
CHECK(count == 0);
CHECK(doc == orig);
}

SECTION("store book 2 price")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(2).append("price");

CHECK(doc["store"]["book"].size() == 3);
CHECK(doc["store"]["book"][2]["author"].as<std::string>() == "Herman Melville");
CHECK(doc["store"]["book"][2].contains("price"));

std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);

CHECK(count == 1);
CHECK(doc["store"]["book"].size() == 3);
Expand All @@ -155,13 +156,13 @@ TEST_CASE("json_location remove tests")
{
json orig = doc;

json_location loc;
jsonpath::json_location loc;
loc.append("store").append(0);

CHECK(doc["store"]["book"].size() == 3);
CHECK(doc["store"]["book"][2].contains("price"));

std::size_t count = json_erase(doc, loc);
std::size_t count = jsonpath::json_erase(doc, loc);

CHECK(count == 0);
CHECK(doc == orig);
Expand Down Expand Up @@ -199,82 +200,82 @@ TEST_CASE("json_location select tests")

SECTION("store book 1")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(1);

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);

CHECK_FALSE(p_json == nullptr);
CHECK(*p_json == doc.at("store").at("book").at(1));
}

SECTION("store book 2")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(2);

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);

CHECK_FALSE(p_json == nullptr);
CHECK(*p_json == doc.at("store").at("book").at(2));
}

SECTION("store book 3")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(3);

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);

CHECK(p_json == nullptr);
}

SECTION("store")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store");

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);
CHECK_FALSE(p_json == nullptr);
CHECK(*p_json == doc.at("store"));
}

SECTION("store book")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book");

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);
CHECK_FALSE(p_json == nullptr);
CHECK(*p_json == doc.at("store").at("book"));
}

SECTION("store lost&found")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("lost&found");

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);
CHECK(p_json == nullptr);
}

SECTION("store book 2 price")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append("book").append(2).append("price");

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);

CHECK_FALSE(p_json == nullptr);
CHECK(*p_json == doc.at("store").at("book").at(2).at("price"));
}

SECTION("store 0")
{
json_location loc;
jsonpath::json_location loc;
loc.append("store").append(0);

json* p_json = json_select(doc, loc);
json* p_json = jsonpath::json_get(doc, loc);

CHECK(p_json == nullptr);
}
Expand Down
6 changes: 3 additions & 3 deletions test/jsonpath/src/jsonpath_select_paths_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ TEST_CASE("jsonpath.jsonpath select_paths test")
std::cout << s << std::endl;
}

std::vector<json_location> locations;
json_location expected1;
std::vector<jsonpath::json_location> locations;
jsonpath::json_location expected1;
expected1.append("store").append("book").append(1).append("title");
json_location expected2;
jsonpath::json_location expected2;
expected2.append("store").append("book").append(2).append("title");

locations.push_back(expected1);
Expand Down
2 changes: 1 addition & 1 deletion test/jsonpath/src/path_node_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ TEST_CASE("test to_json_location")
jsonpath::path_node a3(&a2,"bar");
jsonpath::path_node a4(&a3,7);

json_location location;
jsonpath::json_location location;
location.append("foo").append("bar").append(7);

std::string jsonpath_string = "$['foo']['bar'][7]";
Expand Down

0 comments on commit ccb5698

Please sign in to comment.