|
1 |
| -package de.paymill.net; |
2 |
| - |
3 |
| -import java.io.IOException; |
4 |
| -import java.lang.reflect.Type; |
5 |
| -import java.util.Date; |
6 |
| -import java.util.HashMap; |
7 |
| -import java.util.Locale; |
8 |
| -import java.util.Map; |
9 |
| - |
10 |
| -import com.google.gson.FieldNamingPolicy; |
11 |
| -import com.google.gson.Gson; |
12 |
| -import com.google.gson.GsonBuilder; |
13 |
| -import com.google.gson.JsonArray; |
14 |
| -import com.google.gson.JsonDeserializationContext; |
15 |
| -import com.google.gson.JsonDeserializer; |
16 |
| -import com.google.gson.JsonElement; |
17 |
| -import com.google.gson.JsonObject; |
18 |
| -import com.google.gson.JsonParseException; |
19 |
| -import com.google.gson.JsonPrimitive; |
20 |
| -import com.google.gson.JsonSerializationContext; |
21 |
| -import com.google.gson.JsonSerializer; |
22 |
| -import com.google.gson.TypeAdapter; |
23 |
| -import com.google.gson.TypeAdapterFactory; |
24 |
| -import com.google.gson.reflect.TypeToken; |
25 |
| -import com.google.gson.stream.JsonReader; |
26 |
| -import com.google.gson.stream.JsonToken; |
27 |
| -import com.google.gson.stream.JsonWriter; |
28 |
| - |
29 |
| -import de.paymill.PaymillException; |
30 |
| -import de.paymill.model.Client; |
31 |
| -import de.paymill.model.IPaymillObject; |
32 |
| -import de.paymill.model.Offer; |
33 |
| -import de.paymill.model.Payment; |
34 |
| -import de.paymill.model.Refund; |
35 |
| -import de.paymill.model.Subscription; |
36 |
| -import de.paymill.model.Transaction; |
37 |
| - |
38 |
| -/** |
39 |
| - * Base class for gson encoder/decoder. |
40 |
| - */ |
41 |
| -public class GsonAdapter { |
42 |
| - |
43 |
| - /** |
44 |
| - * Construct a gson instance and add custom serializers/deserializers. |
45 |
| - * |
46 |
| - * @return |
47 |
| - */ |
48 |
| - protected Gson createGson() { |
49 |
| - JsonSerializer<Date> serializer = new JsonSerializer<Date>() { |
50 |
| - @Override |
51 |
| - public JsonElement serialize(Date src, Type type, |
52 |
| - JsonSerializationContext context) { |
53 |
| - return src == null ? null : new JsonPrimitive( |
54 |
| - (int) (src.getTime() / 1000)); |
55 |
| - } |
56 |
| - }; |
57 |
| - |
58 |
| - JsonDeserializer<Date> deserializer = new JsonDeserializer<Date>() { |
59 |
| - @Override |
60 |
| - public Date deserialize(JsonElement json, Type type, |
61 |
| - JsonDeserializationContext context) |
62 |
| - throws JsonParseException { |
63 |
| - return json == null ? null : new Date(json.getAsInt() * 1000); |
64 |
| - } |
65 |
| - }; |
66 |
| - |
67 |
| - TypeAdapterFactory enumFactory = new TypeAdapterFactory() { |
68 |
| - public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { |
69 |
| - @SuppressWarnings("unchecked") |
70 |
| - Class<T> rawType = (Class<T>) type.getRawType(); |
71 |
| - if (!rawType.isEnum()) { |
72 |
| - return null; |
73 |
| - } |
74 |
| - |
75 |
| - final Map<String, T> lowercaseToConstant = new HashMap<String, T>(); |
76 |
| - for (T constant : rawType.getEnumConstants()) { |
77 |
| - lowercaseToConstant.put(toLowercase(constant), constant); |
78 |
| - } |
79 |
| - |
80 |
| - return new TypeAdapter<T>() { |
81 |
| - public void write(JsonWriter out, T value) |
82 |
| - throws IOException { |
83 |
| - if (value == null) { |
84 |
| - out.nullValue(); |
85 |
| - } else { |
86 |
| - out.value(toLowercase(value)); |
87 |
| - } |
88 |
| - } |
89 |
| - |
90 |
| - public T read(JsonReader reader) throws IOException { |
91 |
| - if (reader.peek() == JsonToken.NULL) { |
92 |
| - reader.nextNull(); |
93 |
| - return null; |
94 |
| - } else { |
95 |
| - return lowercaseToConstant.get(reader.nextString()); |
96 |
| - } |
97 |
| - } |
98 |
| - }; |
99 |
| - } |
100 |
| - |
101 |
| - private String toLowercase(Object o) { |
102 |
| - String normalized = o.toString().toLowerCase(Locale.US); |
103 |
| - return normalized.replace('_', '.'); |
104 |
| - } |
105 |
| - }; |
106 |
| - |
107 |
| - return new GsonBuilder() |
108 |
| - .registerTypeAdapter(Date.class, serializer) |
109 |
| - .registerTypeAdapter(Date.class, deserializer) |
110 |
| - .registerTypeAdapter(Client.class, new PaymillObjectDeserializer(Client.class, Client0.class)) |
111 |
| - .registerTypeAdapter(Offer.class, new PaymillObjectDeserializer(Offer.class, Offer0.class)) |
112 |
| - .registerTypeAdapter(Payment.class, new PaymillObjectDeserializer(Payment.class, Payment0.class)) |
113 |
| - .registerTypeAdapter(Refund.class, new PaymillObjectDeserializer(Refund.class, Refund0.class)) |
114 |
| - .registerTypeAdapter(Subscription.class, new SubscriptionDeserializer()) |
115 |
| - .registerTypeAdapter(Transaction.class, new PaymillObjectDeserializer(Transaction.class, Transaction0.class)) |
116 |
| - .registerTypeAdapterFactory(enumFactory) |
117 |
| - .setFieldNamingPolicy( |
118 |
| - FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); |
119 |
| - } |
120 |
| - |
121 |
| - private static class SubscriptionDeserializer implements |
122 |
| - JsonDeserializer<IPaymillObject> { |
123 |
| - |
124 |
| - @Override |
125 |
| - public IPaymillObject deserialize(JsonElement json, Type type, |
126 |
| - JsonDeserializationContext context) throws JsonParseException { |
127 |
| - if (json == null) { |
128 |
| - return null; |
129 |
| - } |
130 |
| - |
131 |
| - if (json.isJsonObject()) { |
132 |
| - JsonObject jsonObject = json.getAsJsonObject(); |
133 |
| - JsonElement offerElement = jsonObject.get("offer"); |
134 |
| - if (offerElement != null && offerElement.isJsonArray()) { |
135 |
| - JsonArray jsonArray = offerElement.getAsJsonArray(); |
136 |
| - if (jsonArray.size() == 0) { |
137 |
| - jsonObject.remove("offer"); |
138 |
| - } else { |
139 |
| - throw new IllegalArgumentException( |
140 |
| - "Received non empty offer array " + jsonArray); |
141 |
| - } |
142 |
| - } |
143 |
| - |
144 |
| - return context.deserialize(json, Subscription0.class); |
145 |
| - } |
146 |
| - try { |
147 |
| - String id = json.getAsString(); |
148 |
| - IPaymillObject instance = new Subscription(); |
149 |
| - instance.setId(id); |
150 |
| - return instance; |
151 |
| - } catch (Exception e) { |
152 |
| - throw new PaymillException("Error instantiating subscription ", |
153 |
| - e); |
154 |
| - } |
155 |
| - } |
156 |
| - } |
157 |
| - |
158 |
| - class PaymillObjectDeserializer implements JsonDeserializer<IPaymillObject> { |
159 |
| - |
160 |
| - private Class<?> targetClass; |
161 |
| - private Class<?> dummyClass; |
162 |
| - |
163 |
| - public PaymillObjectDeserializer(Class<?> targetClass, |
164 |
| - Class<?> dummyClass) { |
165 |
| - this.targetClass = targetClass; |
166 |
| - this.dummyClass = dummyClass; |
167 |
| - } |
168 |
| - |
169 |
| - @Override |
170 |
| - public IPaymillObject deserialize(JsonElement json, Type type, |
171 |
| - JsonDeserializationContext context) throws JsonParseException { |
172 |
| - if (json == null) { |
173 |
| - return null; |
174 |
| - } |
175 |
| - |
176 |
| - if (json.isJsonObject()) { |
177 |
| - return context.deserialize(json, dummyClass); |
178 |
| - } |
179 |
| - |
180 |
| - try { |
181 |
| - String id = json.getAsString(); |
182 |
| - IPaymillObject instance = (IPaymillObject)targetClass.newInstance(); |
183 |
| - instance.setId(id); |
184 |
| - return instance; |
185 |
| - } catch (Exception e) { |
186 |
| - throw new PaymillException("Error instantiating " + targetClass, e); |
187 |
| - } |
188 |
| - } |
189 |
| - }; |
190 |
| - |
191 |
| - public static class Client0 extends Client {} |
192 |
| - public static class Offer0 extends Offer {} |
193 |
| - public static class Payment0 extends Payment {} |
194 |
| - public static class Refund0 extends Refund {} |
195 |
| - public static class Subscription0 extends Subscription {} |
196 |
| - public static class Transaction0 extends Transaction {} |
197 |
| -} |
| 1 | +package de.paymill.net; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.lang.reflect.Type; |
| 5 | +import java.util.Date; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Locale; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import com.google.gson.FieldNamingPolicy; |
| 11 | +import com.google.gson.Gson; |
| 12 | +import com.google.gson.GsonBuilder; |
| 13 | +import com.google.gson.JsonArray; |
| 14 | +import com.google.gson.JsonDeserializationContext; |
| 15 | +import com.google.gson.JsonDeserializer; |
| 16 | +import com.google.gson.JsonElement; |
| 17 | +import com.google.gson.JsonObject; |
| 18 | +import com.google.gson.JsonParseException; |
| 19 | +import com.google.gson.JsonPrimitive; |
| 20 | +import com.google.gson.JsonSerializationContext; |
| 21 | +import com.google.gson.JsonSerializer; |
| 22 | +import com.google.gson.TypeAdapter; |
| 23 | +import com.google.gson.TypeAdapterFactory; |
| 24 | +import com.google.gson.reflect.TypeToken; |
| 25 | +import com.google.gson.stream.JsonReader; |
| 26 | +import com.google.gson.stream.JsonToken; |
| 27 | +import com.google.gson.stream.JsonWriter; |
| 28 | + |
| 29 | +import de.paymill.PaymillException; |
| 30 | +import de.paymill.model.Client; |
| 31 | +import de.paymill.model.IPaymillObject; |
| 32 | +import de.paymill.model.Offer; |
| 33 | +import de.paymill.model.Payment; |
| 34 | +import de.paymill.model.Refund; |
| 35 | +import de.paymill.model.Subscription; |
| 36 | +import de.paymill.model.Transaction; |
| 37 | + |
| 38 | +/** |
| 39 | + * Base class for gson encoder/decoder. |
| 40 | + */ |
| 41 | +public class GsonAdapter { |
| 42 | + |
| 43 | + /** |
| 44 | + * Construct a gson instance and add custom serializers/deserializers. |
| 45 | + * |
| 46 | + * @return |
| 47 | + */ |
| 48 | + protected Gson createGson() { |
| 49 | + JsonSerializer<Date> serializer = new JsonSerializer<Date>() { |
| 50 | + @Override |
| 51 | + public JsonElement serialize(Date src, Type type, |
| 52 | + JsonSerializationContext context) { |
| 53 | + return src == null ? null : new JsonPrimitive( |
| 54 | + (int) (src.getTime() / 1000)); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + JsonDeserializer<Date> deserializer = new JsonDeserializer<Date>() { |
| 59 | + @Override |
| 60 | + public Date deserialize(JsonElement json, Type type, |
| 61 | + JsonDeserializationContext context) |
| 62 | + throws JsonParseException { |
| 63 | + return json == null ? null : new Date(json.getAsLong() * 1000L); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + TypeAdapterFactory enumFactory = new TypeAdapterFactory() { |
| 68 | + public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { |
| 69 | + @SuppressWarnings("unchecked") |
| 70 | + Class<T> rawType = (Class<T>) type.getRawType(); |
| 71 | + if (!rawType.isEnum()) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + final Map<String, T> lowercaseToConstant = new HashMap<String, T>(); |
| 76 | + for (T constant : rawType.getEnumConstants()) { |
| 77 | + lowercaseToConstant.put(toLowercase(constant), constant); |
| 78 | + } |
| 79 | + |
| 80 | + return new TypeAdapter<T>() { |
| 81 | + public void write(JsonWriter out, T value) |
| 82 | + throws IOException { |
| 83 | + if (value == null) { |
| 84 | + out.nullValue(); |
| 85 | + } else { |
| 86 | + out.value(toLowercase(value)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public T read(JsonReader reader) throws IOException { |
| 91 | + if (reader.peek() == JsonToken.NULL) { |
| 92 | + reader.nextNull(); |
| 93 | + return null; |
| 94 | + } else { |
| 95 | + return lowercaseToConstant.get(reader.nextString()); |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + private String toLowercase(Object o) { |
| 102 | + String normalized = o.toString().toLowerCase(Locale.US); |
| 103 | + return normalized.replace('_', '.'); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + return new GsonBuilder() |
| 108 | + .registerTypeAdapter(Date.class, serializer) |
| 109 | + .registerTypeAdapter(Date.class, deserializer) |
| 110 | + .registerTypeAdapter(Client.class, new PaymillObjectDeserializer(Client.class, Client0.class)) |
| 111 | + .registerTypeAdapter(Offer.class, new PaymillObjectDeserializer(Offer.class, Offer0.class)) |
| 112 | + .registerTypeAdapter(Payment.class, new PaymillObjectDeserializer(Payment.class, Payment0.class)) |
| 113 | + .registerTypeAdapter(Refund.class, new PaymillObjectDeserializer(Refund.class, Refund0.class)) |
| 114 | + .registerTypeAdapter(Subscription.class, new SubscriptionDeserializer()) |
| 115 | + .registerTypeAdapter(Transaction.class, new PaymillObjectDeserializer(Transaction.class, Transaction0.class)) |
| 116 | + .registerTypeAdapterFactory(enumFactory) |
| 117 | + .setFieldNamingPolicy( |
| 118 | + FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); |
| 119 | + } |
| 120 | + |
| 121 | + private static class SubscriptionDeserializer implements |
| 122 | + JsonDeserializer<IPaymillObject> { |
| 123 | + |
| 124 | + @Override |
| 125 | + public IPaymillObject deserialize(JsonElement json, Type type, |
| 126 | + JsonDeserializationContext context) throws JsonParseException { |
| 127 | + if (json == null) { |
| 128 | + return null; |
| 129 | + } |
| 130 | + |
| 131 | + if (json.isJsonObject()) { |
| 132 | + JsonObject jsonObject = json.getAsJsonObject(); |
| 133 | + JsonElement offerElement = jsonObject.get("offer"); |
| 134 | + if (offerElement != null && offerElement.isJsonArray()) { |
| 135 | + JsonArray jsonArray = offerElement.getAsJsonArray(); |
| 136 | + if (jsonArray.size() == 0) { |
| 137 | + jsonObject.remove("offer"); |
| 138 | + } else { |
| 139 | + throw new IllegalArgumentException( |
| 140 | + "Received non empty offer array " + jsonArray); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return context.deserialize(json, Subscription0.class); |
| 145 | + } |
| 146 | + try { |
| 147 | + String id = json.getAsString(); |
| 148 | + IPaymillObject instance = new Subscription(); |
| 149 | + instance.setId(id); |
| 150 | + return instance; |
| 151 | + } catch (Exception e) { |
| 152 | + throw new PaymillException("Error instantiating subscription ", |
| 153 | + e); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + class PaymillObjectDeserializer implements JsonDeserializer<IPaymillObject> { |
| 159 | + |
| 160 | + private Class<?> targetClass; |
| 161 | + private Class<?> dummyClass; |
| 162 | + |
| 163 | + public PaymillObjectDeserializer(Class<?> targetClass, |
| 164 | + Class<?> dummyClass) { |
| 165 | + this.targetClass = targetClass; |
| 166 | + this.dummyClass = dummyClass; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public IPaymillObject deserialize(JsonElement json, Type type, |
| 171 | + JsonDeserializationContext context) throws JsonParseException { |
| 172 | + if (json == null) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + if (json.isJsonObject()) { |
| 177 | + return context.deserialize(json, dummyClass); |
| 178 | + } |
| 179 | + |
| 180 | + try { |
| 181 | + String id = json.getAsString(); |
| 182 | + IPaymillObject instance = (IPaymillObject)targetClass.newInstance(); |
| 183 | + instance.setId(id); |
| 184 | + return instance; |
| 185 | + } catch (Exception e) { |
| 186 | + throw new PaymillException("Error instantiating " + targetClass, e); |
| 187 | + } |
| 188 | + } |
| 189 | + }; |
| 190 | + |
| 191 | + public static class Client0 extends Client {} |
| 192 | + public static class Offer0 extends Offer {} |
| 193 | + public static class Payment0 extends Payment {} |
| 194 | + public static class Refund0 extends Refund {} |
| 195 | + public static class Subscription0 extends Subscription {} |
| 196 | + public static class Transaction0 extends Transaction {} |
| 197 | +} |
0 commit comments