Skip to content

Commit

Permalink
GsonUtils provide toJson method, GenericFilter use gson mode support …
Browse files Browse the repository at this point in the history
…toJson (#12633)

Co-authored-by: Albumen Kevin <[email protected]>
  • Loading branch information
finefuture and AlbumenJ authored Jul 11, 2023
1 parent 4b62513 commit 8bb17b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public static Object fromJson(String json, Type originType) throws RuntimeExcept
}
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) {
}
appResponse.setException(appException);
}
if (ProtocolUtils.isGenericReturnRawResult(generic)) {
return;
}
if (ProtocolUtils.isJavaGenericSerialization(generic)) {
try {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
Expand All @@ -270,8 +273,8 @@ public void onResponse(Result appResponse, Invoker<?> invoker, Invocation inv) {
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()));
}
Expand Down

0 comments on commit 8bb17b8

Please sign in to comment.