diff --git a/src/jmespath.net.parser/JmesPathParser.cs b/src/jmespath.net.parser/JmesPathParser.cs index 492fbfa..8f1f8d0 100644 --- a/src/jmespath.net.parser/JmesPathParser.cs +++ b/src/jmespath.net.parser/JmesPathParser.cs @@ -181,6 +181,12 @@ void OnHashWildcardProjection() => void AddMultiSelectHashExpression() => generator_.AddMultiSelectHashExpression(); void PopMultiSelectHash() => generator_.PopMultiSelectHash(); + void AddCompactHashExpression(Token token) + { + generator_.OnIdentifier((string)token.Value); + generator_.AddMultiSelectHashExpression(); + } + #endregion #region multi_select_list diff --git a/src/jmespath.net.parser/JmesPathParser.y b/src/jmespath.net.parser/JmesPathParser.y index 89f23db..135c526 100644 --- a/src/jmespath.net.parser/JmesPathParser.y +++ b/src/jmespath.net.parser/JmesPathParser.y @@ -246,7 +246,28 @@ hash_wildcard : T_STAR } ; -multi_select_hash : T_LBRACE keyval_expressions T_RBRACE +multi_select_hash : multi_select_hash_key + | multi_select_hash_keyval + ; + +multi_select_hash_key + : T_LBRACE key_expressions T_RBRACE + { + PopMultiSelectHash(); + }; + +key_expressions : identifier + { + PushMultiSelectHash(); + AddCompactHashExpression($1.Token); + } + |key_expressions T_COMMA identifier + { + AddCompactHashExpression($3.Token); + }; + +multi_select_hash_keyval + : T_LBRACE keyval_expressions T_RBRACE { PopMultiSelectHash(); }; diff --git a/tests/jmespathnet.tests/Parser/MultiSelectHashTest.cs b/tests/jmespathnet.tests/Parser/MultiSelectHashTest.cs index a94e293..00a6756 100644 --- a/tests/jmespathnet.tests/Parser/MultiSelectHashTest.cs +++ b/tests/jmespathnet.tests/Parser/MultiSelectHashTest.cs @@ -29,5 +29,11 @@ public void ParseMultiSelectHash_Compliance() { Assert("missing.{foo: bar}", "[]", "null"); } + [Fact] + public void ParseMultiSelectHash_Compact() + { + Assert("{foo: foo, baz: baz}", "{\"foo\": \"bar\", \"baz\": \"qux\"}", "{\"foo\":\"bar\",\"baz\":\"qux\"}"); + Assert("{foo, baz}", "{\"foo\": \"bar\", \"baz\": \"qux\"}", "{\"foo\":\"bar\",\"baz\":\"qux\"}"); + } } } \ No newline at end of file