Skip to content

Feature: Use gson for android-client instead of jackson #726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public AndroidClientCodegen() {
additionalProperties.put("artifactId", artifactId);
additionalProperties.put("artifactVersion", artifactVersion);

supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("build.mustache", "", "build.gradle"));
supportingFiles.add(new SupportingFile("manifest.mustache", projectFolder, "AndroidManifest.xml"));
supportingFiles.add(new SupportingFile("apiInvoker.mustache",
Expand Down
26 changes: 10 additions & 16 deletions modules/swagger-codegen/src/main/resources/Java/JsonUtil.mustache
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package {{invokerPackage}};

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.core.JsonGenerator.Feature;

import com.fasterxml.jackson.datatype.joda.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonUtil {
public static ObjectMapper mapper;
public static GsonBuilder gsonBuilder;

static {
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.registerModule(new JodaModule());
}
gsonBuilder = new GsonBuilder();
gsonBuilder.serializeNulls();
}

public static ObjectMapper getJsonMapper() {
return mapper;
}
}
public static Gson getGson() {
return gsonBuilder.create();
}
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package {{invokerPackage}};


import com.fasterxml.jackson.core.JsonGenerator.Feature;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
Expand Down Expand Up @@ -55,6 +49,8 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import com.google.gson.JsonParseException;

public class ApiInvoker {
private static ApiInvoker INSTANCE = new ApiInvoker();
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
Expand Down Expand Up @@ -159,9 +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)) {
JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls);
List response = (List<?>) JsonUtil.getJsonMapper().readValue(json, typeInfo);
return response;
return JsonUtil.deserializeToList(json, cls);
}
else if(String.class.equals(cls)) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
Expand All @@ -170,18 +164,18 @@ public class ApiInvoker {
return json;
}
else {
return JsonUtil.getJsonMapper().readValue(json, cls);
return JsonUtil.deserializeToObject(json, cls);
}
}
catch (IOException e) {
catch (JsonParseException e) {
throw new ApiException(500, e.getMessage());
}
}

public static String serialize(Object obj) throws ApiException {
try {
if (obj != null)
return JsonUtil.getJsonMapper().writeValueAsString(obj);
return JsonUtil.serialize(obj);
else
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ android {
}
}


ext {
swagger_annotations_version = "1.5.3-M1"
jackson_version = "2.5.2"
gson_version = "2.3.1"
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.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
compile "com.google.code.gson:gson:$gson_version"
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
package {{invokerPackage}};

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.core.JsonGenerator.Feature;
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 ObjectMapper mapper;
public static GsonBuilder gsonBuilder;

static {
mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
gsonBuilder = new GsonBuilder();
gsonBuilder.serializeNulls();
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
}

public static ObjectMapper getJsonMapper() {
return mapper;
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();
}

};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package {{package}};
{{/imports}}

import com.wordnik.swagger.annotations.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.SerializedName;
{{#models}}

{{#model}}{{#description}}
Expand All @@ -17,7 +17,9 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {
public enum {{datatypeWithEnum}} {
{{#allowableValues}}{{#values}} {{.}}, {{/values}}{{/allowableValues}}
};
@SerializedName("{{baseName}}")
private {{{datatypeWithEnum}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{^isEnum}}
@SerializedName("{{baseName}}")
private {{{datatype}}} {{name}} = {{{defaultValue}}};{{/isEnum}}{{/vars}}

{{#vars}}
Expand All @@ -27,7 +29,6 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {
* maximum: {{maximum}}{{/maximum}}
**/
@ApiModelProperty({{#required}}required = {{required}}, {{/required}}value = "{{{description}}}")
@JsonProperty("{{baseName}}")
public {{{datatypeWithEnum}}} {{getter}}() {
return {{name}};
}
Expand Down
155 changes: 155 additions & 0 deletions modules/swagger-codegen/src/main/resources/android-java/pom.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>{{groupId}}</groupId>
<artifactId>{{artifactId}}</artifactId>
<packaging>jar</packaging>
<name>{{artifactId}}</name>
<version>{{artifactVersion}}</version>
<scm>
<connection>scm:git:[email protected]:wordnik/swagger-mustache.git</connection>
<developerConnection>scm:git:[email protected]:wordnik/swagger-codegen.git</developerConnection>
<url>https://github.com/wordnik/swagger-codegen</url>
</scm>
<prerequisites>
<maven>2.2.0</maven>
</prerequisites>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<systemProperties>
<property>
<name>loggerPath</name>
<value>conf/log4j.properties</value>
</property>
</systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel>
<forkMode>pertest</forkMode>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

<!-- attach test jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
<configuration>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add_sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add_test_sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson-version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpclient-version}</version>
<scope>compile</scope>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
<properties>
<swagger-annotations-version>1.5.1-M1</swagger-annotations-version>
<gson-version>2.3.1</gson-version>
<junit-version>4.8.1</junit-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version>
<httpclient-version>4.3.6</httpclient-version>
</properties>
</project>
7 changes: 3 additions & 4 deletions samples/client/petstore/android-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ android {
}
}


ext {
swagger_annotations_version = "1.5.3-M1"
jackson_version = "2.5.2"
gson_version = "2.3.1"
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.fasterxml.jackson.core:jackson-annotations:$jackson_version"
compile "com.fasterxml.jackson.core:jackson-databind:$jackson_version"
compile "com.google.code.gson:gson:$gson_version"
compile "org.apache.httpcomponents:httpcore:$httpclient_version"
compile "org.apache.httpcomponents:httpclient:$httpclient_version"
compile "org.apache.httpcomponents:httpmime:$httpclient_version"
Expand Down
Loading