31
31
import com .fasterxml .jackson .databind .ObjectMapper ;
32
32
import com .fasterxml .jackson .databind .SerializationFeature ;
33
33
import com .fasterxml .jackson .datatype .joda .JodaModule ;
34
+ import com .jkoolcloud .client .api .service .JKStreamException ;
34
35
35
36
/**
36
37
* This class implements common API utilities
@@ -132,6 +133,33 @@ public static String getVMName() {
132
133
return VM_NAME ;
133
134
}
134
135
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
+ */
135
163
public static ObjectMapper newObjectMapper () {
136
164
ObjectMapper mapper = new ObjectMapper ();
137
165
mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
@@ -155,17 +183,6 @@ public static String jsonFormat(JsonStructure json, String... options) {
155
183
return stringWriter .toString ();
156
184
}
157
185
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
-
169
186
public static JsonValue getJsonValue (String json_path , JsonObject response ) {
170
187
StringTokenizer tk = new StringTokenizer (json_path , "/" );
171
188
@@ -182,4 +199,15 @@ public static JsonValue getJsonValue(String json_path, JsonObject response) {
182
199
}
183
200
return rValue ;
184
201
}
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
+ }
185
213
}
0 commit comments