Skip to content

Commit 107169e

Browse files
authored
Merge pull request #140 from contentstack/fix/DX-1529
fix: preserve order of response object
2 parents 9bf81c6 + cfd2495 commit 107169e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.contentstack.sdk</groupId>
77
<artifactId>java</artifactId>
8-
<version>2.0.0</version>
8+
<version>2.0.1</version>
99
<packaging>jar</packaging>
1010
<name>contentstack-java</name>
1111
<description>Java SDK for Contentstack Content Delivery API</description>
@@ -17,7 +17,7 @@
1717
<maven.compiler.source>1.8</maven.compiler.source>
1818
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1919
<surefire-report-plugin.version>2.22.0</surefire-report-plugin.version>
20-
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
20+
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
2121
<maven-javadoc-plugin.version>3.4.1</maven-javadoc-plugin.version>
2222
<dotenv-source.version>3.0.0</dotenv-source.version>
2323
<rxjava-source.version>3.1.8</rxjava-source.version>
@@ -183,6 +183,12 @@
183183
<version>${json-simple-version}</version>
184184
<scope>compile</scope>
185185
</dependency>
186+
187+
<dependency>
188+
<groupId>com.fasterxml.jackson.core</groupId>
189+
<artifactId>jackson-databind</artifactId>
190+
<version>2.15.2</version>
191+
</dependency>
186192
</dependencies>
187193

188194
<build>

src/main/java/com/contentstack/sdk/CSHttpConnection.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import okhttp3.Request;
44
import okhttp3.ResponseBody;
5+
56
import org.json.JSONArray;
67
import org.json.JSONException;
78
import org.json.JSONObject;
9+
810
import retrofit2.Call;
911
import retrofit2.Response;
1012

@@ -20,6 +22,10 @@
2022
import java.util.logging.Level;
2123
import java.util.logging.Logger;
2224
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;
2329

2430
import static com.contentstack.sdk.Constants.*;
2531

@@ -186,6 +192,14 @@ public void send() {
186192
}
187193
}
188194

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+
189203
private void getService(String requestUrl) throws IOException {
190204

191205
this.headers.put(X_USER_AGENT_KEY, "contentstack-delivery-java/" + SDK_VERSION);
@@ -210,16 +224,22 @@ private void getService(String requestUrl) throws IOException {
210224
if (request != null) {
211225
response = pluginResponseImp(request, response);
212226
}
213-
String responseBody = response.body().string();
214227
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);
216236
if (this.config.livePreviewEntry != null && !this.config.livePreviewEntry.isEmpty()) {
217237
handleJSONArray();
218238
}
219239
connectionRequest.onRequestFinished(CSHttpConnection.this);
220240
} catch (JSONException e) {
221241
// Handle non-JSON response
222-
setError("Invalid JSON response: " + responseBody);
242+
setError("Invalid JSON response");
223243
}
224244
} else {
225245
assert response.errorBody() != null;

0 commit comments

Comments
 (0)