GsonUtils provide toJson method, GenericFilter use gson mode support toJson (#12633)

Co-authored-by: Albumen Kevin <jhq0812@gmail.com>
This commit is contained in:
TomlongTK 2023-07-11 12:49:07 +08:00 committed by GitHub
parent 4b62513dff
commit 8bb17b85f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -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) {

View File

@ -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()));
}