|
10 | 10 | import com.google.gson.stream.JsonWriter; |
11 | 11 |
|
12 | 12 | import java.io.IOException; |
| 13 | +import java.lang.reflect.Type; |
13 | 14 | import java.util.*; |
14 | 15 |
|
15 | 16 | public class JsonSerializers { |
16 | 17 | public static JsonSerializer<Date> getDateSerializer(){ |
17 | | - return (src, typeOfSrc, context) -> src == null ? null : new JsonPrimitive(Utils.toJsonDate(src)); |
| 18 | + return new JsonSerializer<Date>() { |
| 19 | + @Override |
| 20 | + public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { |
| 21 | + return src == null ? null : new JsonPrimitive(Utils.toJsonDate(src)); |
| 22 | + } |
| 23 | + }; |
18 | 24 | } |
19 | 25 |
|
20 | 26 | public static JsonDeserializer<Date> getDateDeserializer(){ |
21 | | - return (json, typeOfT, context) -> json == null ? null : Utils.parseDate(json.getAsString()); |
| 27 | + return new JsonDeserializer<Date>() { |
| 28 | + @Override |
| 29 | + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
| 30 | + return json == null ? null : Utils.parseDate(json.getAsString()); |
| 31 | + } |
| 32 | + }; |
22 | 33 | } |
23 | 34 |
|
24 | 35 | public static JsonSerializer<TimeSpan> getTimeSpanSerializer(){ |
25 | | - return (src, typeOfSrc, context) -> src == null ? null : new JsonPrimitive(src.toXsdDuration()); |
| 36 | + return new JsonSerializer<TimeSpan>() { |
| 37 | + @Override |
| 38 | + public JsonElement serialize(TimeSpan src, Type typeOfSrc, JsonSerializationContext context) { |
| 39 | + return src == null ? null : new JsonPrimitive(src.toXsdDuration()); |
| 40 | + } |
| 41 | + }; |
26 | 42 | } |
27 | 43 |
|
28 | 44 | public static JsonDeserializer<TimeSpan> getTimeSpanDeserializer(){ |
29 | | - return (json, typeOfT, context) -> json == null ? null : TimeSpan.parse(json.getAsString()); |
| 45 | + return new JsonDeserializer<TimeSpan>() { |
| 46 | + @Override |
| 47 | + public TimeSpan deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
| 48 | + return json == null ? null : TimeSpan.parse(json.getAsString()); |
| 49 | + } |
| 50 | + }; |
30 | 51 | } |
31 | 52 |
|
32 | 53 | public static JsonSerializer<UUID> getGuidSerializer(){ |
33 | | - return (src, typeOfSrc, context) -> src == null ? null : new JsonPrimitive(Utils.toGuidString(src)); |
| 54 | + return new JsonSerializer<UUID>() { |
| 55 | + @Override |
| 56 | + public JsonElement serialize(UUID src, Type typeOfSrc, JsonSerializationContext context) { |
| 57 | + return src == null ? null : new JsonPrimitive(Utils.toGuidString(src)); |
| 58 | + } |
| 59 | + }; |
34 | 60 | } |
35 | 61 |
|
36 | 62 | public static JsonDeserializer<UUID> getGuidDeserializer(){ |
37 | | - return (json, typeOfT, context) -> json == null ? null : Utils.fromGuidString(json.getAsString()); |
| 63 | + return new JsonDeserializer<UUID>() { |
| 64 | + @Override |
| 65 | + public UUID deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
| 66 | + return json == null ? null : Utils.fromGuidString(json.getAsString()); |
| 67 | + } |
| 68 | + }; |
38 | 69 | } |
39 | 70 |
|
40 | 71 | public static JsonSerializer<byte[]> getByteArraySerializer(){ |
41 | | - return (src, typeOfSrc, context) -> src == null ? null : new JsonPrimitive(Base64.getEncoder().encodeToString(src)); |
| 72 | + return new JsonSerializer<byte[]>() { |
| 73 | + @Override |
| 74 | + public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) { |
| 75 | + return src == null ? null : new JsonPrimitive(Base64.getEncoder().encodeToString(src)); |
| 76 | + } |
| 77 | + }; |
42 | 78 | } |
43 | 79 |
|
44 | 80 | public static JsonDeserializer<byte[]> getByteArrayDeserializer(){ |
45 | | - return (json, typeOfT, context) -> json == null ? null : Base64.getDecoder().decode(json.getAsString()); |
| 81 | + return new JsonDeserializer<byte[]>() { |
| 82 | + @Override |
| 83 | + public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { |
| 84 | + return json == null ? null : Base64.getDecoder().decode(json.getAsString()); |
| 85 | + } |
| 86 | + }; |
46 | 87 | } |
47 | 88 |
|
48 | 89 | public static class CaseInsensitiveEnumTypeAdapterFactory implements TypeAdapterFactory { |
|
0 commit comments