2
2
3
3
import okhttp3 .Request ;
4
4
import okhttp3 .ResponseBody ;
5
+
5
6
import org .json .JSONArray ;
6
7
import org .json .JSONException ;
7
8
import org .json .JSONObject ;
9
+
8
10
import retrofit2 .Call ;
9
11
import retrofit2 .Response ;
10
12
20
22
import java .util .logging .Level ;
21
23
import java .util .logging .Logger ;
22
24
import java .util .stream .IntStream ;
25
+ import com .fasterxml .jackson .databind .ObjectMapper ; // Jackson for JSON parsing
26
+ import com .fasterxml .jackson .databind .json .JsonMapper ;
27
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
28
+ import com .fasterxml .jackson .databind .type .MapType ;
23
29
24
30
import static com .contentstack .sdk .Constants .*;
25
31
@@ -186,6 +192,14 @@ public void send() {
186
192
}
187
193
}
188
194
195
+ private JSONObject createOrderedJSONObject (Map <String , Object > map ) {
196
+ JSONObject json = new JSONObject ();
197
+ for (Map .Entry <String , Object > entry : map .entrySet ()) {
198
+ json .put (entry .getKey (), entry .getValue ());
199
+ }
200
+ return json ;
201
+ }
202
+
189
203
private void getService (String requestUrl ) throws IOException {
190
204
191
205
this .headers .put (X_USER_AGENT_KEY , "contentstack-delivery-java/" + SDK_VERSION );
@@ -210,16 +224,22 @@ private void getService(String requestUrl) throws IOException {
210
224
if (request != null ) {
211
225
response = pluginResponseImp (request , response );
212
226
}
213
- String responseBody = response .body ().string ();
214
227
try {
215
- responseJSON = new JSONObject (responseBody );
228
+ // Use Jackson to parse the JSON while preserving order
229
+ ObjectMapper mapper = JsonMapper .builder ().build ();
230
+ MapType type = mapper .getTypeFactory ().constructMapType (LinkedHashMap .class , String .class ,
231
+ Object .class );
232
+ Map <String , Object > responseMap = mapper .readValue (response .body ().string (), type );
233
+
234
+ // Use the custom method to create an ordered JSONObject
235
+ responseJSON = createOrderedJSONObject (responseMap );
216
236
if (this .config .livePreviewEntry != null && !this .config .livePreviewEntry .isEmpty ()) {
217
237
handleJSONArray ();
218
238
}
219
239
connectionRequest .onRequestFinished (CSHttpConnection .this );
220
240
} catch (JSONException e ) {
221
241
// Handle non-JSON response
222
- setError ("Invalid JSON response: " + responseBody );
242
+ setError ("Invalid JSON response" );
223
243
}
224
244
} else {
225
245
assert response .errorBody () != null ;
0 commit comments