use of com.dtflys.forest.exceptions.ForestConvertException in project forest by dromara.
the class DefaultBinaryConverter method convertToJavaObject.
private <T> T convertToJavaObject(Object source, Class<T> targetType, Charset charset) {
if (source instanceof byte[]) {
source = new ByteArrayInputStream((byte[]) source);
}
if (source instanceof InputStream) {
InputStream in = (InputStream) source;
if (InputStream.class.isAssignableFrom(targetType)) {
return (T) source;
}
if (byte[].class.isAssignableFrom(targetType)) {
return (T) inputStreamToByteArray(in);
}
byte[] tmp = inputStreamToByteArray(in);
String str = null;
try {
String encode;
if (charset == null) {
encode = ByteEncodeUtils.getCharsetName(tmp);
if (encode.toUpperCase().startsWith("GB")) {
encode = "GBK";
}
} else {
encode = charset.name();
}
str = IOUtils.toString(tmp, encode);
} catch (IOException e) {
throw new ForestRuntimeException(e);
}
if (String.class.isAssignableFrom(targetType)) {
return (T) str;
}
return autoConverter.convertToJavaObject(str, targetType);
} else if (source instanceof File) {
File file = (File) source;
if (File.class.isAssignableFrom(targetType)) {
return (T) file;
}
try {
if (InputStream.class.isAssignableFrom(targetType)) {
return (T) FileUtils.openInputStream(file);
}
if (byte[].class.isAssignableFrom(targetType)) {
return (T) FileUtils.readFileToByteArray(file);
}
String str = FileUtils.readFileToString(file);
if (String.class.isAssignableFrom(targetType)) {
return (T) str;
}
return autoConverter.convertToJavaObject(str, targetType);
} catch (IOException e) {
throw new ForestConvertException(this, e);
}
}
return convertToJavaObjectEx(source, targetType);
}
use of com.dtflys.forest.exceptions.ForestConvertException in project forest by dromara.
the class ForestGoogleProtobufConverter method convertToJavaObject.
@Override
public <T> T convertToJavaObject(byte[] source, Type targetType) {
Class<?> c = (Class<?>) targetType;
Parser<T> parser;
try {
// 转换器 都会有parser方法
Method method = c.getDeclaredMethod("parser");
// noinspection unchecked
parser = (Parser<T>) method.invoke(null);
return parser.parseFrom(source);
} catch (ReflectiveOperationException | InvalidProtocolBufferException e) {
throw new ForestConvertException(this, e);
}
}
use of com.dtflys.forest.exceptions.ForestConvertException in project forest by dromara.
the class ForestJaxbConverter method convertToJavaObject.
@Override
public <T> T convertToJavaObject(String source, Class<T> targetType) {
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(targetType);
StringReader reader = new StringReader(source);
return (T) createUnmarshaller(jaxbContext).unmarshal(reader);
} catch (JAXBException e) {
throw new ForestConvertException(this, e);
}
}
use of com.dtflys.forest.exceptions.ForestConvertException in project forest by dromara.
the class TestExceptions method testConvertException.
@Test
public void testConvertException() {
Throwable th = new Exception("xxx");
ForestConverter<?> converter = new ForestFastjsonConverter();
ForestConvertException exception = new ForestConvertException(converter, th);
assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestFastjsonConverter' error: xxx");
assertThat(exception.getConverterClass()).isEqualTo(ForestFastjsonConverter.class);
converter = new ForestJacksonConverter();
exception = new ForestConvertException(converter, th);
assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestJacksonConverter' error: xxx");
assertThat(exception.getConverterClass()).isEqualTo(ForestJacksonConverter.class);
converter = new DefaultAutoConverter(ForestConfiguration.configuration());
exception = new ForestConvertException(converter, th);
assertThat(exception.getMessage()).isEqualTo("[Forest] auto converter: 'DefaultAutoConverter' error: xxx");
assertThat(exception.getConverterClass()).isEqualTo(DefaultAutoConverter.class);
}
use of com.dtflys.forest.exceptions.ForestConvertException in project forest by dromara.
the class ForestGsonConverter method convertToJavaObject.
@Override
public <T> T convertToJavaObject(String source, Type targetType) {
if (StringUtils.isBlank(source)) {
return null;
}
try {
if (targetType instanceof ParameterizedType || targetType.getClass().getName().startsWith("com.google.gson")) {
Gson gson = createGson();
return gson.fromJson(source, targetType);
}
Class clazz = ReflectUtils.toClass(targetType);
try {
if (Map.class.isAssignableFrom(clazz)) {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = jsonParser.parse(source).getAsJsonObject();
return (T) toMap(jsonObject, false);
} else if (List.class.isAssignableFrom(clazz)) {
JsonParser jsonParser = new JsonParser();
JsonArray jsonArray = jsonParser.parse(source).getAsJsonArray();
return (T) toList(jsonArray);
}
Gson gson = createGson();
return (T) gson.fromJson(source, targetType);
} catch (Throwable th) {
throw new ForestConvertException(this, th);
}
} catch (Exception ex) {
throw new ForestConvertException(this, ex);
}
}
Aggregations