Skip to content

Commit 4256669

Browse files
committed
Update Content App Command Delegate
1 parent 17fd793 commit 4256669

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

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

+36-9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ 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+
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
64+
return false;
65+
}
66+
67+
// Validate and access JSON data safely
68+
if (!value.isObject()) {
69+
ChipLogError(Zcl, "Invalid JSON structure: not an object");
70+
return false;
71+
}
72+
73+
if (!reader.parse(response, value))
74+
{
75+
return false;
76+
}
77+
78+
return true;
79+
}
80+
5281
void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerContext & handlerContext)
5382
{
5483
if (handlerContext.mRequestPath.mEndpointId >= FIXED_ENDPOINT_COUNT)
@@ -94,7 +123,13 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
94123
{
95124
JniUtfString respStr(env, resp);
96125
ChipLogProgress(Zcl, "ContentAppCommandDelegate::InvokeCommand got response %s", respStr.c_str());
97-
FormatResponseData(handlerContext, respStr.c_str());
126+
if (isValidJson(respStr.c_str())) {
127+
FormatResponseData(handlerContext, respStr.c_str());
128+
} else {
129+
// return dummy value in case JSON is invalid
130+
FormatResponseData(handlerContext, "{\"value\":{}}");
131+
}
132+
98133
}
99134
env->DeleteLocalRef(resp);
100135
}
@@ -141,22 +176,19 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
141176
if (!testReader->parse(respStr.c_str(), respStr.c_str() + std::strlen(respStr.c_str()), &value, &errors))
142177
{
143178
ChipLogError(Zcl, "Failed to parse JSON: %s\n", errors.c_str());
144-
env->DeleteLocalRef(resp);
145179
return chip::Protocols::InteractionModel::Status::Failure;
146180
}
147181

148182
// Validate and access JSON data safely
149183
if (!value.isObject())
150184
{
151185
ChipLogError(Zcl, "Invalid JSON structure: not an object");
152-
env->DeleteLocalRef(resp);
153186
return chip::Protocols::InteractionModel::Status::Failure;
154187
}
155188

156189
Json::Reader reader;
157190
if (!reader.parse(respStr.c_str(), value))
158191
{
159-
env->DeleteLocalRef(resp);
160192
return chip::Protocols::InteractionModel::Status::Failure;
161193
}
162194
}
@@ -185,11 +217,6 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
185217
void ContentAppCommandDelegate::FormatResponseData(CommandHandlerInterface::HandlerContext & handlerContext, const char * response)
186218
{
187219
handlerContext.SetCommandHandled();
188-
Json::Reader reader;
189-
190-
Json::CharReaderBuilder readerBuilder;
191-
std::string errors;
192-
193220
Json::Value value;
194221
std::unique_ptr<Json::CharReader> testReader(readerBuilder.newCharReader());
195222

0 commit comments

Comments
 (0)