This repository has been archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor visual changes
- Loading branch information
Showing
33 changed files
with
667 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
src/main/java/com/github/franckyi/cmpdl/api/CMPDLConverterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.github.franckyi.cmpdl.api; | ||
|
||
import okhttp3.MediaType; | ||
import okhttp3.RequestBody; | ||
import okhttp3.ResponseBody; | ||
import okio.BufferedSink; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import retrofit2.Converter; | ||
import retrofit2.Retrofit; | ||
|
||
import java.io.IOException; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CMPDLConverterFactory extends Converter.Factory { | ||
|
||
private CMPDLConverterFactory() { | ||
} | ||
|
||
public static CMPDLConverterFactory create() { | ||
return new CMPDLConverterFactory(); | ||
} | ||
|
||
@Override | ||
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { | ||
return value -> { | ||
try { | ||
if (type instanceof Class) { | ||
Class<?> clazz = (Class<?>) type; | ||
if (clazz == Instant.class) { | ||
return Instant.parse(value.string()); | ||
} else if (IBean.class.isAssignableFrom(clazz)) { | ||
return ((IBean) clazz.newInstance()).fromJson(new JSONObject(value.string())); | ||
} | ||
} else if (type instanceof ParameterizedType) { | ||
ParameterizedType type0 = (ParameterizedType) type; | ||
if (List.class.isAssignableFrom((Class<?>) type0.getRawType())) { | ||
return toList(type0, new JSONArray(value.string())); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
}; | ||
} | ||
|
||
@Override | ||
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { | ||
return value -> { | ||
try { | ||
if (value instanceof List) { | ||
return new JSONBody(fromList(((ParameterizedType) type), (List) value)); | ||
} else if (value instanceof IBean) { | ||
return new JSONBody(((IBean) value).toJson()); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
}; | ||
} | ||
|
||
public static List toList(ParameterizedType type, JSONArray array) throws Exception { | ||
Class<?> type0 = (Class<?>) getParameterUpperBound(0, type); | ||
List<Object> list = new ArrayList<>(); | ||
if (IBean.class.isAssignableFrom(type0)) { | ||
for (int i = 0; i < array.length(); i++) { | ||
list.add(((IBean) type0.newInstance()).fromJson(array.getJSONObject(i))); | ||
} | ||
} else { | ||
list.addAll(array.toList()); | ||
} | ||
return list; | ||
} | ||
|
||
public static JSONArray fromList(List<?> list) throws Exception { | ||
Type[] types = CMPDLConverterFactory.class.getDeclaredMethod("fromList", List.class).getGenericParameterTypes(); | ||
ParameterizedType type = (ParameterizedType) types[0]; | ||
return fromList(type, list); | ||
} | ||
|
||
public static JSONArray fromList(ParameterizedType type, List<?> list) { | ||
Class<?> clazz = (Class<?>) type.getActualTypeArguments()[0]; | ||
return fromList(clazz, list); | ||
} | ||
|
||
public static JSONArray fromList(Class<?> clazz, List<?> list) { | ||
JSONArray array = new JSONArray(); | ||
if (IBean.class.isAssignableFrom(clazz)) { | ||
list.stream() | ||
.map(IBean.class::cast) | ||
.map(IBean::toJson) | ||
.forEach(array::put); | ||
} else { | ||
array.put(list); | ||
} | ||
return array; | ||
} | ||
|
||
private class JSONBody extends RequestBody { | ||
private final Object json; | ||
|
||
public JSONBody(JSONArray json) { | ||
this.json = json; | ||
} | ||
|
||
public JSONBody(JSONObject json) { | ||
this.json = json; | ||
} | ||
|
||
@Override | ||
public MediaType contentType() { | ||
return MediaType.get("application/json"); | ||
} | ||
|
||
@Override | ||
public void writeTo(BufferedSink sink) throws IOException { | ||
sink.writeUtf8(json.toString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.