Skip to content

Commit

Permalink
对于获取字符串数据,不再通过convert转换,避免转换异常问题 fixed #511
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed Oct 19, 2024
1 parent b2b129d commit 30b74c2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ public static FastJsonConverter create(ParserConfig parserConfig, SerializeConfi
}

@NotNull
@SuppressWarnings("unchecked")
@Override
public <T> T convert(@NotNull ResponseBody body, @NotNull Type type, boolean needDecodeResult) throws IOException {
try {
String result = body.string();
if (needDecodeResult) {
result = RxHttpPlugins.onResultDecoder(result);
}
if (type == String.class) {
return (T) result;
}
T t = JSON.parseObject(result, type, parserConfig);
if (t == null) {
throw new IllegalStateException("FastJsonConverter Could not deserialize body as " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ public static JacksonConverter create(ObjectMapper mapper, MediaType contentType
}

@NotNull
@SuppressWarnings("unchecked")
@Override
public <T> T convert(@NotNull ResponseBody body, @NotNull Type type, boolean needDecodeResult) throws IOException {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectReader reader = mapper.readerFor(javaType);
try {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectReader reader = mapper.readerFor(javaType);
String result = body.string();
if (needDecodeResult) {
result = RxHttpPlugins.onResultDecoder(result);
}
if (type == String.class) {
return (T) result;
}
T t = reader.readValue(result);
if (t == null) {
throw new IllegalStateException("JacksonConverter Could not deserialize body as " + type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,20 @@ public <T> T convert(@NotNull ResponseBody body, @NotNull Type type, boolean nee
if (serializeNulls) {
adapter = adapter.serializeNulls();
}
BufferedSource source;
if (needDecodeResult) {
String decodeResult = RxHttpPlugins.onResultDecoder(body.string());
if (type == String.class) {
return (T) decodeResult;
}
source = new Buffer().writeUtf8(decodeResult);
} else {
source = body.source();
}
try {
BufferedSource source;
if (needDecodeResult || type == String.class) {
String result = body.string();
if (needDecodeResult) {
result = RxHttpPlugins.onResultDecoder(result);
}
if (type == String.class) {
return (T) result;
}
source = new Buffer().writeUtf8(result);
} else {
source = body.source();
}
// Moshi has no document-level API so the responsibility of BOM skipping falls to whatever
// is delegating to it. Since it's a UTF-8-only library as well we only honor the UTF-8 BOM.
if (source.rangeEquals(0, UTF8_BOM)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,15 @@ public ProtoConverter(ExtensionRegistryLite registry, MediaType contentType) {
@SuppressWarnings("unchecked")
@Override
public <T> T convert(@NotNull ResponseBody body, @NotNull Type type, boolean needDecodeResult) throws IOException {
if (!(type instanceof Class<?>)) {
return null;
}
Class<?> c = (Class<?>) type;
if (!MessageLite.class.isAssignableFrom(c)) {
return null;
}

Parser<MessageLite> parser;
try {
Method method = c.getDeclaredMethod("parser");
//noinspection unchecked
parser = (Parser<MessageLite>) method.invoke(null);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
} catch (NoSuchMethodException | IllegalAccessException ignored) {
// If the method is missing, fall back to original static field for pre-3.0 support.
try {
Field field = c.getDeclaredField("PARSER");
//noinspection unchecked
parser = (Parser<MessageLite>) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalArgumentException("Found a protobuf message but "
+ c.getName()
+ " had no parser() method or PARSER field.");
if (!(type instanceof Class<?>)) {
throw new IllegalArgumentException("type must be a Class, but it was " + type);
}
}

try {
Class<?> c = (Class<?>) type;
if (!MessageLite.class.isAssignableFrom(c)) {
throw new IllegalArgumentException("type must be MessageLite.class, but it was " + type);
}
Parser<MessageLite> parser = getParser(c);
T t = (T) (registry == null ? parser.parseFrom(body.byteStream())
: parser.parseFrom(body.byteStream(), registry));
if (t == null) {
Expand All @@ -95,4 +75,27 @@ public <T> RequestBody convert(T value) throws IOException {
byte[] bytes = messageLite.toByteArray();
return RequestBody.create(contentType, bytes);
}

private static Parser<MessageLite> getParser(Class<?> c) {
Parser<MessageLite> parser;
try {
Method method = c.getDeclaredMethod("parser");
//noinspection unchecked
parser = (Parser<MessageLite>) method.invoke(null);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
} catch (NoSuchMethodException | IllegalAccessException ignored) {
// If the method is missing, fall back to original static field for pre-3.0 support.
try {
Field field = c.getDeclaredField("PARSER");
//noinspection unchecked
parser = (Parser<MessageLite>) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalArgumentException("Found a protobuf message but "
+ c.getName()
+ " had no parser() method or PARSER field.");
}
}
return parser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class SerializationConverter(
if (needDecodeResult) {
json = RxHttpPlugins.onResultDecoder(json)
}
if (type == String::class.java) {
return json as T
}
val serializer = format.serializersModule.serializer(type)
return format.decodeFromString(serializer, json) as T
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ public static XmlConverter createNonStrict(Serializer serializer, MediaType cont
@SuppressWarnings("unchecked")
@Override
public <T> T convert(@NotNull ResponseBody body, @NotNull Type type, boolean needDecodeResult) throws IOException {
if (!(type instanceof Class)) return null;
Class<T> cls = (Class<T>) type;
try {
if (!(type instanceof Class<?>)) {
throw new IllegalArgumentException("type must be a Class, but it was " + type);
}
Class<T> cls = (Class<T>) type;
T read;
if (needDecodeResult) {
String result = RxHttpPlugins.onResultDecoder(body.string());
if (needDecodeResult || type == String.class) {
String result = body.string();
if (needDecodeResult) {
result = RxHttpPlugins.onResultDecoder(result);
}
if (type == String.class) {
return (T) result;
}
read = serializer.read(cls, result, strict);
} else {
read = serializer.read(cls, body.charStream(), strict);
Expand Down
10 changes: 8 additions & 2 deletions rxhttp/src/main/java/rxhttp/wrapper/converter/GsonConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ public static GsonConverter create(Gson gson, MediaType contentType) {
}

@NotNull
@SuppressWarnings("unchecked")
@Override
public <T> T convert(@NotNull ResponseBody body, @NotNull Type type, boolean needDecodeResult) throws IOException {
try {
T t;
if (needDecodeResult) {
if (needDecodeResult || type == String.class) {
String result = body.string();
result = RxHttpPlugins.onResultDecoder(result);
if (needDecodeResult) {
result = RxHttpPlugins.onResultDecoder(result);
}
if (type == String.class) {
return (T) result;
}
t = gson.fromJson(result, type);
} else {
t = gson.fromJson(body.charStream(), type);
Expand Down

0 comments on commit 30b74c2

Please sign in to comment.