Skip to content

Commit

Permalink
反序列化为null时,抛出异常
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed Apr 5, 2022
1 parent 5171443 commit 5c2b4ff
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public <T> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public <T> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public <T> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ public <T> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public <T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,7 +55,7 @@ public <T> 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 {
Expand Down
3 changes: 2 additions & 1 deletion rxhttp/src/main/java/rxhttp/wrapper/utils/Converter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ fun <R> 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")
}

0 comments on commit 5c2b4ff

Please sign in to comment.