forked from loafle/openapi-generator-original
Implementing gson in android client templates
This commit is contained in:
parent
902c56f09b
commit
01b7385a5e
@ -155,8 +155,7 @@ public class ApiInvoker {
|
|||||||
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
|
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
|
||||||
try{
|
try{
|
||||||
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
|
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
|
||||||
List response = (List<?>) JsonUtil.getGson().fromJson(json, cls);
|
return JsonUtil.deserializeToList(json, cls);
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
else if(String.class.equals(cls)) {
|
else if(String.class.equals(cls)) {
|
||||||
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
|
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
|
||||||
@ -165,7 +164,7 @@ public class ApiInvoker {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return JsonUtil.getGson().fromJson(json, cls);
|
return JsonUtil.deserializeToObject(json, cls);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (JsonParseException e) {
|
catch (JsonParseException e) {
|
||||||
@ -176,7 +175,7 @@ public class ApiInvoker {
|
|||||||
public static String serialize(Object obj) throws ApiException {
|
public static String serialize(Object obj) throws ApiException {
|
||||||
try {
|
try {
|
||||||
if (obj != null)
|
if (obj != null)
|
||||||
return JsonUtil.getGson().toJson(obj);
|
return JsonUtil.serialize(obj);
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -28,17 +28,13 @@ android {
|
|||||||
ext {
|
ext {
|
||||||
swagger_annotations_version = "1.5.3-M1"
|
swagger_annotations_version = "1.5.3-M1"
|
||||||
gson_version = "2.3.1"
|
gson_version = "2.3.1"
|
||||||
jackson_version = "2.5.2"
|
|
||||||
httpclient_version = "4.3.3"
|
httpclient_version = "4.3.3"
|
||||||
junit_version = "4.8.1"
|
junit_version = "4.8.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile "com.wordnik:swagger-annotations:$swagger_annotations_version"
|
compile "com.wordnik:swagger-annotations:$swagger_annotations_version"
|
||||||
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
|
|
||||||
compile "com.google.code.gson:gson:$gson_version"
|
compile "com.google.code.gson:gson:$gson_version"
|
||||||
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
|
|
||||||
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
|
|
||||||
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
|
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
|
||||||
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
|
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
|
||||||
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
|
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
|
||||||
|
@ -2,6 +2,10 @@ package {{invokerPackage}};
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.List;
|
||||||
|
import io.swagger.client.model.*;
|
||||||
|
|
||||||
public class JsonUtil {
|
public class JsonUtil {
|
||||||
public static GsonBuilder gsonBuilder;
|
public static GsonBuilder gsonBuilder;
|
||||||
@ -9,9 +13,53 @@ public class JsonUtil {
|
|||||||
static {
|
static {
|
||||||
gsonBuilder = new GsonBuilder();
|
gsonBuilder = new GsonBuilder();
|
||||||
gsonBuilder.serializeNulls();
|
gsonBuilder.serializeNulls();
|
||||||
|
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Gson getGson() {
|
public static Gson getGson() {
|
||||||
return gsonBuilder.create();
|
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) {
|
||||||
|
Type type = null;
|
||||||
|
switch (cls.getSimpleName()) {
|
||||||
|
{{#models}}{{#model}}
|
||||||
|
case "{{classname}}":
|
||||||
|
type = new TypeToken<List<{{classname}}>>(){}.getType();
|
||||||
|
break;
|
||||||
|
{{/model}}{{/models}}
|
||||||
|
default:
|
||||||
|
type = new TypeToken<List<Object>>(){}.getType();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Type getTypeForDeserialization(Class cls) {
|
||||||
|
Type type = null;
|
||||||
|
switch (cls.getSimpleName()) {
|
||||||
|
{{#models}}{{#model}}
|
||||||
|
case "{{classname}}":
|
||||||
|
type = new TypeToken<{{classname}}>(){}.getType();
|
||||||
|
break;
|
||||||
|
{{/model}}{{/models}}
|
||||||
|
default:
|
||||||
|
type = new TypeToken<Object>(){}.getType();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
@ -28,17 +28,13 @@ android {
|
|||||||
ext {
|
ext {
|
||||||
swagger_annotations_version = "1.5.3-M1"
|
swagger_annotations_version = "1.5.3-M1"
|
||||||
gson_version = "2.3.1"
|
gson_version = "2.3.1"
|
||||||
jackson_version = "2.5.2"
|
|
||||||
httpclient_version = "4.3.3"
|
httpclient_version = "4.3.3"
|
||||||
junit_version = "4.8.1"
|
junit_version = "4.8.1"
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile "com.wordnik:swagger-annotations:$swagger_annotations_version"
|
compile "com.wordnik:swagger-annotations:$swagger_annotations_version"
|
||||||
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
|
|
||||||
compile "com.google.code.gson:gson:$gson_version"
|
compile "com.google.code.gson:gson:$gson_version"
|
||||||
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"
|
|
||||||
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
|
|
||||||
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
|
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
|
||||||
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
|
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
|
||||||
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
|
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
|
||||||
|
@ -152,14 +152,10 @@ public class ApiInvoker {
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T submitUrl(String url){
|
|
||||||
Type typeOfT = new TypeToken<RestResponse<T>>(){}.getType();
|
|
||||||
}
|
|
||||||
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
|
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
|
||||||
try{
|
try{
|
||||||
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
|
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
|
||||||
List response = (List<?>) JsonUtil.getGson().fromJson(json, cls);
|
return JsonUtil.deserializeToList(json, cls);
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
else if(String.class.equals(cls)) {
|
else if(String.class.equals(cls)) {
|
||||||
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
|
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
|
||||||
@ -168,7 +164,7 @@ public class ApiInvoker {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return JsonUtil.getGson().fromJson(json, cls);
|
return JsonUtil.deserializeToObject(json, cls);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (JsonParseException e) {
|
catch (JsonParseException e) {
|
||||||
@ -179,7 +175,7 @@ public class ApiInvoker {
|
|||||||
public static String serialize(Object obj) throws ApiException {
|
public static String serialize(Object obj) throws ApiException {
|
||||||
try {
|
try {
|
||||||
if (obj != null)
|
if (obj != null)
|
||||||
return JsonUtil.getGson().toJson(obj);
|
return JsonUtil.serialize(obj);
|
||||||
else
|
else
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,10 @@ package io.swagger.client;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.List;
|
||||||
|
import io.swagger.client.model.*;
|
||||||
|
|
||||||
public class JsonUtil {
|
public class JsonUtil {
|
||||||
public static GsonBuilder gsonBuilder;
|
public static GsonBuilder gsonBuilder;
|
||||||
@ -9,9 +13,85 @@ public class JsonUtil {
|
|||||||
static {
|
static {
|
||||||
gsonBuilder = new GsonBuilder();
|
gsonBuilder = new GsonBuilder();
|
||||||
gsonBuilder.serializeNulls();
|
gsonBuilder.serializeNulls();
|
||||||
|
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Gson getGson() {
|
public static Gson getGson() {
|
||||||
return gsonBuilder.create();
|
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) {
|
||||||
|
Type type = null;
|
||||||
|
switch (cls.getSimpleName()) {
|
||||||
|
|
||||||
|
case "User":
|
||||||
|
type = new TypeToken<List<User>>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Category":
|
||||||
|
type = new TypeToken<List<Category>>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Pet":
|
||||||
|
type = new TypeToken<List<Pet>>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Tag":
|
||||||
|
type = new TypeToken<List<Tag>>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Order":
|
||||||
|
type = new TypeToken<List<Order>>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
type = new TypeToken<List<Object>>(){}.getType();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Type getTypeForDeserialization(Class cls) {
|
||||||
|
Type type = null;
|
||||||
|
switch (cls.getSimpleName()) {
|
||||||
|
|
||||||
|
case "User":
|
||||||
|
type = new TypeToken<User>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Category":
|
||||||
|
type = new TypeToken<Category>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Pet":
|
||||||
|
type = new TypeToken<Pet>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Tag":
|
||||||
|
type = new TypeToken<Tag>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "Order":
|
||||||
|
type = new TypeToken<Order>(){}.getType();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
type = new TypeToken<Object>(){}.getType();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package io.swagger.client.model;
|
package io.swagger.client.model;
|
||||||
|
|
||||||
import io.swagger.client.model.Category;
|
import io.swagger.client.model.Category;
|
||||||
import java.util.*;
|
|
||||||
import io.swagger.client.model.Tag;
|
import io.swagger.client.model.Tag;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import com.wordnik.swagger.annotations.*;
|
import com.wordnik.swagger.annotations.*;
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user