Skip to content

Commit de48467

Browse files
Added a gson singleton without pretty printing. #209
1 parent 70693b4 commit de48467

20 files changed

Lines changed: 82 additions & 58 deletions

File tree

src/main/java/com/ibm/watson/developer_cloud/concept_insights/v2/ConceptInsights.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void createCorpus(final Corpus corpus) {
287287
final Request request =
288288
RequestBuilder
289289
.put(API_VERSION + corpusId)
290-
.withBodyContent(GsonSingleton.getGson().toJson(corpus), HttpMediaType.APPLICATION_JSON)
290+
.withBodyContent(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(corpus), HttpMediaType.APPLICATION_JSON)
291291
.build();
292292
executeWithoutResponse(request);
293293
}
@@ -302,7 +302,7 @@ public void createDocument(final Document document) {
302302
final Request request =
303303
RequestBuilder
304304
.put(API_VERSION + document.getId())
305-
.withBodyContent(GsonSingleton.getGson().toJson(document),
305+
.withBodyContent(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(document),
306306
HttpMediaType.APPLICATION_JSON).build();
307307

308308
executeWithoutResponse(request);
@@ -849,7 +849,7 @@ public void updateCorpus(final Corpus corpus) {
849849
final Request request =
850850
RequestBuilder
851851
.post(API_VERSION + corpusId)
852-
.withBodyContent(GsonSingleton.getGson().toJson(corpus), HttpMediaType.APPLICATION_JSON)
852+
.withBodyContent(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(corpus), HttpMediaType.APPLICATION_JSON)
853853
.build();
854854
executeWithoutResponse(request);
855855
}
@@ -864,7 +864,7 @@ public void updateDocument(final Document document) {
864864
final Request request =
865865
RequestBuilder
866866
.post(API_VERSION + documentId)
867-
.withBodyContent(GsonSingleton.getGson().toJson(document),
867+
.withBodyContent(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(document),
868868
HttpMediaType.APPLICATION_JSON).build();
869869
executeWithoutResponse(request);
870870
}

src/main/java/com/ibm/watson/developer_cloud/dialog/v1/DialogService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public List<DialogContent> getContent(final String dialogId) {
251251
final Response response = execute(request);
252252
final JsonObject jsonObject = ResponseUtil.getJsonObject(response);
253253
final List<DialogContent> content =
254-
GsonSingleton.getGson().fromJson(jsonObject.get("items"), listDialogContentType);
254+
GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonObject.get("items"), listDialogContentType);
255255
return content;
256256
}
257257

@@ -320,7 +320,7 @@ public List<Dialog> getDialogs() {
320320
final Response response = execute(request);
321321
final JsonObject jsonObject = ResponseUtil.getJsonObject(response);
322322
final List<Dialog> dialogs =
323-
GsonSingleton.getGson().fromJson(jsonObject.get("dialogs"), listDialogType);
323+
GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonObject.get("dialogs"), listDialogType);
324324
return dialogs;
325325
}
326326

@@ -352,7 +352,7 @@ public Map<String, String> getProfile(String dialogId, Integer clientId, String.
352352
final Response response = execute(request);
353353
final JsonObject jsonObject = ResponseUtil.getJsonObject(response);
354354
final List<NameValue> nameValues =
355-
GsonSingleton.getGson().fromJson(jsonObject.get(NAME_VALUES), listNameValueType);
355+
GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonObject.get(NAME_VALUES), listNameValueType);
356356

357357
return fromNameValues(nameValues);
358358
}
@@ -420,7 +420,7 @@ public void updateProfile(final String dialogId, final Integer clientId,
420420
if (clientId != null)
421421
contentJson.addProperty(CLIENT_ID, clientId);
422422

423-
contentJson.add(NAME_VALUES, GsonSingleton.getGson().toJsonTree(toNameValue(profile)));
423+
contentJson.add(NAME_VALUES, GsonSingleton.getGsonWithoutPrettyPrinting().toJsonTree(toNameValue(profile)));
424424

425425
final Request request =
426426
RequestBuilder.put(String.format(PATH_PROFILE, dialogId)).withBodyJson(contentJson).build();

src/main/java/com/ibm/watson/developer_cloud/document_conversion/v1/DocumentConversion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public Answers convertDocumentToAnswer(File document, String mediaType) {
177177
public Answers convertDocumentToAnswer(File document, String mediaType, JsonObject customConfig) {
178178
final InputStream is = convertDocument(document, mediaType, ANSWER_UNITS, customConfig);
179179
final String convertedDocument = responseToString(is);
180-
return GsonSingleton.getGson().fromJson(convertedDocument, Answers.class);
180+
return GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(convertedDocument, Answers.class);
181181
}
182182

183183
/**

src/main/java/com/ibm/watson/developer_cloud/retrieve_and_rank/v1/RetrieveAndRank.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public SolrCluster createSolrCluster(SolrClusterOptions config) {
159159
final RequestBuilder requestBuilder = RequestBuilder.post(PATH_SOLR_CLUSTERS);
160160

161161
if (config != null) {
162-
requestBuilder.withBodyContent(GsonSingleton.getGson().toJson(config),
162+
requestBuilder.withBodyContent(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(config),
163163
HttpMediaType.APPLICATION_JSON);
164164
}
165165

src/main/java/com/ibm/watson/developer_cloud/speech_to_text/v1/SpeechToText.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private void buildRecognizeRequest(RequestBuilder requestBuilder, RecognizeOptio
107107
requestBuilder.withQuery(KEYWORDS_THRESHOLD, options.getKeywordsThreshold());
108108

109109
if (options.getKeywords() != null && options.getKeywords().length > 0)
110-
requestBuilder.withQuery(KEYWORDS, GsonSingleton.getGson().toJson(options.getKeywords()));
110+
requestBuilder.withQuery(KEYWORDS, GsonSingleton.getGsonWithoutPrettyPrinting().toJson(options.getKeywords()));
111111

112112
if (options.getWordAlternativesThreshold() != null)
113113
requestBuilder.withQuery(WORD_ALTERNATIVES_THRESHOLD, options.getWordAlternativesThreshold());
@@ -219,7 +219,7 @@ public SessionStatus getRecognizeStatus(final SpeechSession session) {
219219
RequestBuilder.get(String.format(PATH_SESSION_RECOGNIZE, session.getSessionId())).build();
220220
final Response response = execute(request);
221221
final JsonObject jsonObject = ResponseUtil.getJsonObject(response);
222-
return GsonSingleton.getGson().fromJson(jsonObject.get(SESSION), SessionStatus.class);
222+
return GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonObject.get(SESSION), SessionStatus.class);
223223
}
224224

225225
/**

src/main/java/com/ibm/watson/developer_cloud/speech_to_text/v1/websocket/WebSocketSpeechToTextClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void onTextMessage(WebSocket websocket, String message) {
6868
if (json.has(ERROR)) {
6969
delegate.onError(new RuntimeException(json.get(ERROR).getAsString()));
7070
} else if (json.has(RESULTS)) {
71-
SpeechResults transcript = GsonSingleton.getGson().fromJson(message, SpeechResults.class);
71+
SpeechResults transcript = GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(message, SpeechResults.class);
7272
delegate.onMessage(transcript);
7373
} else if (audioSent) {
7474
websocket.sendClose();

src/main/java/com/ibm/watson/developer_cloud/text_to_speech/v1/TextToSpeech.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public List<Voice> getVoices() {
6666
final Response response = execute(request);
6767
final JsonObject jsonObject = ResponseUtil.getJsonObject(response);
6868
final List<Voice> voices =
69-
GsonSingleton.getGson().fromJson(jsonObject.get("voices"), listVoiceType);
69+
GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonObject.get("voices"), listVoiceType);
7070
return voices;
7171
}
7272

src/main/java/com/ibm/watson/developer_cloud/tone_analyzer/v1/ToneAnalyzer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ public List<SynonymResult> getSynonyms(SynonymOptions options) {
7979
final Request request =
8080
RequestBuilder
8181
.post(PATH_SYNONYM)
82-
.withBodyContent(GsonSingleton.getGson().toJson(options),
82+
.withBodyContent(GsonSingleton.getGsonWithoutPrettyPrinting().toJson(options),
8383
HttpMediaType.APPLICATION_JSON).build();
8484

8585
final Response response = execute(request);
8686
final String synonymResultJson = ResponseUtil.getString(response);
8787
final List<SynonymResult> synonyms =
88-
GsonSingleton.getGson().fromJson(synonymResultJson, synonymListType);
88+
GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(synonymResultJson, synonymListType);
8989
return synonyms;
9090
}
9191

@@ -137,7 +137,7 @@ public List<Scorecard> getScorecards() {
137137
final Response response = execute(RequestBuilder.get(PATH_SCORECARD).build());
138138
final JsonObject jsonObject = ResponseUtil.getJsonObject(response);
139139
final List<Scorecard> scorecards =
140-
GsonSingleton.getGson().fromJson(jsonObject.get(SCORECARDS), scorecardListType);
140+
GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonObject.get(SCORECARDS), scorecardListType);
141141

142142
return scorecards;
143143
}

src/main/java/com/ibm/watson/developer_cloud/tradeoff_analytics/v1/TradeoffAnalytics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public Dilemma dilemmas(final Problem problem) {
8484
public Dilemma dilemmas(final Problem problem, final Boolean generateVisualization) {
8585
Validate.notNull(problem, "problem was not specified");
8686

87-
final String contentJson = GsonSingleton.getGson().toJson(problem);
87+
final String contentJson = GsonSingleton.getGsonWithoutPrettyPrinting().toJson(problem);
8888

8989
final RequestBuilder requestBuilder =
9090
RequestBuilder.post(PATH_DILEMMAS).withBodyContent(contentJson,

src/main/java/com/ibm/watson/developer_cloud/util/GsonSingleton.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,52 @@
2525
public class GsonSingleton {
2626

2727
private static Gson gson;
28+
private static Gson gsonWithoutPrinting;
2829

2930
/**
3031
* Creates a {@link com.google.gson.Gson} object that can be use to serialize and deserialize Java
3132
* objects
32-
*
33+
*
34+
* @param prettyPrint if true the JSON will be pretty printed
3335
* @return the {@link Gson}
3436
*/
35-
private static Gson createGson() {
36-
GsonBuilder builder = new GsonBuilder().setPrettyPrinting();
37+
private static Gson createGson(Boolean prettyPrint) {
38+
GsonBuilder builder = new GsonBuilder();
39+
40+
registerTypeAdapters(builder);
41+
42+
if (prettyPrint)
43+
builder.setPrettyPrinting();
44+
45+
return builder.create();
46+
}
3747

48+
private static void registerTypeAdapters(GsonBuilder builder) {
3849
// Date serializer and deserializer
3950
builder.registerTypeAdapter(Date.class, new DateDeserializer());
4051
builder.registerTypeAdapter(Date.class, new DateSerializer());
41-
42-
return builder.create();
4352
}
4453

4554
/**
46-
* Gets the Gson instance
47-
*
55+
* Gets the Gson instance.
56+
*
57+
* @return the Gson
58+
*/
59+
public static Gson getGsonWithoutPrettyPrinting() {
60+
if (gsonWithoutPrinting == null) {
61+
gsonWithoutPrinting = createGson(false);
62+
}
63+
return gsonWithoutPrinting;
64+
}
65+
66+
/**
67+
* Gets the Gson instance.
68+
*
4869
* @return the Gson
4970
*/
5071
public static Gson getGson() {
5172
if (gson == null) {
52-
gson = createGson();
73+
gson = createGson(true);
5374
}
5475
return gson;
5576
}

0 commit comments

Comments
 (0)