Initial commit of jackson-to-gson

This commit is contained in:
who
2015-05-06 17:46:54 -07:00
parent 4725dfb121
commit 902c56f09b
13 changed files with 80 additions and 92 deletions

View File

@@ -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.*;
@@ -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>();
@@ -159,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)) {
JavaType typeInfo = JsonUtil.getJsonMapper().getTypeFactory().constructCollectionType(List.class, cls);
List response = (List<?>) JsonUtil.getJsonMapper().readValue(json, typeInfo);
List response = (List<?>) JsonUtil.getGson().fromJson(json, cls);
return response;
}
else if(String.class.equals(cls)) {
@@ -170,10 +165,10 @@ public class ApiInvoker {
return json;
}
else {
return JsonUtil.getJsonMapper().readValue(json, cls);
return JsonUtil.getGson().fromJson(json, cls);
}
}
catch (IOException e) {
catch (JsonParseException e) {
throw new ApiException(500, e.getMessage());
}
}
@@ -181,7 +176,7 @@ public class ApiInvoker {
public static String serialize(Object obj) throws ApiException {
try {
if (obj != null)
return JsonUtil.getJsonMapper().writeValueAsString(obj);
return JsonUtil.getGson().toJson(obj);
else
return null;
}

View File

@@ -24,8 +24,10 @@ 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"
@@ -34,6 +36,7 @@ ext {
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"

View File

@@ -1,20 +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.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);
gsonBuilder = new GsonBuilder();
gsonBuilder.serializeNulls();
}
public static ObjectMapper getJsonMapper() {
return mapper;
public static Gson getGson() {
return gsonBuilder.create();
}
}
};

View File

@@ -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}}
@@ -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}}
@@ -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}};
}