Skip to content

Commit 3d890c5

Browse files
Update Content App Command Delegate (#34895)
* Update Content App Command Delegate * Restyled by whitespace * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent b942f5c commit 3d890c5

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

examples/tv-app/android/java/ContentAppCommandDelegate.cpp

+40-28
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,37 @@ using Status = chip::Protocols::InteractionModel::Status;
4949
const std::string FAILURE_KEY = "PlatformError";
5050
const std::string FAILURE_STATUS_KEY = "Status";
5151

52+
bool isValidJson(const char * response)
53+
{
54+
Json::Reader reader;
55+
56+
Json::CharReaderBuilder readerBuilder;
57+
std::string errors;
58+
59+
Json::Value value;
60+
std::unique_ptr<Json::CharReader> testReader(readerBuilder.newCharReader());
61+
62+
if (!testReader->parse(response, response + std::strlen(response), &value, &errors))
63+
{
64+
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
65+
return false;
66+
}
67+
68+
// Validate and access JSON data safely
69+
if (!value.isObject())
70+
{
71+
ChipLogError(Zcl, "Invalid JSON structure: not an object");
72+
return false;
73+
}
74+
75+
if (!reader.parse(response, value))
76+
{
77+
return false;
78+
}
79+
80+
return true;
81+
}
82+
5283
void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerContext & handlerContext)
5384
{
5485
if (handlerContext.mRequestPath.mEndpointId >= FIXED_ENDPOINT_COUNT)
@@ -94,7 +125,15 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
94125
{
95126
JniUtfString respStr(env, resp);
96127
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand got response %s", respStr.c_str());
97-
FormatResponseData(handlerContext, respStr.c_str());
128+
if (isValidJson(respStr.c_str()))
129+
{
130+
FormatResponseData(handlerContext, respStr.c_str());
131+
}
132+
else
133+
{
134+
// return dummy value in case JSON is invalid
135+
FormatResponseData(handlerContext, "{\"value\":{}}");
136+
}
98137
}
99138
env->DeleteLocalRef(resp);
100139
}
@@ -141,22 +180,19 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
141180
if (!testReader->parse(respStr.c_str(), respStr.c_str() + std::strlen(respStr.c_str()), &value, &errors))
142181
{
143182
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
144-
env->DeleteLocalRef(resp);
145183
return chip::Protocols::InteractionModel::Status::Failure;
146184
}
147185

148186
// Validate and access JSON data safely
149187
if (!value.isObject())
150188
{
151189
ChipLogError(Zcl, "Invalid JSON structure: not an object");
152-
env->DeleteLocalRef(resp);
153190
return chip::Protocols::InteractionModel::Status::Failure;
154191
}
155192

156193
Json::Reader reader;
157194
if (!reader.parse(respStr.c_str(), value))
158195
{
159-
env->DeleteLocalRef(resp);
160196
return chip::Protocols::InteractionModel::Status::Failure;
161197
}
162198
}
@@ -185,31 +221,7 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
185221
void ContentAppCommandDelegate::FormatResponseData(CommandHandlerInterface::HandlerContext & handlerContext, const char * response)
186222
{
187223
handlerContext.SetCommandHandled();
188-
Json::Reader reader;
189-
190-
Json::CharReaderBuilder readerBuilder;
191-
std::string errors;
192-
193224
Json::Value value;
194-
std::unique_ptr<Json::CharReader> testReader(readerBuilder.newCharReader());
195-
196-
if (!testReader->parse(response, response + std::strlen(response), &value, &errors))
197-
{
198-
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
199-
return;
200-
}
201-
202-
// Validate and access JSON data safely
203-
if (!value.isObject())
204-
{
205-
ChipLogError(Zcl, "Invalid JSON structure: not an object");
206-
return;
207-
}
208-
209-
if (!reader.parse(response, value))
210-
{
211-
return;
212-
}
213225

214226
// handle errors from platform-app
215227
if (!value[FAILURE_KEY].empty())

0 commit comments

Comments
 (0)