-
-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jsonpath: evaluation throws on json containing json_const_pointer #554
Comments
Yes, in 0.172.0 there were some changes to You can still run this query by changing the template parameter in make_expression from #include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
#include <iostream>
int main()
{
jsoncons::json root_works = jsoncons::json::parse(R"(
{
"books":
[
{
"category": "fiction",
"title" : "The Night Watch",
"author" : "Sergei Lukyanenko",
"price" : 23.58
},
{
"category": "fiction",
"title" : "The Comedians",
"author" : "Graham Greene",
"price" : 21.99
}
]
}
)");
jsoncons::json nested_json = jsoncons::json::parse(R"(
[
{
"category": "fiction",
"title" : "The Night Watch",
"author" : "Sergei Lukyanenko",
"price" : 23.58
},
{
"category": "fiction",
"title" : "The Comedians",
"author" : "Graham Greene",
"price" : 21.99
}
]
)");
jsoncons::json now_works = root_works;
now_works["books"] = jsoncons::json(jsoncons::json_const_pointer_arg, &nested_json);
auto expr = jsoncons::jsonpath::make_expression<const jsoncons::json>("$.books[?(@.price > avg($.books[*].price))].title");
try
{
jsoncons::json result_works = expr.evaluate(root_works);
jsoncons::json result_now_works = expr.evaluate(now_works);
std::cout << result_now_works << "\n";
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
} Output: ["The Night Watch"] There's a bit of an issue with The next version (0.179.0) will support a json value containing non-const references to other json values, and you'll be able to write it as follows: jsoncons::json now_works = root_works;
now_works["books"] = jsoncons::json(jsoncons::json_reference_arg, nested_json);
auto expr = jsoncons::jsonpath::make_expression<jsoncons::json>("$.books[?(@.price > avg($.books[*].price))].title");
try
{
jsoncons::json result_works = expr.evaluate(root_works);
jsoncons::json result_now_works = expr.evaluate(now_works);
std::cout << result_now_works << "\n";
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
|
Many thanks for the detailed explanation! Sorry that I did not immediately recognize the change in 0.172.0. The problem is therefore solved for me. I will test the new value type |
Starting with the next release, code currently on master, this won't happen anymore, even with |
Calling
jsonpath_expression::evaluate()
on a JSON that contains ajson_const_pointer
to a valid second JSON throws "Index on non-array value not supported". The reason seems to be a non-const call tobasic_json::at[int i]
when selecting an array object.This behavior exist in the latest release 0.178.0. I used release 0.170.2 before which did not show this exception. There everything worked fine.
The attached example_code.txt shows how to reproduce the error.
The text was updated successfully, but these errors were encountered: