CLOUDSTACK-8155: Remove unwanted whitespaces from json response

This removes extra whitespaces from the JSON serialized response.
After the fix, tested to work with:
- Present UI
- CloudMonkey
- Old buggy json parsers

Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
(cherry picked from commit 921ad057def3015cda9d9f5861c9be29a88b148e)
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
This commit is contained in:
Rohit Yadav 2015-01-14 02:33:34 +05:30
parent b7b3a4fb3c
commit 185f7e0152

View File

@ -79,13 +79,13 @@ public class ApiResponseSerializer {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("{ \"").append(result.getResponseName()).append("\" : "); sb.append("{\"").append(result.getResponseName()).append("\":");
if (result instanceof ListResponse) { if (result instanceof ListResponse) {
List<? extends ResponseObject> responses = ((ListResponse)result).getResponses(); List<? extends ResponseObject> responses = ((ListResponse)result).getResponses();
Integer count = ((ListResponse)result).getCount(); Integer count = ((ListResponse)result).getCount();
boolean nonZeroCount = (count != null && count.longValue() != 0); boolean nonZeroCount = (count != null && count.longValue() != 0);
if (nonZeroCount) { if (nonZeroCount) {
sb.append("{ \"").append(ApiConstants.COUNT).append("\":").append(count); sb.append("{\"").append(ApiConstants.COUNT).append("\":").append(count);
} }
if ((responses != null) && !responses.isEmpty()) { if ((responses != null) && !responses.isEmpty()) {
@ -93,24 +93,24 @@ public class ApiResponseSerializer {
jsonStr = unescape(jsonStr); jsonStr = unescape(jsonStr);
if (nonZeroCount) { if (nonZeroCount) {
sb.append(" ,\"").append(responses.get(0).getObjectName()).append("\" : [ ").append(jsonStr); sb.append(",\"").append(responses.get(0).getObjectName()).append("\":[").append(jsonStr);
} }
for (int i = 1; i < ((ListResponse)result).getResponses().size(); i++) { for (int i = 1; i < ((ListResponse)result).getResponses().size(); i++) {
jsonStr = gson.toJson(responses.get(i)); jsonStr = gson.toJson(responses.get(i));
jsonStr = unescape(jsonStr); jsonStr = unescape(jsonStr);
sb.append(", ").append(jsonStr); sb.append(",").append(jsonStr);
} }
sb.append(" ] }"); sb.append("]}");
} else { } else {
if (!nonZeroCount) { if (!nonZeroCount) {
sb.append("{"); sb.append("{");
} }
sb.append(" }"); sb.append("}");
} }
} else if (result instanceof SuccessResponse) { } else if (result instanceof SuccessResponse) {
sb.append("{ \"success\" : \"").append(((SuccessResponse)result).getSuccess()).append("\"} "); sb.append("{\"success\":\"").append(((SuccessResponse)result).getSuccess()).append("\"}");
} else if (result instanceof ExceptionResponse) { } else if (result instanceof ExceptionResponse) {
String jsonErrorText = gson.toJson(result); String jsonErrorText = gson.toJson(result);
jsonErrorText = unescape(jsonErrorText); jsonErrorText = unescape(jsonErrorText);
@ -122,13 +122,13 @@ public class ApiResponseSerializer {
if (result instanceof AsyncJobResponse || result instanceof CreateCmdResponse || result instanceof AuthenticationCmdResponse) { if (result instanceof AsyncJobResponse || result instanceof CreateCmdResponse || result instanceof AuthenticationCmdResponse) {
sb.append(jsonStr); sb.append(jsonStr);
} else { } else {
sb.append(" { \"").append(result.getObjectName()).append("\" : ").append(jsonStr).append(" } "); sb.append("{\"").append(result.getObjectName()).append("\":").append(jsonStr).append("}");
} }
} else { } else {
sb.append("{ }"); sb.append("{}");
} }
} }
sb.append(" }"); sb.append("}");
return sb.toString(); return sb.toString();
} }
return null; return null;