From 5c2b4ffce0276bc593e183511596b6ce1eeac901 Mon Sep 17 00:00:00 2001 From: liujingxing <18268800083ljx@gmail.com> Date: Tue, 5 Apr 2022 23:44:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=BAnu?= =?UTF-8?q?ll=E6=97=B6=EF=BC=8C=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/rxhttp/wrapper/converter/FastJsonConverter.java | 6 +++++- .../java/rxhttp/wrapper/converter/JacksonConverter.java | 6 +++++- .../main/java/rxhttp/wrapper/converter/MoshiConverter.java | 3 +++ .../main/java/rxhttp/wrapper/converter/ProtoConverter.java | 6 +++++- .../main/java/rxhttp/wrapper/converter/XmlConverter.java | 2 +- .../main/java/rxhttp/wrapper/converter/GsonConverter.java | 3 +-- rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.kt | 3 ++- 7 files changed, 22 insertions(+), 7 deletions(-) diff --git a/rxhttp-converter/converter-fastjson/src/main/java/rxhttp/wrapper/converter/FastJsonConverter.java b/rxhttp-converter/converter-fastjson/src/main/java/rxhttp/wrapper/converter/FastJsonConverter.java index 54ea3a35..455ae397 100644 --- a/rxhttp-converter/converter-fastjson/src/main/java/rxhttp/wrapper/converter/FastJsonConverter.java +++ b/rxhttp-converter/converter-fastjson/src/main/java/rxhttp/wrapper/converter/FastJsonConverter.java @@ -53,7 +53,11 @@ public T convert(ResponseBody body, Type type, boolean needDecodeResult) thr if (needDecodeResult) { result = RxHttpPlugins.onResultDecoder(result); } - return JSON.parseObject(result, type, parserConfig); + T t = JSON.parseObject(result, type, parserConfig); + if (t == null) { + throw new IllegalStateException("FastJsonConverter Could not deserialize body as " + type); + } + return t; } finally { body.close(); } diff --git a/rxhttp-converter/converter-jackson/src/main/java/rxhttp/wrapper/converter/JacksonConverter.java b/rxhttp-converter/converter-jackson/src/main/java/rxhttp/wrapper/converter/JacksonConverter.java index c4debbef..29325e3d 100644 --- a/rxhttp-converter/converter-jackson/src/main/java/rxhttp/wrapper/converter/JacksonConverter.java +++ b/rxhttp-converter/converter-jackson/src/main/java/rxhttp/wrapper/converter/JacksonConverter.java @@ -44,7 +44,11 @@ public T convert(ResponseBody body, Type type, boolean needDecodeResult) thr if (needDecodeResult) { result = RxHttpPlugins.onResultDecoder(result); } - return reader.readValue(result); + T t = reader.readValue(result); + if (t == null) { + throw new IllegalStateException("JacksonConverter Could not deserialize body as " + type); + } + return t; } finally { body.close(); } diff --git a/rxhttp-converter/converter-moshi/src/main/java/rxhttp/wrapper/converter/MoshiConverter.java b/rxhttp-converter/converter-moshi/src/main/java/rxhttp/wrapper/converter/MoshiConverter.java index e2d4010a..ce9eefcc 100644 --- a/rxhttp-converter/converter-moshi/src/main/java/rxhttp/wrapper/converter/MoshiConverter.java +++ b/rxhttp-converter/converter-moshi/src/main/java/rxhttp/wrapper/converter/MoshiConverter.java @@ -121,6 +121,9 @@ public T convert(ResponseBody body, Type type, boolean needDecodeResult) thr if (reader.peek() != JsonReader.Token.END_DOCUMENT) { throw new JsonDataException("JSON document was not fully consumed."); } + if (result == null) { + throw new IllegalStateException("MoshiConverter Could not deserialize body as " + type); + } return result; } finally { body.close(); diff --git a/rxhttp-converter/converter-protobuf/src/main/java/rxhttp/wrapper/converter/ProtoConverter.java b/rxhttp-converter/converter-protobuf/src/main/java/rxhttp/wrapper/converter/ProtoConverter.java index d7fe4fcf..dcb73bb2 100644 --- a/rxhttp-converter/converter-protobuf/src/main/java/rxhttp/wrapper/converter/ProtoConverter.java +++ b/rxhttp-converter/converter-protobuf/src/main/java/rxhttp/wrapper/converter/ProtoConverter.java @@ -66,8 +66,12 @@ public T convert(ResponseBody body, Type type, boolean needDecodeResult) thr } try { - return (T) (registry == null ? parser.parseFrom(body.byteStream()) + T t = (T) (registry == null ? parser.parseFrom(body.byteStream()) : parser.parseFrom(body.byteStream(), registry)); + if (t == null) { + throw new IllegalStateException("ProtoConverter Could not deserialize body as " + type); + } + return t; } catch (InvalidProtocolBufferException e) { throw new RuntimeException(e); // Despite extending IOException, this is data mismatch. } finally { diff --git a/rxhttp-converter/converter-simplexml/src/main/java/rxhttp/wrapper/converter/XmlConverter.java b/rxhttp-converter/converter-simplexml/src/main/java/rxhttp/wrapper/converter/XmlConverter.java index 3e3f4a24..d8c0fa5f 100644 --- a/rxhttp-converter/converter-simplexml/src/main/java/rxhttp/wrapper/converter/XmlConverter.java +++ b/rxhttp-converter/converter-simplexml/src/main/java/rxhttp/wrapper/converter/XmlConverter.java @@ -65,7 +65,7 @@ public T convert(@NonNull ResponseBody body, @NonNull Type type, boolean nee read = serializer.read(cls, body.charStream(), strict); } if (read == null) { - throw new IllegalStateException("Could not deserialize body as " + cls); + throw new IllegalStateException("XmlConverter Could not deserialize body as " + cls); } return read; } catch (RuntimeException | IOException e) { diff --git a/rxhttp/src/main/java/rxhttp/wrapper/converter/GsonConverter.java b/rxhttp/src/main/java/rxhttp/wrapper/converter/GsonConverter.java index 23f3338c..0050cb61 100644 --- a/rxhttp/src/main/java/rxhttp/wrapper/converter/GsonConverter.java +++ b/rxhttp/src/main/java/rxhttp/wrapper/converter/GsonConverter.java @@ -2,7 +2,6 @@ import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonWriter; @@ -56,7 +55,7 @@ public T convert(ResponseBody body, @NonNull Type type, boolean needDecodeRe if (type == String.class) return (T) result; T t = gson.fromJson(result, type); if (t == null) { - throw new JsonSyntaxException("The string '" + result + "' could not be deserialized to " + type + " object"); + throw new IllegalStateException("GsonConverter Could not deserialize body as " + type); } return t; } finally { diff --git a/rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.kt b/rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.kt index c6b00699..ac533a82 100644 --- a/rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.kt +++ b/rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.kt @@ -42,5 +42,6 @@ fun Response.convert(type: Type): R { val needDecodeResult = OkHttpCompat.needDecodeResult(this) LogUtil.log(this, null) val converter = OkHttpCompat.getConverter(this) - return converter!!.convert(body, type, needDecodeResult) + return converter?.convert(body, type, needDecodeResult) + ?: throw IllegalStateException("Converter Could not deserialize body as $type") } \ No newline at end of file