Skip to content

Commit b869300

Browse files
committed
Replace Gson completely by Jackson
1 parent 112ba65 commit b869300

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,6 @@
180180
<artifactId>guava</artifactId>
181181
<version>18.0</version>
182182
</dependency>
183-
<dependency>
184-
<groupId>com.google.code.gson</groupId>
185-
<artifactId>gson</artifactId>
186-
<version>2.2.4</version>
187-
</dependency>
188183
<dependency>
189184
<groupId>net.karneim</groupId>
190185
<artifactId>pojobuilder</artifactId>
Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.dabsquared.gitlabjenkins.util;
22

3-
import com.google.gson.FieldNamingPolicy;
4-
import com.google.gson.Gson;
5-
import com.google.gson.GsonBuilder;
6-
import com.google.gson.JsonDeserializationContext;
7-
import com.google.gson.JsonDeserializer;
8-
import com.google.gson.JsonElement;
9-
import com.google.gson.JsonParseException;
10-
import com.google.gson.JsonParser;
11-
12-
import java.lang.reflect.Type;
3+
import com.fasterxml.jackson.databind.DeserializationContext;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
7+
import com.fasterxml.jackson.databind.SerializationFeature;
8+
import com.fasterxml.jackson.databind.module.SimpleModule;
9+
10+
import java.io.IOException;
1311
import java.text.ParseException;
1412
import java.text.SimpleDateFormat;
1513
import java.util.Arrays;
@@ -21,40 +19,53 @@
2119
*/
2220
public final class JsonUtil {
2321

24-
private static final Gson prettyPrint = new GsonBuilder().setPrettyPrinting().create();
25-
private static final Gson gson = new GsonBuilder()
26-
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
27-
.registerTypeAdapter(Date.class, new DateSerializer())
28-
.create();
22+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
23+
.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
24+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
25+
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true)
26+
.configure(SerializationFeature.INDENT_OUTPUT, true)
27+
.registerModule(new DateModule());
2928

3029
private JsonUtil() { }
3130

3231
public static String toPrettyPrint(String json) {
33-
JsonParser parser = new JsonParser();
34-
return prettyPrint.toJson(parser.parse(json));
32+
try {
33+
return OBJECT_MAPPER.writeValueAsString(OBJECT_MAPPER.readValue(json, Object.class));
34+
} catch (IOException e) {
35+
throw new RuntimeException(e);
36+
}
3537
}
3638

3739
public static <T> T read(String json, Class<T> type) {
38-
return gson.fromJson(json, type);
40+
try {
41+
return OBJECT_MAPPER.readValue(json, type);
42+
} catch (IOException e) {
43+
throw new RuntimeException(e);
44+
}
3945
}
4046

41-
private static final String[] DATE_FORMATS = new String[] {
42-
"yyyy-MM-dd HH:mm:ss Z", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ssX" };
43-
44-
private static class DateSerializer implements JsonDeserializer<Date> {
45-
public Date deserialize(JsonElement jsonElement, Type typeOF,
46-
JsonDeserializationContext context) throws JsonParseException {
47-
for (String format : DATE_FORMATS) {
48-
try {
49-
return new SimpleDateFormat(format, Locale.US)
50-
.parse(jsonElement.getAsString());
51-
} catch (ParseException e) {
52-
// nothing to do
47+
private static class DateModule extends SimpleModule {
48+
private static final String[] DATE_FORMATS = new String[] {
49+
"yyyy-MM-dd HH:mm:ss Z", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd'T'HH:mm:ssX"
50+
};
51+
52+
private DateModule() {
53+
addDeserializer(Date.class, new com.fasterxml.jackson.databind.JsonDeserializer<Date>() {
54+
@Override
55+
public Date deserialize(com.fasterxml.jackson.core.JsonParser p, DeserializationContext ctxt) throws IOException {
56+
for (String format : DATE_FORMATS) {
57+
try {
58+
return new SimpleDateFormat(format, Locale.US)
59+
.parse(p.getValueAsString());
60+
} catch (ParseException e) {
61+
// nothing to do
62+
}
63+
}
64+
throw new IOException("Unparseable date: \""
65+
+ p.getValueAsString() + "\". Supported formats: "
66+
+ Arrays.toString(DATE_FORMATS));
5367
}
54-
}
55-
throw new JsonParseException("Unparseable date: \""
56-
+ jsonElement.getAsString() + "\". Supported formats: "
57-
+ Arrays.toString(DATE_FORMATS));
68+
});
5869
}
5970
}
6071
}

0 commit comments

Comments
 (0)