Skip to content

Commit 7c8ec50

Browse files
committed
Refactor json serialization
Refactor json serialization
1 parent b00378f commit 7c8ec50

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/main/java/com/jkoolcloud/client/api/service/JKService.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ public String getServiceUrl() {
102102
* if error occurs during a call
103103
*/
104104
public String serialize(Object obj) throws JKStreamException {
105-
if (obj == null) {
106-
throw new JKStreamException(500, "Object must not be null");
107-
}
108-
try {
109-
return mapper.writeValueAsString(obj);
110-
} catch (Exception e) {
111-
throw new JKStreamException(600, "Failed to serialize object: " + e.getMessage(), e);
112-
}
105+
return JKUtils.serialize(mapper, obj);
113106
}
114107
}

src/main/java/com/jkoolcloud/client/api/utils/JKUtils.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.fasterxml.jackson.databind.ObjectMapper;
3232
import com.fasterxml.jackson.databind.SerializationFeature;
3333
import com.fasterxml.jackson.datatype.joda.JodaModule;
34+
import com.jkoolcloud.client.api.service.JKStreamException;
3435

3536
/**
3637
* This class implements common API utilities
@@ -132,6 +133,33 @@ public static String getVMName() {
132133
return VM_NAME;
133134
}
134135

136+
/**
137+
* Serialize an object into JSON format
138+
*
139+
* @param mapper
140+
* JSON object mapper instance
141+
* @param obj
142+
* java object instance (non null)
143+
* @return JSON representation of the object
144+
* @throws JKStreamException
145+
* if error occurs during a call
146+
*/
147+
public static String serialize(ObjectMapper mapper, Object obj) throws JKStreamException {
148+
if (obj == null) {
149+
throw new JKStreamException(500, "Object must not be null");
150+
}
151+
try {
152+
return mapper.writeValueAsString(obj);
153+
} catch (Throwable e) {
154+
throw new JKStreamException(600, "Failed to serialize object", e);
155+
}
156+
}
157+
158+
/**
159+
* Create new Object mapper instance for JSON serialization
160+
*
161+
* @return JSON object mapper
162+
*/
135163
public static ObjectMapper newObjectMapper() {
136164
ObjectMapper mapper = new ObjectMapper();
137165
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -155,17 +183,6 @@ public static String jsonFormat(JsonStructure json, String... options) {
155183
return stringWriter.toString();
156184
}
157185

158-
private static Map<String, Boolean> buildConfig(String... options) {
159-
Map<String, Boolean> config = new HashMap<>();
160-
161-
if (options != null) {
162-
for (String option : options) {
163-
config.put(option, true);
164-
}
165-
}
166-
return config;
167-
}
168-
169186
public static JsonValue getJsonValue(String json_path, JsonObject response) {
170187
StringTokenizer tk = new StringTokenizer(json_path, "/");
171188

@@ -182,4 +199,15 @@ public static JsonValue getJsonValue(String json_path, JsonObject response) {
182199
}
183200
return rValue;
184201
}
202+
203+
private static Map<String, Boolean> buildConfig(String... options) {
204+
Map<String, Boolean> config = new HashMap<>();
205+
206+
if (options != null) {
207+
for (String option : options) {
208+
config.put(option, true);
209+
}
210+
}
211+
return config;
212+
}
185213
}

0 commit comments

Comments
 (0)