Skip to content

Commit 1616f50

Browse files
Merge pull request #687 from BartelNieuwenhuyse/bugfix/jmespath_keyval-expr
jmespath: allow rhs_expression in a keyvalue - expression
2 parents 85d273f + 0e39596 commit 1616f50

3 files changed

Lines changed: 50 additions & 14 deletions

File tree

include/jsoncons_ext/jmespath/jmespath.hpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4431,8 +4431,8 @@ namespace detail {
44314431
push_token(token<Json>(begin_multi_select_list_arg), resources, output_stack, ec);
44324432
if (JSONCONS_UNLIKELY(ec)) {return jmespath_expression{};}
44334433
state_stack.back() = expr_state::multi_select_list;
4434-
state_stack.push_back(expr_state::rhs_expression);
4435-
state_stack.push_back(expr_state::lhs_expression);
4434+
state_stack.push_back(expr_state::rhs_expression);
4435+
state_stack.push_back(expr_state::lhs_expression);
44364436
context_stack.push_back(expression_context<Json>{});
44374437
}
44384438
break;
@@ -4490,7 +4490,8 @@ namespace detail {
44904490
case '-':case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
44914491
break;
44924492
default:
4493-
state_stack.back() = expr_state::key_val_expr;
4493+
state_stack.back() = expr_state::expect_rbrace;
4494+
state_stack.push_back(expr_state::key_val_expr);
44944495
break;
44954496
}
44964497
break;
@@ -4906,16 +4907,7 @@ namespace detail {
49064907
case ',':
49074908
push_token(token<Json>(separator_arg), resources, output_stack, ec);
49084909
if (JSONCONS_UNLIKELY(ec)) {return jmespath_expression{};}
4909-
state_stack.back() = expr_state::key_val_expr;
4910-
++p_;
4911-
++column_;
4912-
break;
4913-
case '[':
4914-
case '{':
4915-
state_stack.push_back(expr_state::lhs_expression);
4916-
break;
4917-
case '.':
4918-
state_stack.push_back(expr_state::sub_expression);
4910+
state_stack.push_back(expr_state::key_val_expr);
49194911
++p_;
49204912
++column_;
49214913
break;
@@ -4942,8 +4934,9 @@ namespace detail {
49424934
advance_past_space_character();
49434935
break;
49444936
case ':':
4945-
state_stack.back() = expr_state::expect_rbrace;
4937+
state_stack.back() = expr_state::rhs_expression;
49464938
state_stack.push_back(expr_state::lhs_expression);
4939+
context_stack.push_back(expression_context<Json>{});
49474940
++p_;
49484941
++column_;
49494942
break;

test/jmespath/input/test.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
11
[
2+
{
3+
"given": {
4+
"foo": {
5+
"bar": "baz"
6+
}
7+
},
8+
"cases": [
9+
{
10+
"comment": "Comparator expression evaluates to true boolean",
11+
"expression": "foo.bar == 'baz'",
12+
"result": true
13+
},
14+
{
15+
"comment": "Comparator expression evaluates to false boolean",
16+
"expression": "foo.bar == 'bar'",
17+
"result": false
18+
},
19+
{
20+
"comment": "Multi select list comparator value expression evaluates to true boolean",
21+
"expression": "[foo.bar == 'baz']",
22+
"result": [ true ]
23+
},
24+
{
25+
"comment": "Multi select list comparator value expression evaluates to false boolean",
26+
"expression": "[foo.bar == 'bar']",
27+
"result": [ false ]
28+
},
29+
{
30+
"comment": "Multi select hash comparator value expression evaluates to true boolean",
31+
"expression": "{value: foo.bar == 'baz'}",
32+
"result": { "value": true }
33+
},
34+
{
35+
"comment": "Multi select hash comparator value expression evaluates to false boolean",
36+
"expression": "{value: foo.bar == 'bar'}",
37+
"result": { "value": false }
38+
}
39+
]
40+
}
241
]
342

test/jmespath/src/jmespath_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ void jmespath_tests(const std::string& fpath)
9090

9191
TEST_CASE("jmespath-tests")
9292
{
93+
SECTION("Tests")
94+
{
95+
jmespath_tests("./jmespath/input/test.json");
96+
}
9397
SECTION("Examples and tutorials")
9498
{
9599
jmespath_tests("./jmespath/input/examples/jmespath-examples.json");

0 commit comments

Comments
 (0)