forked from loafle/openapi-generator-original
Added jsonUtil and Pair android-volley
This commit is contained in:
parent
090839fe06
commit
c3679893df
@ -239,12 +239,12 @@ public class AndroidVolleyClientCodegen extends DefaultCodegen implements Codege
|
||||
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiInvoker.java"));
|
||||
// supportingFiles.add(new SupportingFile("httpPatch.mustache",
|
||||
// (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "HttpPatch.java"));
|
||||
// supportingFiles.add(new SupportingFile("jsonUtil.mustache",
|
||||
// (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "JsonUtil.java"));
|
||||
supportingFiles.add(new SupportingFile("jsonUtil.mustache",
|
||||
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "JsonUtil.java"));
|
||||
// supportingFiles.add(new SupportingFile("apiException.mustache",
|
||||
// (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "ApiException.java"));
|
||||
// supportingFiles.add(new SupportingFile("Pair.mustache",
|
||||
// (sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "Pair.java"));
|
||||
supportingFiles.add(new SupportingFile("Pair.mustache",
|
||||
(sourceFolder + File.separator + invokerPackage).replace(".", java.io.File.separator), "Pair.java"));
|
||||
supportingFiles.add(new SupportingFile("getrequest.mustache",
|
||||
(sourceFolder + File.separator + requestPackage).replace(".", java.io.File.separator), "GetRequest.java"));
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
public class Pair {
|
||||
private String name = "";
|
||||
private String value = "";
|
||||
|
||||
public Pair(String name, String value) {
|
||||
setName(name);
|
||||
setValue(value);
|
||||
}
|
||||
|
||||
private void setName(String name) {
|
||||
if (!isValidString(name)) return;
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private void setValue(String value) {
|
||||
if (!isValidString(value)) return;
|
||||
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
private boolean isValidString(String arg) {
|
||||
if (arg == null) return false;
|
||||
if (arg.trim().isEmpty()) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import {{modelPackage}}.*;
|
||||
|
||||
public class JsonUtil {
|
||||
public static GsonBuilder gsonBuilder;
|
||||
|
||||
static {
|
||||
gsonBuilder = new GsonBuilder();
|
||||
gsonBuilder.serializeNulls();
|
||||
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||
}
|
||||
|
||||
public static Gson getGson() {
|
||||
return gsonBuilder.create();
|
||||
}
|
||||
|
||||
public static String serialize(Object obj){
|
||||
return getGson().toJson(obj);
|
||||
}
|
||||
|
||||
public static <T> T deserializeToList(String jsonString, Class cls){
|
||||
return getGson().fromJson(jsonString, getListTypeForDeserialization(cls));
|
||||
}
|
||||
|
||||
public static <T> T deserializeToObject(String jsonString, Class cls){
|
||||
return getGson().fromJson(jsonString, getTypeForDeserialization(cls));
|
||||
}
|
||||
|
||||
public static Type getListTypeForDeserialization(Class cls) {
|
||||
String className = cls.getSimpleName();
|
||||
{{#models}}{{#model}}
|
||||
if ("{{classname}}".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<List<{{classname}}>>(){}.getType();
|
||||
}
|
||||
{{/model}}{{/models}}
|
||||
return new TypeToken<List<Object>>(){}.getType();
|
||||
}
|
||||
|
||||
public static Type getTypeForDeserialization(Class cls) {
|
||||
String className = cls.getSimpleName();
|
||||
{{#models}}{{#model}}
|
||||
if ("{{classname}}".equalsIgnoreCase(className)) {
|
||||
return new TypeToken<{{classname}}>(){}.getType();
|
||||
}
|
||||
{{/model}}{{/models}}
|
||||
return new TypeToken<Object>(){}.getType();
|
||||
}
|
||||
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user