diff --git a/src/server/json_family.cc b/src/server/json_family.cc index a11222b0d..8edcb516d 100644 --- a/src/server/json_family.cc +++ b/src/server/json_family.cc @@ -38,6 +38,8 @@ using JsonReplaceCb = function; using JsonReplaceVerify = std::function; using CI = CommandId; +static const char DefaultJsonPath[] = "$"; + namespace { inline OpStatus JsonReplaceVerifyNoOp() { @@ -1004,7 +1006,10 @@ void JsonFamily::Set(CmdArgList args, ConnectionContext* cntx) { void JsonFamily::Resp(CmdArgList args, ConnectionContext* cntx) { string_view key = ArgS(args, 1); - string_view path = ArgS(args, 2); + string_view path = DefaultJsonPath; + if (args.size() > 2) { + path = ArgS(args, 2); + } error_code ec; JsonExpression expression = jsonpath::make_expression(path, ec); @@ -1661,7 +1666,7 @@ void JsonFamily::Register(CommandRegistry* registry) { ArrAppend); *registry << CI{"JSON.ARRINDEX", CO::READONLY | CO::FAST, -4, 1, 1, 1}.HFUNC(ArrIndex); *registry << CI{"JSON.DEBUG", CO::READONLY | CO::FAST, -2, 1, 1, 1}.HFUNC(Debug); - *registry << CI{"JSON.RESP", CO::READONLY | CO::FAST, 3, 1, 1, 1}.HFUNC(Resp); + *registry << CI{"JSON.RESP", CO::READONLY | CO::FAST, -2, 1, 1, 1}.HFUNC(Resp); *registry << CI{"JSON.SET", CO::WRITE | CO::DENYOOM | CO::FAST, 4, 1, 1, 1}.HFUNC(Set); } diff --git a/src/server/json_family_test.cc b/src/server/json_family_test.cc index 9fb2734d5..a26b07ca3 100644 --- a/src/server/json_family_test.cc +++ b/src/server/json_family_test.cc @@ -899,6 +899,9 @@ TEST_F(JsonFamilyTest, Resp) { auto resp = Run({"JSON.SET", "json", ".", PhonebookJson}); ASSERT_THAT(resp, "OK"); + resp = Run({"JSON.RESP", "json"}); + ASSERT_EQ(RespExpr::ARRAY, resp.type); + resp = Run({"JSON.RESP", "json", "$.address.*"}); ASSERT_EQ(RespExpr::ARRAY, resp.type); EXPECT_THAT(resp.GetVec(), ElementsAre("New York", "NY", "21 2nd Street", "10021-3100"));