@@ -49,6 +49,37 @@ 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
+ {
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
+
52
83
void ContentAppCommandDelegate::InvokeCommand (CommandHandlerInterface::HandlerContext & handlerContext)
53
84
{
54
85
if (handlerContext.mRequestPath .mEndpointId >= FIXED_ENDPOINT_COUNT)
@@ -94,7 +125,15 @@ void ContentAppCommandDelegate::InvokeCommand(CommandHandlerInterface::HandlerCo
94
125
{
95
126
JniUtfString respStr (env, resp);
96
127
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
+ }
98
137
}
99
138
env->DeleteLocalRef (resp);
100
139
}
@@ -141,22 +180,19 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
141
180
if (!testReader->parse (respStr.c_str (), respStr.c_str () + std::strlen (respStr.c_str ()), &value, &errors))
142
181
{
143
182
ChipLogError (Zcl, " Failed to parse JSON: %s\n " , errors.c_str ());
144
- env->DeleteLocalRef (resp);
145
183
return chip::Protocols::InteractionModel::Status::Failure;
146
184
}
147
185
148
186
// Validate and access JSON data safely
149
187
if (!value.isObject ())
150
188
{
151
189
ChipLogError (Zcl, " Invalid JSON structure: not an object" );
152
- env->DeleteLocalRef (resp);
153
190
return chip::Protocols::InteractionModel::Status::Failure;
154
191
}
155
192
156
193
Json::Reader reader;
157
194
if (!reader.parse (respStr.c_str (), value))
158
195
{
159
- env->DeleteLocalRef (resp);
160
196
return chip::Protocols::InteractionModel::Status::Failure;
161
197
}
162
198
}
@@ -185,31 +221,7 @@ Status ContentAppCommandDelegate::InvokeCommand(EndpointId epId, ClusterId clust
185
221
void ContentAppCommandDelegate::FormatResponseData (CommandHandlerInterface::HandlerContext & handlerContext, const char * response)
186
222
{
187
223
handlerContext.SetCommandHandled ();
188
- Json::Reader reader;
189
-
190
- Json::CharReaderBuilder readerBuilder;
191
- std::string errors;
192
-
193
224
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
- }
213
225
214
226
// handle errors from platform-app
215
227
if (!value[FAILURE_KEY].empty ())
0 commit comments