GsonUtils provide toJson method, GenericFilter use gson mode support toJson (#12633)
Co-authored-by: Albumen Kevin <jhq0812@gmail.com>
This commit is contained in:
parent
4b62513dff
commit
8bb17b85f2
|
|
@ -60,6 +60,17 @@ public class GsonUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static String toJson(Object obj) throws RuntimeException {
|
||||
if (!isSupportGson()) {
|
||||
throw new RuntimeException("Gson is not supported. Please import Gson in JVM env.");
|
||||
}
|
||||
try {
|
||||
return getGson().toJson(obj);
|
||||
} catch (JsonSyntaxException ex) {
|
||||
throw new RuntimeException(String.format("Generic serialization [%s] Json syntax exception thrown when parsing (object:%s ) error:%s", GENERIC_SERIALIZATION_GSON, obj, ex.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private static Gson getGson() {
|
||||
if (gsonCache == null || !(gsonCache instanceof Gson)) {
|
||||
synchronized (GsonUtils.class) {
|
||||
|
|
|
|||
|
|
@ -244,6 +244,9 @@ public class GenericFilter implements Filter, Filter.Listener, ScopeModelAware {
|
|||
}
|
||||
appResponse.setException(appException);
|
||||
}
|
||||
if (ProtocolUtils.isGenericReturnRawResult(generic)) {
|
||||
return;
|
||||
}
|
||||
if (ProtocolUtils.isJavaGenericSerialization(generic)) {
|
||||
try {
|
||||
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
|
||||
|
|
@ -270,8 +273,8 @@ public class GenericFilter implements Filter, Filter.Listener, ScopeModelAware {
|
|||
GENERIC_SERIALIZATION_PROTOBUF +
|
||||
"] serialize result failed.", e);
|
||||
}
|
||||
} else if (ProtocolUtils.isGenericReturnRawResult(generic)) {
|
||||
return;
|
||||
} else if (ProtocolUtils.isGsonGenericSerialization(generic)) {
|
||||
appResponse.setValue(GsonUtils.toJson(appResponse.getValue()));
|
||||
} else {
|
||||
appResponse.setValue(PojoUtils.generalize(appResponse.getValue()));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue