Skip to content

Commit

Permalink
Merge pull request #353 from Mangopay/bugfix/cardinfo-type
Browse files Browse the repository at this point in the history
[bugfix] added CardInfoType serializer/deserializer
  • Loading branch information
iulian03 authored Feb 11, 2025
2 parents 1445978 + 5a61e9d commit ad4c869
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/mangopay/MangoPayApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.mangopay.core.APIs.implementation.*;
import com.mangopay.core.AuthorizationTokenManager;
import com.mangopay.core.Configuration;
import com.mangopay.core.deserializer.CardInfoTypeDeserializer;
import com.mangopay.core.enumerations.CardInfoType;
import com.mangopay.core.serializer.CardInfoTypeSerializer;
import com.mangopay.entities.RateLimit;

import java.util.List;
Expand Down Expand Up @@ -55,6 +58,11 @@ public MangoPayApi() {
setVirtualAccountApi(new VirtualAccountApiImpl(this));
setConversionsApi(new ConversionsApiImpl(this, gsonBuilder));
setIdentityVerificationApi(new IdentityVerificationApiImpl(this));

// register custom serializers/deserializers for objects that are used in multiple APIs
gsonBuilder.registerTypeAdapter(CardInfoType.class, new CardInfoTypeSerializer());
gsonBuilder.registerTypeAdapter(CardInfoType.class, new CardInfoTypeDeserializer());

setGson(gsonBuilder.create());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mangopay.core.deserializer;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.mangopay.core.enumerations.CardInfoType;

import java.lang.reflect.Type;

public class CardInfoTypeDeserializer implements JsonDeserializer<CardInfoType> {
@Override
public CardInfoType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String value = json.getAsString();
if ("null".equals(value)) {
return null;
}
if ("CHARGE CARD".equals(value)) {
return CardInfoType.CHARGE_CARD;
}
return CardInfoType.valueOf(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mangopay.core.serializer;

import com.google.gson.*;
import com.mangopay.core.enumerations.CardInfoType;

import java.lang.reflect.Type;

public class CardInfoTypeSerializer implements JsonSerializer<CardInfoType> {
@Override
public JsonElement serialize(CardInfoType src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return JsonNull.INSTANCE;
}
if (src == CardInfoType.CHARGE_CARD) {
return new JsonPrimitive("CHARGE CARD");
}
return new JsonPrimitive(src.name());
}
}
32 changes: 32 additions & 0 deletions src/test/java/com/mangopay/entities/GsonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mangopay.entities;

import com.mangopay.core.BaseTest;
import com.mangopay.core.enumerations.CardInfoType;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class GsonTest extends BaseTest {
@Test
public void testSerializeCardInfoType() {
CardInfoType type = CardInfoType.CHARGE_CARD;
String json = getApi().getGson().toJson(type);
assertEquals("\"CHARGE CARD\"", json);

type = null;
json = getApi().getGson().toJson(type);
assertEquals("null", json);
}

@Test
public void testDeserializeCardInfoType() {
String jsonInput = "\"CHARGE CARD\"";
CardInfoType deserialized = getApi().getGson().fromJson(jsonInput, CardInfoType.class);
assertEquals(CardInfoType.CHARGE_CARD, deserialized);

jsonInput = "null";
deserialized = getApi().getGson().fromJson(jsonInput, CardInfoType.class);
assertNull(deserialized);
}
}

0 comments on commit ad4c869

Please sign in to comment.