Skip to content

Commit ad4c869

Browse files
authored
Merge pull request #353 from Mangopay/bugfix/cardinfo-type
[bugfix] added CardInfoType serializer/deserializer
2 parents 1445978 + 5a61e9d commit ad4c869

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

src/main/java/com/mangopay/MangoPayApi.java

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import com.mangopay.core.APIs.implementation.*;
77
import com.mangopay.core.AuthorizationTokenManager;
88
import com.mangopay.core.Configuration;
9+
import com.mangopay.core.deserializer.CardInfoTypeDeserializer;
10+
import com.mangopay.core.enumerations.CardInfoType;
11+
import com.mangopay.core.serializer.CardInfoTypeSerializer;
912
import com.mangopay.entities.RateLimit;
1013

1114
import java.util.List;
@@ -55,6 +58,11 @@ public MangoPayApi() {
5558
setVirtualAccountApi(new VirtualAccountApiImpl(this));
5659
setConversionsApi(new ConversionsApiImpl(this, gsonBuilder));
5760
setIdentityVerificationApi(new IdentityVerificationApiImpl(this));
61+
62+
// register custom serializers/deserializers for objects that are used in multiple APIs
63+
gsonBuilder.registerTypeAdapter(CardInfoType.class, new CardInfoTypeSerializer());
64+
gsonBuilder.registerTypeAdapter(CardInfoType.class, new CardInfoTypeDeserializer());
65+
5866
setGson(gsonBuilder.create());
5967
}
6068

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mangopay.core.deserializer;
2+
3+
import com.google.gson.JsonDeserializationContext;
4+
import com.google.gson.JsonDeserializer;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonParseException;
7+
import com.mangopay.core.enumerations.CardInfoType;
8+
9+
import java.lang.reflect.Type;
10+
11+
public class CardInfoTypeDeserializer implements JsonDeserializer<CardInfoType> {
12+
@Override
13+
public CardInfoType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
14+
String value = json.getAsString();
15+
if ("null".equals(value)) {
16+
return null;
17+
}
18+
if ("CHARGE CARD".equals(value)) {
19+
return CardInfoType.CHARGE_CARD;
20+
}
21+
return CardInfoType.valueOf(value);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.mangopay.core.serializer;
2+
3+
import com.google.gson.*;
4+
import com.mangopay.core.enumerations.CardInfoType;
5+
6+
import java.lang.reflect.Type;
7+
8+
public class CardInfoTypeSerializer implements JsonSerializer<CardInfoType> {
9+
@Override
10+
public JsonElement serialize(CardInfoType src, Type typeOfSrc, JsonSerializationContext context) {
11+
if (src == null) {
12+
return JsonNull.INSTANCE;
13+
}
14+
if (src == CardInfoType.CHARGE_CARD) {
15+
return new JsonPrimitive("CHARGE CARD");
16+
}
17+
return new JsonPrimitive(src.name());
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.mangopay.entities;
2+
3+
import com.mangopay.core.BaseTest;
4+
import com.mangopay.core.enumerations.CardInfoType;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNull;
9+
10+
public class GsonTest extends BaseTest {
11+
@Test
12+
public void testSerializeCardInfoType() {
13+
CardInfoType type = CardInfoType.CHARGE_CARD;
14+
String json = getApi().getGson().toJson(type);
15+
assertEquals("\"CHARGE CARD\"", json);
16+
17+
type = null;
18+
json = getApi().getGson().toJson(type);
19+
assertEquals("null", json);
20+
}
21+
22+
@Test
23+
public void testDeserializeCardInfoType() {
24+
String jsonInput = "\"CHARGE CARD\"";
25+
CardInfoType deserialized = getApi().getGson().fromJson(jsonInput, CardInfoType.class);
26+
assertEquals(CardInfoType.CHARGE_CARD, deserialized);
27+
28+
jsonInput = "null";
29+
deserialized = getApi().getGson().fromJson(jsonInput, CardInfoType.class);
30+
assertNull(deserialized);
31+
}
32+
}

0 commit comments

Comments
 (0)