Skip to content

Commit

Permalink
jsonpath jsonpath_expression
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Oct 23, 2023
1 parent d32f163 commit 5d6668b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ add_executable(unit_tests
jsonpath/src/jsonpath_flatten_tests.cpp
jsonpath/src/jsonpath_custom_function_tests.cpp
jsonpath/src/jsonpath_json_query_tests.cpp
jsonpath/src/jsonpath_make_expression_tests.cpp
jsonpath/src/jsonpath_json_replace_tests.cpp
jsonpath/src/jsonpath_test_suite.cpp
jsonpath/src/jsonpath_stateful_allocator_tests.cpp
Expand Down
111 changes: 111 additions & 0 deletions test/jsonpath/src/jsonpath_make_expression_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2013-2023 Daniel Parker
// Distributed under Boost license

#if defined(_MSC_VER)
#include "windows.h" // test no inadvertant macro expansions
#endif
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/json_query.hpp>
#include <catch/catch.hpp>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <utility>
#include <ctime>
#include <new>
#include <unordered_set> // std::unordered_set
#include <fstream>

using namespace jsoncons;

TEST_CASE("jsonpath make_expression test")
{
std::string input = R"(
{
"books":
[
{
"category": "fiction",
"title" : "A Wild Sheep Chase",
"author" : "Haruki Murakami",
"price" : 22.72
},
{
"category": "fiction",
"title" : "The Night Watch",
"author" : "Sergei Lukyanenko",
"price" : 23.58
},
{
"category": "fiction",
"title" : "The Comedians",
"author" : "Graham Greene",
"price" : 21.99
},
{
"category": "memoir",
"title" : "The Night Watch",
"author" : "Phillips, David Atlee"
}
]
}
)";

SECTION("test 1")
{
int count = 0;

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

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

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

expr.evaluate(doc, callback);

CHECK(count == 1);
CHECK_FALSE(doc["books"][3].contains("price"));
}

SECTION("test 2")
{
int count = 0;

json doc = json::parse(input);

auto expr = jsoncons::jsonpath::make_expression_for_update<json, json&>("$.books[*]");

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

auto callback2 = [](const std::string& /*path*/, json& book)
{
if (book.at("category") == "memoir" && !book.contains("price"))
{
book.try_emplace("price", 140.0);
}
};

expr.evaluate(doc, callback1);

CHECK(count == 1);

CHECK_FALSE(doc["books"][3].contains("price"));
expr.evaluate_and_update(doc, callback2);
CHECK(doc["books"][3].contains("price"));
CHECK(doc["books"][3].at("price") == 140);
}
}

0 comments on commit 5d6668b

Please sign in to comment.