mirror of https://github.com/apache/cloudstack.git
bug 8353: set dateFormat to "yyyy-MM-dd'T'HH:mm:ssZ" when initiate gson builder
status 8353: resolved fixed
This commit is contained in:
parent
191d689912
commit
aa094868fc
|
|
@ -3,15 +3,14 @@ package com.cloud.api;
|
|||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class ApiGsonHelper {
|
||||
private static final GsonBuilder s_gBuilder;
|
||||
static {
|
||||
s_gBuilder = new GsonBuilder();
|
||||
private static final GsonBuilder s_gBuilder;
|
||||
static {
|
||||
s_gBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
||||
s_gBuilder.setVersion(1.3);
|
||||
s_gBuilder.registerTypeAdapter(ResponseObject.class, new ResponseObjectTypeAdapter());
|
||||
}
|
||||
|
||||
public static GsonBuilder getBuilder() {
|
||||
return s_gBuilder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static GsonBuilder getBuilder() {
|
||||
return s_gBuilder;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,32 +5,32 @@ import org.apache.log4j.Logger;
|
|||
import com.google.gson.Gson;
|
||||
|
||||
public class ApiSerializerHelper {
|
||||
public static final Logger s_logger = Logger.getLogger(ApiSerializerHelper.class.getName());
|
||||
public static final Logger s_logger = Logger.getLogger(ApiSerializerHelper.class.getName());
|
||||
public static String token = "/";
|
||||
|
||||
|
||||
public static String toSerializedStringOld(Object result) {
|
||||
if(result != null) {
|
||||
Class<?> clz = result.getClass();
|
||||
Gson gson = ApiGsonHelper.getBuilder().create();
|
||||
if (result != null) {
|
||||
Class<?> clz = result.getClass();
|
||||
Gson gson = ApiGsonHelper.getBuilder().create();
|
||||
|
||||
if (result instanceof ResponseObject) {
|
||||
return clz.getName() + token + ((ResponseObject)result).getObjectName() + token + gson.toJson(result);
|
||||
} else {
|
||||
return clz.getName() + token + gson.toJson(result);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Object fromSerializedString(String result) {
|
||||
try {
|
||||
if(result != null && !result.isEmpty()) {
|
||||
|
||||
String[] serializedParts = result.split(token);
|
||||
if (result instanceof ResponseObject) {
|
||||
return clz.getName() + token + ((ResponseObject) result).getObjectName() + token + gson.toJson(result);
|
||||
} else {
|
||||
return clz.getName() + token + gson.toJson(result);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (serializedParts.length < 2) {
|
||||
return null;
|
||||
}
|
||||
public static Object fromSerializedString(String result) {
|
||||
try {
|
||||
if (result != null && !result.isEmpty()) {
|
||||
|
||||
String[] serializedParts = result.split(token);
|
||||
|
||||
if (serializedParts.length < 2) {
|
||||
return null;
|
||||
}
|
||||
String clzName = serializedParts[0];
|
||||
String nameField = null;
|
||||
String content = null;
|
||||
|
|
@ -42,24 +42,24 @@ public class ApiSerializerHelper {
|
|||
content = result.substring(index + nameField.length() + 2);
|
||||
}
|
||||
|
||||
Class<?> clz;
|
||||
try {
|
||||
clz = Class.forName(clzName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Gson gson = ApiGsonHelper.getBuilder().create();
|
||||
Object obj = gson.fromJson(content, clz);
|
||||
if (nameField != null) {
|
||||
((ResponseObject)obj).setObjectName(nameField);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
return null;
|
||||
} catch(RuntimeException e) {
|
||||
s_logger.error("Caught runtime exception when doing GSON deserialization on: " + result);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
Class<?> clz;
|
||||
try {
|
||||
clz = Class.forName(clzName);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Gson gson = ApiGsonHelper.getBuilder().create();
|
||||
Object obj = gson.fromJson(content, clz);
|
||||
if (nameField != null) {
|
||||
((ResponseObject) obj).setObjectName(nameField);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
return null;
|
||||
} catch (RuntimeException e) {
|
||||
s_logger.error("Caught runtime exception when doing GSON deserialization on: " + result);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
package com.cloud.api;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
|
@ -14,79 +11,65 @@ import com.google.gson.JsonElement;
|
|||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class ResponseObjectTypeAdapter implements JsonSerializer<ResponseObject> {
|
||||
public static final Logger s_logger = Logger.getLogger(ResponseObjectTypeAdapter.class.getName());
|
||||
public static final Logger s_logger = Logger.getLogger(ResponseObjectTypeAdapter.class.getName());
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public JsonElement serialize(ResponseObject responseObj, Type typeOfResponseObj, JsonSerializationContext ctx) {
|
||||
JsonObject obj = new JsonObject();
|
||||
|
||||
if (responseObj instanceof SuccessResponse) {
|
||||
obj.addProperty("success", ((SuccessResponse)responseObj).getSuccess());
|
||||
return obj;
|
||||
obj.addProperty("success", ((SuccessResponse) responseObj).getSuccess());
|
||||
return obj;
|
||||
} else if (responseObj instanceof ExceptionResponse) {
|
||||
obj.addProperty("errorcode", ((ExceptionResponse)responseObj).getErrorCode());
|
||||
obj.addProperty("errortext", ((ExceptionResponse)responseObj).getErrorText());
|
||||
return obj;
|
||||
obj.addProperty("errorcode", ((ExceptionResponse) responseObj).getErrorCode());
|
||||
obj.addProperty("errortext", ((ExceptionResponse) responseObj).getErrorText());
|
||||
return obj;
|
||||
} else {
|
||||
obj.add(responseObj.getObjectName(), ApiGsonHelper.getBuilder().create().toJsonTree(responseObj));
|
||||
return obj;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
private static Method getGetMethod(Object o, String propName) {
|
||||
Method method = null;
|
||||
String methodName = getGetMethodName("get", propName);
|
||||
try {
|
||||
method = o.getClass().getMethod(methodName);
|
||||
} catch (SecurityException e1) {
|
||||
s_logger.error("Security exception in getting ResponseObject "
|
||||
+ o.getClass().getName() + " get method for property: "
|
||||
+ propName);
|
||||
} catch (NoSuchMethodException e1) {
|
||||
if (s_logger.isTraceEnabled()) {
|
||||
s_logger
|
||||
.trace("ResponseObject "
|
||||
+ o.getClass().getName()
|
||||
+ " does not have "
|
||||
+ methodName
|
||||
+ "() method for property: "
|
||||
+ propName
|
||||
+ ", will check is-prefixed method to see if it is boolean property");
|
||||
}
|
||||
}
|
||||
private static Method getGetMethod(Object o, String propName) {
|
||||
Method method = null;
|
||||
String methodName = getGetMethodName("get", propName);
|
||||
try {
|
||||
method = o.getClass().getMethod(methodName);
|
||||
} catch (SecurityException e1) {
|
||||
s_logger.error("Security exception in getting ResponseObject " + o.getClass().getName() + " get method for property: " + propName);
|
||||
} catch (NoSuchMethodException e1) {
|
||||
if (s_logger.isTraceEnabled()) {
|
||||
s_logger.trace("ResponseObject " + o.getClass().getName() + " does not have " + methodName + "() method for property: " + propName
|
||||
+ ", will check is-prefixed method to see if it is boolean property");
|
||||
}
|
||||
}
|
||||
|
||||
if (method != null)
|
||||
return method;
|
||||
if (method != null)
|
||||
return method;
|
||||
|
||||
methodName = getGetMethodName("is", propName);
|
||||
try {
|
||||
method = o.getClass().getMethod(methodName);
|
||||
} catch (SecurityException e1) {
|
||||
s_logger.error("Security exception in getting ResponseObject "
|
||||
+ o.getClass().getName() + " get method for property: "
|
||||
+ propName);
|
||||
} catch (NoSuchMethodException e1) {
|
||||
s_logger.warn("ResponseObject " + o.getClass().getName()
|
||||
+ " does not have " + methodName
|
||||
+ "() method for property: " + propName);
|
||||
}
|
||||
return method;
|
||||
}
|
||||
methodName = getGetMethodName("is", propName);
|
||||
try {
|
||||
method = o.getClass().getMethod(methodName);
|
||||
} catch (SecurityException e1) {
|
||||
s_logger.error("Security exception in getting ResponseObject " + o.getClass().getName() + " get method for property: " + propName);
|
||||
} catch (NoSuchMethodException e1) {
|
||||
s_logger.warn("ResponseObject " + o.getClass().getName() + " does not have " + methodName + "() method for property: " + propName);
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
private static String getGetMethodName(String prefix, String fieldName) {
|
||||
StringBuffer sb = new StringBuffer(prefix);
|
||||
private static String getGetMethodName(String prefix, String fieldName) {
|
||||
StringBuffer sb = new StringBuffer(prefix);
|
||||
|
||||
if (fieldName.length() >= prefix.length()
|
||||
&& fieldName.substring(0, prefix.length()).equals(prefix)) {
|
||||
return fieldName;
|
||||
} else {
|
||||
sb.append(fieldName.substring(0, 1).toUpperCase());
|
||||
sb.append(fieldName.substring(1));
|
||||
}
|
||||
if (fieldName.length() >= prefix.length() && fieldName.substring(0, prefix.length()).equals(prefix)) {
|
||||
return fieldName;
|
||||
} else {
|
||||
sb.append(fieldName.substring(0, 1).toUpperCase());
|
||||
sb.append(fieldName.substring(1));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ public class ApiResponseSerializer {
|
|||
|
||||
private static String toJSONSerializedString(ResponseObject result) {
|
||||
if (result != null) {
|
||||
Gson gson = ApiGsonHelper.getBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
|
||||
Gson gson = ApiGsonHelper.getBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("{ \"" + result.getResponseName() + "\" : ");
|
||||
|
|
|
|||
Loading…
Reference in New Issue