@@ -49,6 +49,35 @@ using Status = chip::Protocols::InteractionModel::Status;
49
49
const std::string FAILURE_KEY = " PlatformError" ;
50
50
const std::string FAILURE_STATUS_KEY = " Status" ;
51
51
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
+
52
81
void ContentAppCommandDelegate::InvokeCommand (CommandHandlerInterface::HandlerContext & handlerContext)
53
82
{
54
83
if (handlerContext.mRequestPath .mEndpointId >= FIXED_ENDPOINT_COUNT)
@@ -94,7 +123,13 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
94
123
{
95
124
JniUtfString respStr (env, resp);
96
125
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
+
98
133
}
99
134
env->DeleteLocalRef (resp);
100
135
}
@@ -141,22 +176,19 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
141
176
if (!testReader->parse (respStr.c_str (), respStr.c_str () + std::strlen (respStr.c_str ()), &value, &errors))
142
177
{
143
178
ChipLogError (Zcl, " Failed to parse JSON: %s\n " , errors.c_str ());
144
- env->DeleteLocalRef (resp);
145
179
return chip::Protocols::InteractionModel::Status::Failure;
146
180
}
147
181
148
182
// Validate and access JSON data safely
149
183
if (!value.isObject ())
150
184
{
151
185
ChipLogError (Zcl, " Invalid JSON structure: not an object" );
152
- env->DeleteLocalRef (resp);
153
186
return chip::Protocols::InteractionModel::Status::Failure;
154
187
}
155
188
156
189
Json::Reader reader;
157
190
if (!reader.parse (respStr.c_str (), value))
158
191
{
159
- env->DeleteLocalRef (resp);
160
192
return chip::Protocols::InteractionModel::Status::Failure;
161
193
}
162
194
}
@@ -185,11 +217,6 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
185
217
void ContentAppCommandDelegate::FormatResponseData (CommandHandlerInterface::HandlerContext & handlerContext, const char * response)
186
218
{
187
219
handlerContext.SetCommandHandled ();
188
- Json::Reader reader;
189
-
190
- Json::CharReaderBuilder readerBuilder;
191
- std::string errors;
192
-
193
220
Json::Value value;
194
221
std::unique_ptr<Json::CharReader> testReader (readerBuilder.newCharReader ());
195
222
0 commit comments