diff --git a/modules/swagger-codegen/src/main/resources/android-java/apiInvoker.mustache b/modules/swagger-codegen/src/main/resources/android-java/apiInvoker.mustache index 721ec6e414f..e759f8264a4 100644 --- a/modules/swagger-codegen/src/main/resources/android-java/apiInvoker.mustache +++ b/modules/swagger-codegen/src/main/resources/android-java/apiInvoker.mustache @@ -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; } diff --git a/modules/swagger-codegen/src/main/resources/android-java/build.mustache b/modules/swagger-codegen/src/main/resources/android-java/build.mustache index 2e22a718fc1..80c0d375ddb 100644 --- a/modules/swagger-codegen/src/main/resources/android-java/build.mustache +++ b/modules/swagger-codegen/src/main/resources/android-java/build.mustache @@ -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" diff --git a/modules/swagger-codegen/src/main/resources/android-java/jsonUtil.mustache b/modules/swagger-codegen/src/main/resources/android-java/jsonUtil.mustache index e77eb50d85b..0680f35c948 100644 --- a/modules/swagger-codegen/src/main/resources/android-java/jsonUtil.mustache +++ b/modules/swagger-codegen/src/main/resources/android-java/jsonUtil.mustache @@ -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(); } -}; \ No newline at end of file + + public static String serialize(Object obj){ + return getGson().toJson(obj); + } + + public static T deserializeToList(String jsonString, Class cls){ + return getGson().fromJson(jsonString, getListTypeForDeserialization(cls)); + } + + public static 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>(){}.getType(); + break; + {{/model}}{{/models}} + default: + type = new TypeToken>(){}.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(){}.getType(); + break; + } + return type; + } + +}; diff --git a/samples/client/petstore/android-java/build.gradle b/samples/client/petstore/android-java/build.gradle index 2e22a718fc1..80c0d375ddb 100644 --- a/samples/client/petstore/android-java/build.gradle +++ b/samples/client/petstore/android-java/build.gradle @@ -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" diff --git a/samples/client/petstore/android-java/src/main/java/io/swagger/client/ApiInvoker.java b/samples/client/petstore/android-java/src/main/java/io/swagger/client/ApiInvoker.java index 562d12f066b..ec6218e602c 100644 --- a/samples/client/petstore/android-java/src/main/java/io/swagger/client/ApiInvoker.java +++ b/samples/client/petstore/android-java/src/main/java/io/swagger/client/ApiInvoker.java @@ -152,14 +152,10 @@ public class ApiInvoker { return str; } - public T submitUrl(String url){ - Type typeOfT = new TypeToken>(){}.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; } diff --git a/samples/client/petstore/android-java/src/main/java/io/swagger/client/JsonUtil.java b/samples/client/petstore/android-java/src/main/java/io/swagger/client/JsonUtil.java index c20de958701..962fae63cb6 100644 --- a/samples/client/petstore/android-java/src/main/java/io/swagger/client/JsonUtil.java +++ b/samples/client/petstore/android-java/src/main/java/io/swagger/client/JsonUtil.java @@ -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(); } -}; \ No newline at end of file + + public static String serialize(Object obj){ + return getGson().toJson(obj); + } + + public static T deserializeToList(String jsonString, Class cls){ + return getGson().fromJson(jsonString, getListTypeForDeserialization(cls)); + } + + public static 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>(){}.getType(); + break; + + case "Category": + type = new TypeToken>(){}.getType(); + break; + + case "Pet": + type = new TypeToken>(){}.getType(); + break; + + case "Tag": + type = new TypeToken>(){}.getType(); + break; + + case "Order": + type = new TypeToken>(){}.getType(); + break; + + default: + type = new TypeToken>(){}.getType(); + break; + } + return type; + } + + public static Type getTypeForDeserialization(Class cls) { + Type type = null; + switch (cls.getSimpleName()) { + + case "User": + type = new TypeToken(){}.getType(); + break; + + case "Category": + type = new TypeToken(){}.getType(); + break; + + case "Pet": + type = new TypeToken(){}.getType(); + break; + + case "Tag": + type = new TypeToken(){}.getType(); + break; + + case "Order": + type = new TypeToken(){}.getType(); + break; + + default: + type = new TypeToken(){}.getType(); + break; + } + return type; + } + +}; diff --git a/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Pet.java b/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Pet.java index 016fcd8c23f..93ec8bd9a42 100644 --- a/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Pet.java +++ b/samples/client/petstore/android-java/src/main/java/io/swagger/client/model/Pet.java @@ -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;