Implementing gson in android client templates

This commit is contained in:
Andrew B 2015-05-06 22:54:25 -07:00
parent 902c56f09b
commit 01b7385a5e
7 changed files with 137 additions and 22 deletions

View File

@ -155,8 +155,7 @@ public class ApiInvoker {
public static Object deserialize(String json, String containerType, Class cls) throws ApiException {
try{
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
List response = (List<?>) JsonUtil.getGson().fromJson(json, cls);
return response;
return JsonUtil.deserializeToList(json, cls);
}
else if(String.class.equals(cls)) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
@ -165,7 +164,7 @@ public class ApiInvoker {
return json;
}
else {
return JsonUtil.getGson().fromJson(json, cls);
return JsonUtil.deserializeToObject(json, cls);
}
}
catch (JsonParseException e) {
@ -176,7 +175,7 @@ public class ApiInvoker {
public static String serialize(Object obj) throws ApiException {
try {
if (obj != null)
return JsonUtil.getGson().toJson(obj);
return JsonUtil.serialize(obj);
else
return null;
}

View File

@ -28,17 +28,13 @@ android {
ext {
swagger_annotations_version = "1.5.3-M1"
gson_version = "2.3.1"
jackson_version = "2.5.2"
httpclient_version = "4.3.3"
junit_version = "4.8.1"
}
dependencies {
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.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:httpclient:$httpclient_version"
compile "org.apache.httpcomponents:httpmime:$httpclient_version"

View File

@ -2,6 +2,10 @@ 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 io.swagger.client.model.*;
public class JsonUtil {
public static GsonBuilder gsonBuilder;
@ -9,9 +13,53 @@ public class JsonUtil {
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) {
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;
}
};

View File

@ -28,17 +28,13 @@ android {
ext {
swagger_annotations_version = "1.5.3-M1"
gson_version = "2.3.1"
jackson_version = "2.5.2"
httpclient_version = "4.3.3"
junit_version = "4.8.1"
}
dependencies {
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.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:httpclient:$httpclient_version"
compile "org.apache.httpcomponents:httpmime:$httpclient_version"

View File

@ -152,14 +152,10 @@ public class ApiInvoker {
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 {
try{
if("list".equalsIgnoreCase(containerType) || "array".equalsIgnoreCase(containerType)) {
List response = (List<?>) JsonUtil.getGson().fromJson(json, cls);
return response;
return JsonUtil.deserializeToList(json, cls);
}
else if(String.class.equals(cls)) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
@ -168,7 +164,7 @@ public class ApiInvoker {
return json;
}
else {
return JsonUtil.getGson().fromJson(json, cls);
return JsonUtil.deserializeToObject(json, cls);
}
}
catch (JsonParseException e) {
@ -179,7 +175,7 @@ public class ApiInvoker {
public static String serialize(Object obj) throws ApiException {
try {
if (obj != null)
return JsonUtil.getGson().toJson(obj);
return JsonUtil.serialize(obj);
else
return null;
}

View File

@ -2,6 +2,10 @@ package io.swagger.client;
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 io.swagger.client.model.*;
public class JsonUtil {
public static GsonBuilder gsonBuilder;
@ -9,9 +13,85 @@ public class JsonUtil {
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) {
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;
}
};

View File

@ -1,8 +1,8 @@
package io.swagger.client.model;
import io.swagger.client.model.Category;
import java.util.*;
import io.swagger.client.model.Tag;
import java.util.*;
import com.wordnik.swagger.annotations.*;
import com.google.gson.annotations.SerializedName;