|
1 | 1 | package com.dabsquared.gitlabjenkins.util;
|
2 | 2 |
|
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; |
13 | 11 | import java.text.ParseException;
|
14 | 12 | import java.text.SimpleDateFormat;
|
15 | 13 | import java.util.Arrays;
|
|
21 | 19 | */
|
22 | 20 | public final class JsonUtil {
|
23 | 21 |
|
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()); |
29 | 28 |
|
30 | 29 | private JsonUtil() { }
|
31 | 30 |
|
32 | 31 | 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 | + } |
35 | 37 | }
|
36 | 38 |
|
37 | 39 | 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 | + } |
39 | 45 | }
|
40 | 46 |
|
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)); |
53 | 67 | }
|
54 |
| - } |
55 |
| - throw new JsonParseException("Unparseable date: \"" |
56 |
| - + jsonElement.getAsString() + "\". Supported formats: " |
57 |
| - + Arrays.toString(DATE_FORMATS)); |
| 68 | + }); |
58 | 69 | }
|
59 | 70 | }
|
60 | 71 | }
|
0 commit comments