32 #ifndef QUERY_JSONPATH_HPP_ 33 #define QUERY_JSONPATH_HPP_ 35 #include "../Helper/Strings.hpp" 36 #include "../Main/Exception.hpp" 38 #include "../_extern/jsoncons/include/jsoncons/json.hpp" 39 #include "../_extern/jsoncons/include/jsoncons_ext/jsonpath/json_query.hpp" 61 JsonPath(
const std::string& pathString,
bool textOnlyQuery);
67 [[nodiscard]]
bool getBool(
const jsoncons::json& json)
const;
68 void getFirst(
const jsoncons::json& json, std::string& resultTo)
const;
69 void getAll(
const jsoncons::json& json, std::vector<std::string>& resultTo)
const;
70 void getSubSets(
const jsoncons::json& json, std::vector<jsoncons::json>& resultTo)
const;
113 : jsonPath(pathString),
114 textOnly(textOnlyQuery) {
117 if(this->jsonPath.empty()) {
118 throw Exception(
"No JSONPath string given");
137 if(this->jsonPath.empty()) {
138 throw Exception(
"No JSONPath query defined");
143 const auto result{jsoncons::jsonpath::json_query(json, this->jsonPath)};
145 return !(result.is_array() && result.empty());
147 catch(
const jsoncons::json_exception& e) {
149 std::string(e.what())
184 if(this->jsonPath.empty()) {
190 const auto result{jsoncons::jsonpath::json_query(json, this->jsonPath)};
193 if(!result.is_array()) {
194 throw Exception(
"jsoncons::jsonpath::json_query() did not return an array");
198 if(!result.array_value().empty()) {
199 if(result[0].is_array() && !(this->textOnly)) {
201 resultTo = result[0][0].as<std::string>();
205 resultTo = result[0].as<std::string>();
209 catch(
const jsoncons::json_exception& e) {
211 std::string(e.what())
213 + this->jsonPath +
"')" 241 inline void JsonPath::getAll(
const jsoncons::json& json, std::vector<std::string>& resultTo)
const {
246 if(this->jsonPath.empty()) {
252 const auto result{jsoncons::jsonpath::json_query(json, this->jsonPath)};
255 if(!result.is_array()) {
256 throw Exception(
"jsoncons::jsonpath::json_query() did not return an array");
260 switch(result.array_value().size()) {
265 if(result[0].is_array() && !(this->textOnly)) {
267 resultTo.reserve(result[0].array_value().size());
269 for(
const auto& element : result[0].array_range()) {
270 resultTo.emplace_back(element.as<std::string>());
274 resultTo.emplace_back(result[0].as<std::string>());
281 resultTo.reserve(result.array_value().size());
283 for(
const auto& element : result.array_range()) {
284 resultTo.emplace_back(element.as<std::string>());
288 catch(
const jsoncons::json_exception& e) {
290 std::string(e.what())
292 + this->jsonPath +
"')" 328 if(this->jsonPath.empty()) {
334 const auto result{jsoncons::jsonpath::json_query(json, this->jsonPath)};
337 if(!result.is_array()) {
338 throw Exception(
"jsoncons::jsonpath::json_query() did not return an array");
342 switch(result.array_value().size()) {
347 if(result[0].is_array() && !(this->textOnly)) {
349 resultTo.reserve(result[0].array_value().size());
351 for(
const auto& element : result[0].array_range()) {
352 resultTo.emplace_back(element);
356 resultTo.emplace_back(result[0]);
363 resultTo.reserve(result.array_value().size());
365 for(
const auto& element : result.array_range()) {
366 resultTo.emplace_back(element);
370 catch(
const jsoncons::json_exception& e) {
372 std::string(e.what())
374 + this->jsonPath +
"')" void getAll(const jsoncons::json &json, std::vector< std::string > &resultTo) const
Gets all matches from performing the query on a parsed JSON document.
Definition: JsonPath.hpp:241
bool getBool(const jsoncons::json &json) const
Gets a boolean result from performing the query on a parsed JSON document.
Definition: JsonPath.hpp:135
void getFirst(const jsoncons::json &json, std::string &resultTo) const
Gets the first match from performing the query on a parsed JSON document.
Definition: JsonPath.hpp:179
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
Implements a JSONPath query using the jsoncons library.
Definition: JsonPath.hpp:56
void trim(std::string &stringToTrim)
Removes whitespaces around a string.
Definition: Strings.hpp:360
JsonPath(const std::string &pathString, bool textOnlyQuery)
Constructor setting a JSONPath string and whether the result should be text-only. ...
Definition: JsonPath.hpp:112
void getSubSets(const jsoncons::json &json, std::vector< jsoncons::json > &resultTo) const
Gets all matching subsets from performing the query on a parsed JSON document.
Definition: JsonPath.hpp:323
Namespace for classes handling queries.
Definition: XML.hpp:51
Class for JSONPath exceptions.
Definition: JsonPath.hpp:85